Home Blog Page 8

delete multiple records using checkbox php

0
delete multiple records in PHP

Welcome back to shortlearner.com, in our previous post we learn How to Create pagination using php and mysql . now in this post we will learn how to delete multiple records using checkbox with the help of PHP and MySQL.

delete multiple records in PHP

before start this tutorial first of all we should understand the overall scenario of this post.
now first of all we need to create a database connection , so in this example my database name is testdb .

after creating a database connection we fetch all the records from our user table and designed it in a tabular format with checkbox field, and also create a java script function that will help us to select all check boxes at a time.
so just follow the below code.

Also Read :
Get Domain name from URL
Unable to create a directory a wordpress error
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.?

<?php
$conn = mysqli_connect("localhost","root","rootroot","testdb");
if(isset($_POST['save'])){
	$checkbox = $_POST['check'];
	for($i=0;$i<count($checkbox);$i++){
	$del_id = $checkbox[$i]; 
	mysqli_query($conn,"DELETE FROM user WHERE id='".$del_id."'");
	$message = "Data deleted successfully !";
}
}
$result = mysqli_query($conn,"SELECT * FROM user");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Delete userandroid data</title>
</head>
<body>
<div><?php if(isset($message)) { echo $message; } ?>
</div>
<form method="post" action="">
<table class="table table-bordered">
<thead>
<tr>
    <th><input type="checkbox" id="checkAl"> Select All</th>
	<th>userandroid Id</th>
	<th>First Name</th>
	
	<th>Email id</th>
</tr>
</thead>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
    <td><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row["id"]; ?>"></td>
	<td><?php echo $row["id"]; ?></td>
	<td><?php echo $row["user_name"]; ?></td>
	<td><?php echo $row["email"]; ?></td>
	
</tr>
<?php
$i++;
}
?>
</table>
<p align="center"><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
</form>
<script>
$("#checkAl").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</body>
</html>

copy the above code and save it on htdocs directory of your xampp server.
there is a www folder if you are using WAMP or Appserv local server.

share this code with your developer buddies and if you have another way to make it simple ,please feel free to contact we will update your method also in this post.

keep coding, keep learning

How to Create pagination using php and mysql

0
pagination in php and mysql

welcome back to shortlearner.com, in our previous post we learn how to create a signup or registration page with the help of AJAX, PHP and MySQL.

Now in this post today we will learn how to create a pagination using PHP and MySQL.

so most of the time we see when we are developing any kind of software and when we fetch records from our MySQL database it probably returns us thousand or millions records. so it is not a good idea to show all records on a single page, so we split all those records into multiple pages.

pagination in php and mysql

so before start this tutorial we just take an overview how we split records into multiple pages.
first of all we are establish a database connection , in this tutorial my database name is library. so i am going to fetch all the books name and their title which are stored in the table name called bookstore.

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
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
$hostname = 'localhost'; // your mysql hostname
$username = 'root';   // Your mysql db username
$password = 'rootroot';   // You mysql db password
$database = 'library';       // mysql db name
$con = mysqli_connect($hostname, $username, $password, $database);
 
    if (isset($_GET["page"])) { 
      $page  = $_GET["page"]; 
    } else { 
      $page=1; 
    };
$recordsPerPage=20; 
$start = ($page-1) * $recordsPerPage; 
$query = "SELECT * FROM bookstore LIMIT $start, $recordsPerPage"; 
$result = mysqli_query($con, $query);
 
echo "<table><tr><th>Title</th><th>Description</th></tr>";
while ($row = mysqli_fetch_assoc($result)) { 
            echo "<tr>
            <td>".$row['Title']."</td><td>".$row['Bookname']."</td></tr>";            
}
echo "</table>";
 
$query = "SELECT * FROM bookstore"; 
$result = mysqli_query($con, $query); //run the query
$totalRecords = mysqli_num_rows($result);  //count number of records
$totalPages = ceil($totalRecords / $recordsPerPage); 
 
echo "<a href='abc.php?page=1'>".'|<'."</a> "; // Go to 1st page  
 
