Home Blog Page 10

PHP program to print out the multiplication table up to 6*6

0
php session interview questions

Welcome back to shorltearner.com, we are starting a new series of PHP interview questions for beginners and experienced,so in previous post we learn, how to Write a PHP program that multiplies corresponding elements of two given lists.
In this post we Write a PHP program to print out the multiplication table upto 6*6.

php session interview questions

Also Read :
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
for ($i = 1; $i < 7; $i++) {
  for ($j = 1; $j < 7; $j++) {
     if ($j == 1) {
       echo str_pad($i*$j, 2, " ", STR_PAD_LEFT);
     } else {
       echo str_pad($i*$j, 4, " ", STR_PAD_LEFT);
     }
  }
  echo "\n";
}
?>

Output

1 2 3 4 5 6 2 4 6 8 10 12 3 6 9 12 15 18 4 8 12 16 20 24 5 10 15 20 25 30 6 12 18 24 30 36

multiplies corresponding elements of two given lists.

0
advanced php interview questions

Welcome back to shorltearner.com, we are starting a new series of PHP interview questions for beginners and experienced,so in this post we will see, how to Write a PHP program that multiplies corresponding elements of two given lists.

advanced php interview questions

Also Read :
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
function multiply_two_lists($x, $y)
 {
    $a = explode(' ',trim($x));
    $b = explode(' ',trim($y));
    foreach($a as $key=>$value) 
 {
        $output[$key] = $a[$key]*$b[$key];
  }
    return implode(' ',$output);
 }
echo multiply_two_lists(("10 12 3"), ("1 3 3"))."\n";
?>

Output

10 36 9

how to delete a file with the help of PHP

0
delete a file using php

Welcome back to shortlearner.com, in this post we will see how to delete a file inside the directorywith the help of PHP.

delete a file using php

so its quite easy to delete the files inside the directory with the help of a single predefined PHP function. PHP provide us a predefined function unlink.
with the help of this function we can easily delete the files from our directoryso in the below example we should try to learn how the unlink function works.

Also Read :
Get Domain name from URL
How to Send Attachment on mail using PHP.
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?


so here i am creating a variable $file_pointer , in the value of this variable we put the file name which we want to delete from our directory,

so in the below example i want to delete myfile.php from my directory so i put this inside the value of variable.

and put this variable as a parameter into unlink function
. whenever i run this file, the unlink function works and delete the myfile.php file from my directory.

How to read and insert an excel file data into MySQL.

0
read excel file and insert into mysql database using php

Welcome back to shortlearner.com, in this post we will learn how to read an excel file and insert the excel data into MySQL with the help of PHP Excel library.

read and insert an excel file data into MySQL

 Now a days i am working on a project of one of my client and he has a bulk amount of data in excel(.xlsx) format.
he wants to insert all his data into database using single process to save his time.

Also Read :
Get Domain name from URL
How to Send Attachment on mail using PHP.
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

Hence, we have created a process which fulfills our client requirement.
So, in this post we are going to share the way

how we can achieve the same. You can also try the same way which will save your time and easy to implement.


Below is the example of inserting excel data into mysql with the help of PHPExcel library.
Short overview of our working flow:


1.Create database and it’s tables.
2.Download php excel library.
3.Write a code which helps to import data into database and also shows the excel data into our webpage.


Here is the way to implement the flow :
We are creating a database named jyotishi and also creating a table ank_jyotishkosh into our database.

