Why page navigation not working in this code?

Codes here !

Moderators: egami, macek, gesf

Post Reply
prohor
New php-forum User
New php-forum User
Posts: 8
Joined: Sun Sep 11, 2016 8:38 am

Thu Mar 30, 2017 11:52 am

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.

Code: Select all

<?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."&nbsp;/&nbsp;".$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."&nbsp;/&nbsp;".$userProfession; ?> <br/>
				
								
				
				</p>
			</div>       
			<?php
		}
	}
	else
	{
		?>
        <div class="col-xs-12">
        	<div class="alert alert-warning">
            	<span class="glyphicon glyphicon-info-sign"></span> &nbsp; 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>

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Thu Mar 30, 2017 11:25 pm

Does you code...

Code: Select all

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);
Fetch what you expect? If so, why don't you use it in the code where your displaying the results instead of...

Code: Select all

	$stmt = $DB_con->prepare('SELECT userID, userName, userProfession, userPic FROM tbl_users ORDER BY userID DESC');
The other thing is that you seem to be mixing PDO and mysqli code together, try and stick to one or the other as it makes the whole thing easier to maintain. It will also mean you make less database connections which are always costly to do.#

Post Reply