for ($num=1; $num<=$totalPages; $num++) { 
            echo "<a href='abc.php?page=".$num."'>".$num."</a> "; 
}; 
echo "<a href='abc.php?page=$totalPages'>".'>|'."</a> "; // Go to last page
?>
</body>
</html>

How to extract all links of web page using PHP

0
extract all url of website

Welcome back to shorltearner.com, in our previous post we learn how to Convert words to numbers with the help of PHP.

extract all url of website

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
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 in this post today we will learn how to extract all links of any web page with the help of PHP and will store all the links into MySQL database.
also make a URL extractor platform which will help us to analysis the website.

if some one is using WordPress website we can extract the URLs and check which kind of themes and plugins that website developers are used.
so just follow the below code an develop your own URL extractor.

<?php 
function getAllLinks($url) {
$urlData = file_get_contents($url);
$dom = new DOMDocument();
@$dom->loadHTML($urlData);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
 for($i = 0; $i < $hrefs->length; $i++){
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    $url = filter_var($url, FILTER_SANITIZE_URL);
    if(!filter_var($url, FILTER_VALIDATE_URL) === false){
        $urlList[] = $url;  
    }
 }
return array_unique($urlList);
}
?>

so in the above code we are just creating a PHP function that takes website URL as a parameter and fetch /extract all the links.
so in the below code we are just passing website URL as a parameter to our function.

<?php
$url = 'http://shortlearner.com';
var_dump(getAllLinks($url));
?>

How to Convert 1000 to 1k |using PHP

0
how to convert number into sort form in php

Welcome back to shorltearner.com, in our previous post we learn how to Convert words to numbers with the help of PHP. so in this post today we will learn how to convert number into sort number format in PHP.
so before start this tutorial we take an overview on number_format which is also a predefined PHP function that are used to The number_format() function formats a number with grouped thousands.

how to convert number into sort form in php

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
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.?

for example if i take the below code as an example

<?php echo number_format("1000000"); ?>

than the number_format function returns an output like

outout : 1,000,000

so If we want to convert number into sort form in thousand, million, billion then we used another predefined PHP function number_format_short that will help to convert any number to it’s sort form like (Eg: 1.4K, 14.32M, 40B ) etc.
so in the below code we take an example which converts a number 1,43,20000 into 14.32M.

<?php 
    function number_format_short($n) {
        // first strip any formatting;
        $n = (0+str_replace(",", "", $n));
        // is this a number?
        if (!is_numeric($n)) return false;
         // now filter it;
        if ($n > 1000000000000) return round(($n/1000000000000), 2).'T';
        elseif ($n > 1000000000) return round(($n/1000000000), 2).'B';
        elseif ($n > 1000000) return round(($n/1000000), 2).'M';
        elseif ($n > 1000) return round(($n/1000), 2).'K';
 
        return number_format($n);
    }
echo number_format_short('14320000'); //14.32M

?>

Upload a file to the Server using|PHP

0
Upload a file to the Server

welcome back to shortlearner.com , in our previous post we learn how to Convert words to numbers in PHP. in this post we will see how to Upload a file to the Server with the help of PHP.
Let’s go through the overview of this program before starting the tutorial. So we will first create a file with the name index.php, in this file we are creating a responsive form by using bootstrap classes and taking a file from user and after submitting the form we will upload it on the server.

Upload a file to the Server

Index.php

<html>
 <head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"><!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
 </head> 
 <body> 
<form method="post"   action="index.php" enctype="multipart/form-data">
 Resume : <input type="file" name="f1" class="form-control"> <br>
<input type="submit" value="Upload" class="btn btn-success">
</form> 
</body> 
</html>

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
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.?

after submitting the form we will upload this file on the server using below code.

<?php 
if(is_uploaded_file($_FILES['f1']['tmp_name'])) {  
$fname = $_FILES['f1']['name'];            if(move_uploaded_file($_FILES['f1']['tmp_name'],"uploads/$fname"))
{   
echo "Uploaded successfully";
 } 
else
 {  
echo "Not uploaded";
 }
 } 
?>