CREATE TABLE `ank_jyotishkosh` (
  `id` int(11) NOT NULL,
  `word` longtext,
  `image` varchar(255) NOT NULL,
  `structure` longtext,
  `meaning` longtext,
  `extra` longtext,
  `search` longtext,
  `lang` varchar(255) NOT NULL DEFAULT 'hindi',
  `status` varchar(255) NOT NULL DEFAULT 'active'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

we move to our second step and download the PHPExcel library click here to download PHPExcel library .

We are creating a config.php file, which helps to make a connection to our MySQL database.

<?php 
$con = mysqli_connect("localhost","root","rootroot","jyotishi");
 ?>

Now we are creating another PHP file where we write a code for import data into database and shows the data into web page as well.

we are creating a form in bootstrap which takes excel file from user and also add some bootstrap maxcdn files and custom CSS as well.

<!DOCTYPE html>
  <html>
  	<head>
    	<title>Import Excel data into mysql database</title>
    	<style>
		  body
		  {
		   margin:0;
		   padding:0;
		   background-color:#f1f1f1;
		  }
		  .box
		  {
		   width:700px;
		   border:1px solid #ccc;
		   background-color:#fff;
		   border-radius:5px;
		   margin-top:100px;
		  }
	  	</style>
  	</head>
  	<body>
  		<div class="container box"> <h3 align="center">Please Upload Only Excel File</h3><br />
   			<form method="post" enctype="multipart/form-data">
			    <label>Select Excel File</label>
			    <input type="file" name="excel" required/ >
			    <br />
		    	<input type="submit" name="import" class="btn btn-info" value="Import" />
		   	</form>
		   <br />
		   <br />
			<?php echo $output; ?>
  		</div>
  	</body>
</html>

In the above code you can see a php variable $output at the end of the code, which will use for showing excel data into web page.

Now move to the next step, when user hit on the import button then it checks whether the file is in the excel format or not.


If the file is in excel format then we move further otherwise it will show an error message and requests user to select valid excel file.

If user select valid excel file then we will include our php excel libaray into our code.

We put our data into PHP variable with the help of foreach loop and then shows in a table format and write an insert query which insert our data into database too.

<?php
if(isset($_POST["import"]))
{
require('config.php');
	$output = '';
	$file_name  = $_FILES["excel"]["name"];
	$tmp = explode('.', $file_name);
	$extension = end($tmp);
	$allowed_extension = array("xls", "xlsx", "csv"); //allowed extension
	if(in_array($extension, $allowed_extension)) //check selected file extension is present in allowed extension array
	{
		$file = $_FILES["excel"]["tmp_name"]; // getting temporary source of excel file
		include("PHPExcel-1.8/Classes/PHPExcel/IOFactory.php"); // Add PHPExcel Library in this code
		$objPHPExcel = PHPExcel_IOFactory::load($file);
		$output .= "<label class='text-success'>Data Inserted</label><br /><div class='container'><div class='row'><table class='table table-bordered'><tr><th>Word</th><th>Meaning</th><th>Extra</th></tr>";
		foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
		{
			$highestRow = $worksheet->getHighestRow();
			for($row=2; $row<=$highestRow; $row++)
			{
				$output .= "<tr>";
				$word = mysqli_real_escape_string($con, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
				$image = mysqli_real_escape_string($con, $worksheet->getCellByColumnAndRow(1, $row)->getValue());
				$structure = mysqli_real_escape_string($con, $worksheet->getCellByColumnAndRow(2, $row)->getValue());
				$meaning = mysqli_real_escape_string($con, $worksheet->getCellByColumnAndRow(3, $row)->getValue());
				$extra = mysqli_real_escape_string($con, $worksheet->getCellByColumnAndRow(4, $row)->getValue());
				$search = mysqli_real_escape_string($con, $worksheet->getCellByColumnAndRow(5, $row)->getValue());
				$lang = mysqli_real_escape_string($con, $worksheet->getCellByColumnAndRow(6, $row)->getValue());
				$status = mysqli_real_escape_string($con, $worksheet->getCellByColumnAndRow(7, $row)->getValue());
				mysqli_set_charset($con,'utf8'); 
				$query = "insert into ank_jyotishkosh(word,image,structure,meaning,extra,search,lang,status) values('".$word."','".$image."','".$structure."','".$meaning."','".$extra."','".$search."','".$lang."','".$status."')";
  				mysqli_query($con, $query);
			    $output .= '<td>'.$word.'</td>';
			    $output .= '<td>'.$meaning.'</td>';
			    $output .= '<td>'.$extra.'</td>';
    			$output .= '</tr>';
   			}
  		} 
  		$output .= '</table></div></div>';
	}
	else
	{
		$output = '<label class="text-danger">Invalid File</label>'; //if non excel file then
	}
}
?>

Note : When you run this code and got some error then please make sure you have enabled zip extension on your server.This is the common error which we receive.


If you would like to know how to check whether extension is enabled or not, please check our post on how to enable extension on server.


Keep learning.
Stay connected with us for any queries.

Enable PHP Zip Extension through cPanel

0
how to enable extensions in cpanel

Welcome back to shortlearner.com, in this post we will see how to enable PHP ZIP extension on cPanel.
before start this tutorial, we should take a overview about PHP extension.
so basically PHP extension enables you to transparently read or write ZIP compressed archives and the files inside them.

how to enable extensions in cpanel


and most of the time it is disable by default in cPanel , so we have to manually install it.
So just follow the steps and install these extensions manually.
Note: try to follow snaps for better understanding.

step 1: So in the very first step , we should login into our cPanel(Control Panel) and than go to the software section
Step 2: in the software section click on PHP Pear Packages

Enable PHP ZIP Extensions in cPanel

Also Read :
Get Domain name from URL
How to Send Attachment on mail using PHP.
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

Step3: after clicking on PHP Pear Packages we are searching our ZIP extension and install it.

enable php extension

step 4 : After installing the package we should again go to our software section and click on the PHP version .

install zip archive

Step5: once we click on the select PHP Version, we can see all the enable extension over there, and we can see our Archiv_ZIP as well. so we just checked it and hit on the save button to add archive_zip in our current PHP version.

enable php zip extension

Step5: now the last and very important step we will follow to complete this tutorial.again go to the software section and click on the PHP processes and hit the kill process button.

zip enable in cpanel

Now we can use your PHP ZIP Extension our website.