prohor Posted March 30, 2017 Share Posted March 30, 2017 I wanted to display maximum 12 image in a page and if more is there there will show pages number like page 1, page 2.. I have tried with this code but all images showing in page1 and page2 also showing the same images. How I can limit and show first 12 image in page 1 and next 12 in page 2?Please help with the code I have tried. <?php require_once 'dbconfig.php'; if(isset($_GET['delete_id'])) { // select image from db to delete $stmt_select = $DB_con->prepare('SELECT userPic FROM tbl_users WHERE userID =:uid'); $stmt_select->execute(array(':uid'=>$_GET['delete_id'])); $imgRow=$stmt_select->fetch(PDO::FETCH_ASSOC); unlink("user_images/".$imgRow['userPic']); // it will delete an actual record from db $stmt_delete = $DB_con->prepare('DELETE FROM tbl_users WHERE userID =:uid'); $stmt_delete->bindParam(':uid',$_GET['delete_id']); $stmt_delete->execute(); header("Location: index3.php"); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> <title>new</title> <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link href="bootstrap/css/navigation.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> <div class="row"> <?php $stmt = $DB_con->prepare('SELECT userID, userName, userProfession, userPic FROM tbl_users ORDER BY userID DESC'); $stmt->execute(); if($stmt->rowCount() > 0) { while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { extract($row); ?> <div class="col-xs-3"> <!-- imgage info uper header sumon <p class="page-header">//<?php echo $userName." / ".$userProfession; ?></p> --> <img src="user_images/<?php echo $row['userPic']; ?>" class="img-rounded" width="250px" height="250px" /> <p class="page-header" align="left"> <?php echo $userName." / ".$userProfession; ?> <br/> </p> </div> <?php } } else { ?> <div class="col-xs-12"> <div class="alert alert-warning"> <span class="glyphicon glyphicon-info-sign"></span> No Data Found ... </div> </div> <?php } ?> </div> <div class="row"> <hr/> <div class="blog_navigation"> <div style="margin-top:20px;margin-left:180px"> <?php $con = mysqli_connect("localhost","root",""); if (!$con) { die(); } mysqli_select_db($con,"shopiteam"); if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = ($page-1) * 12; $sql = "SELECT * FROM tbl_users where status='process' ORDER BY albumid DESC LIMIT $start_from, 12"; //$rs_result = mysql_query ($sql,$con); $rs_result= mysqli_query($con,$sql); $sql = "SELECT COUNT(userName) FROM tbl_users"; $rs_result = mysqli_query($con,$sql); $row = mysqli_fetch_row($rs_result); $total_records = $row[0]; $total_pages = ceil($total_records / 12); for ($i=1; $i<=$total_pages; $i++) { echo "<span class='navigations_items_span'>"; echo "<b>Page </b>"; echo "<a href='index3.php?page=".$i."' class='navigation_item selected_navigation_item'>".$i."</a>"; echo "</span>"; }; ?> </div> </div> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 30, 2017 Share Posted March 30, 2017 (edited) There is nothing in your query to get the photos specific to the selected page. The query will get all records. $stmt = $DB_con->prepare('SELECT userID, userName, userProfession, userPic FROM tbl_users ORDER BY userID DESC'); $stmt->execute(); The only place where you appear to be using the $page value is for the pagination links. You need to use the page value for getting the records. Also, your code allows any user to delete any photo since you just use the id passed on the query string. I rewrote the code to have a more logical workflow and to fix the pagination. Not, I do not have your database so none of this is tested. I'm sure there are some typos or minor mistakes for you to fix. But, the underlying logic and process flow is much improved <?php //Configuration variables $recordsPerPage = 12; //Connect to DB require_once 'dbconfig.php'; //If delete request passed, perform delete operations if(isset($_GET['delete_id'])) { // select image from db to delete $stmt_select = $DB_con->prepare('SELECT userPic FROM tbl_users WHERE userID =:uid'); $stmt_select->execute(array(':uid'=>$_GET['delete_id'])); $imgRow=$stmt_select->fetch(PDO::FETCH_ASSOC); unlink("user_images/".$imgRow['userPic']); // it will delete an actual record from db $stmt_delete = $DB_con->prepare('DELETE FROM tbl_users WHERE userID =:uid'); $stmt_delete->bindParam(':uid',$_GET['delete_id']); $stmt_delete->execute(); header("Location: index3.php"); } //Get total record count and number of total pages $result = $DB_con->query('SELECT COUNT(*) FROM tbl_users'); $totalRecords = $result->fetchColumn(); $totalPages = max(1, ceil($totalRecords / $recordsPerPage)); //Determine the page to display $selectedPage = (isset($_GET["page"])) ? intval($_GET["page"]) : 1; //Ensure selected page falls in the valid range if($selectedPage<1 || $selectedPage>$totalPages) { $selectedPage = 1; } //Calculate the limit start $limitStart = ($selectedPage-1) * $recordsPerPage; //Get the records for the current page $query = 'SELECT userID, userName, userProfession, userPic FROM tbl_users ORDER BY userID DESC LIMIT :limitStart, :limitCount'; $stmt = $DB_con->prepare(array('limitStart'=>$limitStart, 'limitCount'=>$recordsPerPage)); $stmt->execute(); //Process the records into an output value $outputHTML = ''; if($stmt->rowCount() == 0) { $outputHTML .= "<div class='col-xs-12'>\n"; $outputHTML .= " <div class="alert alert-warning">\n"; $outputHTML .= " <span class="glyphicon glyphicon-info-sign"></span> No Data Found ...\n"; $outputHTML .= " </div>\n"; $outputHTML .= "</div>\n"; } else { while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { $outputHTML .= "<div class='col-xs-3'>\n"; $outputHTML .= " <!-- imgage info uper header sumon <p class='page-header'>{$row['userName'}} / {$row['userProfession']}</p> -->\n"; $outputHTML .= " <img src='user_images/{$row['userPic']}' class='img-rounded' width='250px' height='250px' />\n"; $outputHTML .= " <p class='page-header' align='left'>\n"; $outputHTML .= " {$row['userName']} / {$row['userProfession']}<br/>\n"; $outputHTML .= " </p>\n"; $outputHTML .= "</div>\n"; } } //Create pagination links $paginatonHTML = ''; for($page=1; $page<=$totalPage; $page++) { $paginatonHTML .= "<span class='navigations_items_span'>\n"; if($page==$selectedPage) { $paginatonHTML .= "<b>Page {$i}</b>\n"; } else { $paginatonHTML .= "Page <a href='index3.php?page={$p}' class='navigation_item selected_navigation_item'>{$i}</a>\n"; } $paginatonHTML .= "</span>\n"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> <title>new</title> <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link href="bootstrap/css/navigation.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> <div class="row"> <?php echo $output; ?> </div> <div class="row"> <hr/> <div class="blog_navigation"> <div style="margin-top:20px;margin-left:180px"> <?php echo $paginatonHTML; ?> </div> </div> </div> </div> </body> </html> Edited March 30, 2017 by Psycho Quote Link to comment Share on other sites More sharing options...
prohor Posted March 31, 2017 Author Share Posted March 31, 2017 (edited) There is nothing in your query to get the photos specific to the selected page. The query will get all records. $stmt = $DB_con->prepare('SELECT userID, userName, userProfession, userPic FROM tbl_users ORDER BY userID DESC'); $stmt->execute(); The only place where you appear to be using the $page value is for the pagination links. You need to use the page value for getting the records. Also, your code allows any user to delete any photo since you just use the id passed on the query string. I rewrote the code to have a more logical workflow and to fix the pagination. Not, I do not have your database so none of this is tested. I'm sure there are some typos or minor mistakes for you to fix. But, the underlying logic and process flow is much improved <?php //Configuration variables $recordsPerPage = 12; //Connect to DB require_once 'dbconfig.php'; //If delete request passed, perform delete operations if(isset($_GET['delete_id'])) { // select image from db to delete $stmt_select = $DB_con->prepare('SELECT userPic FROM tbl_users WHERE userID =:uid'); $stmt_select->execute(array(':uid'=>$_GET['delete_id'])); $imgRow=$stmt_select->fetch(PDO::FETCH_ASSOC); unlink("user_images/".$imgRow['userPic']); // it will delete an actual record from db $stmt_delete = $DB_con->prepare('DELETE FROM tbl_users WHERE userID =:uid'); $stmt_delete->bindParam(':uid',$_GET['delete_id']); $stmt_delete->execute(); header("Location: index3.php"); } //Get total record count and number of total pages $result = $DB_con->query('SELECT COUNT(*) FROM tbl_users'); $totalRecords = $result->fetchColumn(); $totalPages = max(1, ceil($totalRecords / $recordsPerPage)); //Determine the page to display $selectedPage = (isset($_GET["page"])) ? intval($_GET["page"]) : 1; //Ensure selected page falls in the valid range if($selectedPage<1 || $selectedPage>$totalPages) { $selectedPage = 1; } //Calculate the limit start $limitStart = ($selectedPage-1) * $recordsPerPage; //Get the records for the current page $query = 'SELECT userID, userName, userProfession, userPic FROM tbl_users ORDER BY userID DESC LIMIT :limitStart, :limitCount'; $stmt = $DB_con->prepare(array('limitStart'=>$limitStart, 'limitCount'=>$recordsPerPage)); $stmt->execute(); //Process the records into an output value $outputHTML = ''; if($stmt->rowCount() == 0) { $outputHTML .= "<div class='col-xs-12'>\n"; $outputHTML .= " <div class="alert alert-warning">\n"; $outputHTML .= " <span class="glyphicon glyphicon-info-sign"></span> No Data Found ...\n"; $outputHTML .= " </div>\n"; $outputHTML .= "</div>\n"; } else { while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { $outputHTML .= "<div class='col-xs-3'>\n"; $outputHTML .= " <!-- imgage info uper header sumon <p class='page-header'>{$row['userName'}} / {$row['userProfession']}</p> -->\n"; $outputHTML .= " <img src='user_images/{$row['userPic']}' class='img-rounded' width='250px' height='250px' />\n"; $outputHTML .= " <p class='page-header' align='left'>\n"; $outputHTML .= " {$row['userName']} / {$row['userProfession']}<br/>\n"; $outputHTML .= " </p>\n"; $outputHTML .= "</div>\n"; } } //Create pagination links $paginatonHTML = ''; for($page=1; $page<=$totalPage; $page++) { $paginatonHTML .= "<span class='navigations_items_span'>\n"; if($page==$selectedPage) { $paginatonHTML .= "<b>Page {$i}</b>\n"; } else { $paginatonHTML .= "Page <a href='index3.php?page={$p}' class='navigation_item selected_navigation_item'>{$i}</a>\n"; } $paginatonHTML .= "</span>\n"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> <title>new</title> <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link href="bootstrap/css/navigation.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> <div class="row"> <?php echo $output; ?> </div> <div class="row"> <hr/> <div class="blog_navigation"> <div style="margin-top:20px;margin-left:180px"> <?php echo $paginatonHTML; ?> </div> </div> </div> </div> </body> </html> There is nothing in your query to get the photos specific to the selected page. The query will get all records. $stmt = $DB_con->prepare('SELECT userID, userName, userProfession, userPic FROM tbl_users ORDER BY userID DESC'); $stmt->execute(); The only place where you appear to be using the $page value is for the pagination links. You need to use the page value for getting the records. Also, your code allows any user to delete any photo since you just use the id passed on the query string. I rewrote the code to have a more logical workflow and to fix the pagination. Not, I do not have your database so none of this is tested. I'm sure there are some typos or minor mistakes for you to fix. But, the underlying logic and process flow is much improved <?php //Configuration variables $recordsPerPage = 12; //Connect to DB require_once 'dbconfig.php'; //If delete request passed, perform delete operations if(isset($_GET['delete_id'])) { // select image from db to delete $stmt_select = $DB_con->prepare('SELECT userPic FROM tbl_users WHERE userID =:uid'); $stmt_select->execute(array(':uid'=>$_GET['delete_id'])); $imgRow=$stmt_select->fetch(PDO::FETCH_ASSOC); unlink("user_images/".$imgRow['userPic']); // it will delete an actual record from db $stmt_delete = $DB_con->prepare('DELETE FROM tbl_users WHERE userID =:uid'); $stmt_delete->bindParam(':uid',$_GET['delete_id']); $stmt_delete->execute(); header("Location: index3.php"); } //Get total record count and number of total pages $result = $DB_con->query('SELECT COUNT(*) FROM tbl_users'); $totalRecords = $result->fetchColumn(); $totalPages = max(1, ceil($totalRecords / $recordsPerPage)); //Determine the page to display $selectedPage = (isset($_GET["page"])) ? intval($_GET["page"]) : 1; //Ensure selected page falls in the valid range if($selectedPage<1 || $selectedPage>$totalPages) { $selectedPage = 1; } //Calculate the limit start $limitStart = ($selectedPage-1) * $recordsPerPage; //Get the records for the current page $query = 'SELECT userID, userName, userProfession, userPic FROM tbl_users ORDER BY userID DESC LIMIT :limitStart, :limitCount'; $stmt = $DB_con->prepare(array('limitStart'=>$limitStart, 'limitCount'=>$recordsPerPage)); $stmt->execute(); //Process the records into an output value $outputHTML = ''; if($stmt->rowCount() == 0) { $outputHTML .= "<div class='col-xs-12'>\n"; $outputHTML .= " <div class="alert alert-warning">\n"; $outputHTML .= " <span class="glyphicon glyphicon-info-sign"></span> No Data Found ...\n"; $outputHTML .= " </div>\n"; $outputHTML .= "</div>\n"; } else { while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { $outputHTML .= "<div class='col-xs-3'>\n"; $outputHTML .= " <!-- imgage info uper header sumon <p class='page-header'>{$row['userName'}} / {$row['userProfession']}</p> -->\n"; $outputHTML .= " <img src='user_images/{$row['userPic']}' class='img-rounded' width='250px' height='250px' />\n"; $outputHTML .= " <p class='page-header' align='left'>\n"; $outputHTML .= " {$row['userName']} / {$row['userProfession']}<br/>\n"; $outputHTML .= " </p>\n"; $outputHTML .= "</div>\n"; } } //Create pagination links $paginatonHTML = ''; for($page=1; $page<=$totalPage; $page++) { $paginatonHTML .= "<span class='navigations_items_span'>\n"; if($page==$selectedPage) { $paginatonHTML .= "<b>Page {$i}</b>\n"; } else { $paginatonHTML .= "Page <a href='index3.php?page={$p}' class='navigation_item selected_navigation_item'>{$i}</a>\n"; } $paginatonHTML .= "</span>\n"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" /> <title>new</title> <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link href="bootstrap/css/navigation.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> <div class="row"> <?php echo $output; ?> </div> <div class="row"> <hr/> <div class="blog_navigation"> <div style="margin-top:20px;margin-left:180px"> <?php echo $paginatonHTML; ?> </div> </div> </div> </div> </body> </html> Thank you sir, your post helped me a lot. I have now working pagination, but I can not mark the active page with css- I am going through error. I have tried with <?php if($page===$i) {echo ' class="selected"';}?> [/code] with the following code [code] for ($i=1; $i<=$totalPages; $i++) { echo "<span class='navigations_items_span'>"; if($page===$i) {echo 'class="selected"',"<b>Page </b>";}; //echo "<b>Page </b>"; echo "<a href='index3.php?page=".$i."' class='navigation_item selected_navigation_item'>".$i."</a>"; // here I should use class "selected" with above code if($page===$i) {echo 'class="selected"',"<b>Page </b>";}; Edited March 31, 2017 by prohor Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 31, 2017 Share Posted March 31, 2017 (edited) Stop writing disorganized code. There was a reason I took the time to reformat your code as I did. It makes it much easier to maintain and is more logical. I highly suggest not outputting (i.e. echo) content within the php logic. Just put the content in variables and then output the variables in the appropriate places. It makes it MUCH easier to work with the code. Your code and comments aren't making sense. It seems you have a default class as follows: class='navigation_item selected_navigation_item' But, you state you want the currently selected page to have class="selected" The first one (which I assume to be a default) has a class option that seems to infer it is for selected items since the name is 'selected_navigation_item'. So, I am going to make some more assumptions and provide code for that - since your current code and comments do not make sense. I will assume that all of the page links should have the 'navigation_item' class and that the currently selected page should also have the 'selected_navigation_item' class. <?php $output = ''; for ($pageNo=1; $pageNo<=$totalPages; $pageNo++) { //Determine correct class $class = 'navigation_item'; if ($pageNo==$page) { $class .= ' selected_navigation_item'; //Generate the output $output .= "<span class='navigations_items_span'>"; $output .= "<b>Page </b>"; $output .= "<a href='index3.php?page={$pageNo}' class='{$class}'>{$pageNo}</a>"; $output .= "</span>"; } ?> Edited March 31, 2017 by Psycho Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.