Using Pagination with Mysql Queries - php

I am unable to maintain a GET variable with this pagination script. Was hoping you all could help.
I am using a GET function so the user can choose which categories are displayed. I'm using a pagination script I found online and I am now confronted with this issue, when I choose "page 2" $dynCat no longer is parsed in the URL because the pagination script creates a new URL and uses GET also.
Could someone help me out with using pagination with a variable query? Thank you.
ERROR Notice: Undefined index: dynCat ----/dynCat.php on line 32
I understand why it is undefined, I just don't know how to integrate and maintain the user's Query (i.e. GET variable from URL) with the pagination script. Thanks again.
Page1 index.php (User Selects option)
<?php
//Generate and list Categories include "storescripts/connect_to_mysql.php";
$dynCat = "";
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$listcategory = $info["category"];
$dynCat .=
'
<li>
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a>
</li>
';
}
mysql_close();
?>
<?php echo $dynCat; ?>
Page2 dynCat.php (User Views Selection with Pagination)
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
//Generate and list Categories
include "storescripts/connect_to_mysql.php";
$dynGallery = "";
$dynCat = "";
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$listcategory = $info["category"];
$dynCat .=
'
<li>
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a>
</li>
';
}
mysql_close();
?>
<?php
//Query User Selection & Pagination
include('storescripts/connect_to_mysql.php');
$cat = mysql_escape_string($_GET['dynCat']);
$tableName="products";
$targetpage = "dynCat.php";
$limit = 3;
$query = "SELECT COUNT(*) as num FROM $tableName WHERE category = '$cat'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
$stages = 3;
$page = mysql_escape_string(isset($_GET['page'])) ? mysql_escape_string($_GET['page']) : 0;
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get category Data and Images
$query1 = "SELECT * FROM $tableName WHERE category = '$cat' ORDER BY views DESC LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev#gallery'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>";
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>";
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next#gallery'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
mysql_close();
?>
<?php
$productCount = mysql_num_rows($result); // count the output amount
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynGallery .=
'
<div>
<li>
<a href="product.php?id=' . $id . '#gallery">
<img style="border:#FFF 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="180" height="255" border="1" style="opacity: 0;/></a>
<h4> <a name= ' . $id . ' id= ' . $id . ' value= ' . $id . ' href="product.php?id= ' . $id . '#gallery"> ' . $product_name . ' </a> </h4>
<p>'. $category .'</p>
<p>'. $subcategory .'</p>
<span> $' . $price . '</span>
</li>
</div>
';
}
?>
<!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">
<body>
<?php include ("../header.php");?>
<?php include ("../menu.php");?>
<a id="shop"></a>
<div class="body">
<div class="sidebar">
<div class="first">
<h2>Dora Designs</h2>
<!-- Dynamic Categories -->
<ul>
<?php echo $dynCat; ?>
</ul>
</div>
</div>
<div class="content">
<div class="figure">
<img src="/images/galleryholder.png" alt=""/>
</div>
<div class="products">
<div class="paging" align = "center">
<a id="gallery"></a>
<? echo $paginate;?>
<? echo '</br>'. $cat;?>
<? echo '</br>'. $total_pages.' Results'; ?>
</div>
<ul>
<?php echo $dynGallery; ?>
</ul>
I hate to dump this much code in the post but I am really at a loss and have never used Pagination before. Any help will be greatly appreciated.

change ever instance of
$targetpage?page=
to
$targetpage?dynCat=$cat&page=
you want 2 copies of $cat one for the db requires escaping with mysql_escape_string() the other for the url with urlencode()
replace the line
$cat = mysql_escape_string($_GET['dynCat']);
with
$cat=urldecode($_GET['dynCat']); //from the url, raw value to display
$cat_mysql = mysql_escape_string($cat); //use in mysql queries
$cat_url=urlencode($cat); //use in the url
then
$targetpage?dynCat=$cat&page=
will now be
$targetpage?dynCat=$cat_url&page=

Try adding dynCat=$cat to all of your links that look like this - <a href='$targetpage?page=...
<a href='$targetpage?dynCat=$capage=...
on a quick search there are approx. 14 of them to change.

Related

pagination using post id

Am new to php, i am try to use pagenation , where i post a unique id to a page then use that to load content with the respective id then pagenate all the content with that respective id.
Here below is what have been trying
<?php
if (isset($_GET["post"]))
{
$page = $_GET["post"];
} else
{
$page = 1;
};
$limit = 2;
$total_records = $pagination;
$total_pages = ceil($total_records/$limit);
$lastpage = ceil($total_pages/$limit);
$page != 0;
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
//lastpage is = total pages / items per page, rounded up.
if($prev == 0)
$prev = 1;
$next <= $lastpage;
//if no page var is given, default to 1.
$pagLink = "<ul class='pagination pagination-circle' class='justify- content-center'>";
$pagLink .= "<li class='page-item active'>
<a class='page-link black' href='home.php?post=".$prev."' aria-label='Back'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Next</span>
</a>
</li>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li class='page-item active'><a class='page-link' href='home.php?post=".$i."'>".$i."</a></li> ";
};
$pagLink .= "<li class='page-item pg-red active'>
<a class='page-link black' href='home.php?post=".$next."' aria-label='Next'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a>
</li>";
echo $pagLink . "</ul>";
?>
Here is how i want to pagenate contents per id
enter image description here

$_GET getting destroyed on Pagination

I am learning PHP. I am trying to use pagination in my one of page. If its first page, all is working fine but as soon as I press next page button its giving me error like
Notice: Undefined index: number1 in C:\xampp\htdocs\mycode\compare.php on line 6
Notice: Undefined index: number2 in C:\xampp\htdocs\mycode\compare.php on line 7
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\mycode\compare.php on line 18
I think its due to $_GET which I am using in line 6 and 7 but I do not know why its getting destroyed as soon as I try to go on next page. My full code is like below
<?php include("includes/header.php");
require("includes/function.php");
require("language/language.php");
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$tableName="number_status";
$targetpage = "compare.php";
$limit = 10;
$sr=0;
$query = "SELECT COUNT(*) as num FROM $tableName WHERE number = $number1 OR number = $number2 ";
$total_pages = mysqli_fetch_array(mysqli_query($mysqli,$query));
$total_pages = $total_pages['num'];
$stages = 3;
$page=0;
if(isset($_GET['page'])){
$page = mysqli_real_escape_string($mysqli,$_GET['page']);
}
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
$quotes_qry="SELECT * FROM number_status WHERE number = $number1 OR number = $number2 ORDER BY id DESC LIMIT $start, $limit";
$result=mysqli_query($mysqli,$quotes_qry);
?>
<div class="row">
<div class="col-xs-12">
<div class="card mrg_bottom">
<div class="page_title_block">
<div class="col-md-5 col-xs-12">
<div class="page_title">Number Details</div>
</div>
</div>
<div class="clearfix"></div>
<div class="row mrg-top">
<div class="col-md-12">
<div class="col-md-12 col-sm-12">
<?php if(isset($_SESSION['msg'])){?>
<div class="alert alert-success alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<?php echo $client_lang[$_SESSION['msg']] ; ?></a> </div>
<?php unset($_SESSION['msg']);}?>
</div>
</div>
</div>
<div class="col-md-12 mrg-top">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#SR</th>
<th>Number</th>
<th>Online Time</th>
<th>Offline Time</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
while($row=mysqli_fetch_array($result))
{
$number = $row['number'];
$start_time = "<span style='color:green;'>".$row['start_time']."</span>";
$end_time = "<span style='color:red'>".$row['end_time']."</span>";
$tmp = strtotime($row['end_time']) - strtotime($row['start_time']);
$duration = floor($tmp/3600) . ":" . floor($tmp/60) . ":" . ($tmp%60);
?>
<tr>
<td><?php echo ++$sr+$start;?></td>
<td><?php echo $number ?></td>
<td><?php echo nl2br($start_time);?></td>
<td><?php echo nl2br($end_time);?></td>
<td><?php echo nl2br ($duration); ?></td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
</div>
<div class="col-md-12 col-xs-12">
<div class="pagination_item_block">
<nav>
<?php if(!isset($_POST["quotes_search"])){ include("pagination.php");}?>
</nav>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php include("includes/footer.php");?>
My pagination.php is like this
<?php
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<ul class='pagination'>";
// Previous
if ($page > 1){
$paginate.= "<li><a href='$targetpage?page=$prev' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
}else{
$paginate.= "<li><span aria-hidden='true'>«</span></li>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
$paginate.= "<li>...</li>";
$paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
$paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
$paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
$paginate.= "<li>...</li>";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
$paginate.= "<li>...</li>";
$paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
$paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";
}
// End only hide early pages
else
{
$paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
$paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
$paginate.= "<li>...</li>";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<li><a href='$targetpage?page=$next' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}else{
$paginate.= "<li><span aria-hidden='true'>»</span></li>";
}
$paginate.= "</ul>";
}
// pagination
echo $paginate;
?>
Let me know if someone can look and help me for get out of it.
Thanks
The 2 "notices" mean that the php page was called having omitted the "page" parameter in the query string (for this reason the $_GET['page'] causes an "undefined index" message). The last message means that the "mysqli_query($mysqli,$query)" has returned false, and you cannot use that result as argument to the "mysqli_fetch_array" (you should always check for errors when using DB functions). The query went wrong because $number1 and $number2 were empty because of the previous error.

PHP: paging with Ajax not working correctly

I'm fetching records from a database and displaying them in gruops using php and ajax.
The script below (found somewhere) is almost working, but it seems that when you are on the first page and clicking "Next" you are taken to the LAST page.
Only when clicking the numbers individually, the paging is correct.
I can provide a test link in a PM if needed.
index.php:
<script>
$(document).ready(function() {
$("#results" ).load( "pages.php"); //load initial records
//executes code below when user click on pagination links
$("#results").on( "click", ".pagination a", function (e){
e.preventDefault();
var page = $(this).attr("data-page"); //get page number from link
$("#results").load("pages.php",{"page":page}, function(){ //get content from PHP page
});
});
});
</script>
<div id="results"></div>
pages.php:
<?php
// continue only if $_POST is set and it is a Ajax request
if (isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
include (realpath(__DIR__ . '/../db.php'));
// Get page number from Ajax POST
if (isset($_POST["page"])) {
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
if (!is_numeric($page_number)) {
die('Invalid page number!');
}
}
else {
$page_number = 1; //if there's no page number, set it to 1
}
// get total number of records from database for pagination
$sql = "SELECT * FROM " . $DBtable . " ORDER BY dates DESC";
$rs = $conn->query($sql);
if ($rs === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
}
else {
$get_total_rows = $rs->fetch_row(); //hold total records in variable
}
// break records into pages
$total_pages = ceil($get_total_rows[0] / $item_per_page);
// get starting position to fetch the records
$page_position = (($page_number - 1) * $item_per_page);
// Limit our results within a specified range.
$rs = $conn->prepare("SELECT id, title, description, dates, clicks FROM " . $DBtable . " ORDER BY dates DESC LIMIT $page_position, $item_per_page");
$rs->execute(); //Execute prepared Query
$rs->bind_result($id, $title, $description, $dates, $clicks); //bind variables to prepared statement
echo '<ul class="contents">';
while($rs->fetch()){ //fetch values
echo '<li>';
echo $id. '. <strong>' .$name.'</strong> — '.$description;
echo '</li>';
}
echo '</ul>';
echo '<div align="center">';
/* We call the pagination function here to generate Pagination link for us. */
echo paginate_function($item_per_page, $page_number, $get_total_rows[0], $total_pages);
echo '</div>';
exit;
}
// ############### pagination function #########################################
function paginate_function($item_per_page, $current_page, $total_records, $total_pages) {
$pagination = '';
if ($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages) { //verify total pages and current page number
$pagination.= '<ul class="pagination">';
$right_links = $current_page + 3;
$previous = $current_page - 3; //previous link
$next = $current_page + 1; //next link
$first_link = true; //boolean var to decide our first link
if ($current_page > 1) {
$previous_link = ($previous == 0) ? 1 : $previous;
$pagination.= '<li class="class="page-item" first"><a class="page-link" href="#" data-page="1" title="First">«</a></li>'; //first link
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $previous_link . '" title="Prev"><</a></li>'; //previous link
for ($i = ($current_page - 2); $i < $current_page; $i++) { //Create left-hand side links
if ($i > 0) {
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $i . '" title="Page' . $i . '">' . $i . '</a></li>';
}
}
//set first link to false
$first_link = false;
}
if ($first_link) { //if current active page is first link
$pagination.= '<li class="page-item active first"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
elseif ($current_page == $total_pages) { //if it's the last active link
$pagination.= '<li class="page-item active last"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
else { //regular current link
$pagination.= '<li class="page-item active"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
for ($i = $current_page + 1; $i < $right_links; $i++) { //create right-hand side links
if ($i <= $total_pages) {
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $i . '" title="Page ' . $i . '">' . $i . '</a></li>';
}
}
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $i;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}
$pagination.= '</ul>';
}
//return pagination links
return $pagination;
}
// Free memory
$rs->free();
// Close connection
$conn->close();
?>
This is the current parsed html of the pager:
<ul class="pagination">
<li class="page-item active first"><a class="page-link" href="#">1<span class="sr-only">(current)</span></a></li>
<li class="page-item"><a class="page-link" href="#" data-page="2" title="Page 2">2</a></li>
<li class="page-item"><a class="page-link" href="#" data-page="3" title="Page 3">3</a></li>
<li class="page-item"><a class="page-link" href="#" data-page="4" title="Next">></a></li>
<li class="page-item last"><a class="page-link" href="#" data-page="4" title="Last">»</a></li>
</ul>
just try like below in your paginate_function()
$next_link = ($i > $total_pages) ? $total_pages : $i;
to
$next_link = (($current_page + 1) > $total_pages) ? $total_pages : $current_page + 1;
Please make changes in paginate_function function as below
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $i;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}
Change this to
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $next;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}

New records added in mysqladmin is not displayed in php page

I have 300 records in a table of mysqladmin, and i am displaying the records in a php page,it perfectly works. but when i add new records that is record no. 301,302 and so on, it add the records in table but not getting displayed in php page. i using simple query in php page,I will just specify the query i used in php page.
$sql = " SELECT * FROM td_table ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
QUESTION UPDATED
<?php
$uq=mysql_query("select user_fullname from td_user where user_id='".$_SESSION['use_i']."'");
$u_row=mysql_fetch_object($uq);
// How many adjacent pages should be shown on each side?
$adjacents = 3;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$condition='1 = 1';
$q='';
if(isset($_REQUEST['cname']))
{
$q.='&cname='.$_REQUEST['cname'];
$condition.=" AND Vehicle_description like '%".$_REQUEST['cname']."%'";
}
if(isset($_REQUEST['modelno']))
{
$q.='&modelno='.$_REQUEST['modelno'];
$condition.=" AND Model like '%".$_REQUEST['modelno']."%'";
}
if(isset($_REQUEST['modelno']))
{
$q.='&year='.$_REQUEST['year'];
$condition.=" AND Year like '%".$_REQUEST['year']."%'";
}
$query = "SELECT COUNT(*) as num FROM td_table where $condition";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
/* Setup vars for query. */
$targetpage = "automobile_list.php"; //your file name (the name of this file)
$limit = 1; //how many items to show per page
$page = #$_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
/* $vins = "SELECT SUBSTRING(VIN, 1, 10) FROM td_table"; */
$sql = " SELECT * FROM td_table where $condition LIMIT $start, $limit ";
$result = mysql_query($sql);
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "< previous";
else
$pagination.= "<span class=\"disabled\">< previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "$counter";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "$counter";
}
$pagination.= "...";
$pagination.= "$lpm1";
$pagination.= "$lastpage";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "1";
$pagination.= "2";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "$counter";
}
$pagination.= "...";
$pagination.= "$lpm1";
$pagination.= "$lastpage";
}
//close to end; only hide early pages
else
{
$pagination.= "1";
$pagination.= "2";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "$counter";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "next >";
else
$pagination.= "<span class=\"disabled\">next ></span>";
$pagination.= "</div>\n";
}
?>
<!--/menu_section-->
<div class="paginaton">
<?php echo $pagination;?>
<!--<ul class="paginate clear">
<li>Prev</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>Next</li>
</ul>-->
</div><!--/paginaton-->
<div class="container">
<div class="common" style="padding-top: 0px;">
<?php
if(mysql_num_rows($result)>0)
{
while($row = mysql_fetch_array($result))
{?>
<div class="automobilelist">
<div class="auto_list">
<div class="testonepre" style="width:auto;" align="center">
<p class="list_head">Vehicle Description</p>
</div>
<div class="clear"></div>
<div class="testone">
<div class="list_text">
<span class="list_name"> Vehicle Description : </span><span class="list_desc"> <?php echo $row['Vehicle_description'];?></span>
</div>
<div class="list_text">
<span class="list_name">Year : </span><span class="list_desc"> <?php echo $row['Year'];?></span>
</div>
<div class="list_text">
<span class="list_name">Make : </span><span class="list_desc"> <?php echo $row['Make'];?></span>
</div>
<div class="list_text">
<span class="list_name">Model : </span><span class="list_desc"> <?php echo $row['Model'];?></span>
</div>
<div class="list_text">
<span class="list_name">Body Style : </span><span class="list_desc"> <?php echo $row['Body_Style'];?></span>
</div>
<div class="list_text">
<span class="list_name">Trim : </span><span class="list_desc"> <?php echo $row['Trim_Level'];?></span>
</div>
<div class="list_text">
<span class="list_name">Odometer : </span><span class="list_desc"><?php echo $row['Odometer'];?></span>
LIKE this it will dislplay many rows

Three column layout keeps looping with the same data

I have some PHP code which pulls data from the database and places it in to a three column layout like below:
Column one column two column three
Now this works, the only problem is, the while loop keeps going and it repeats the same data over and over again. If i only have one entry in the database then this is repeated three times and i end up with one column of data. shown here :
http://www.gulfwarmemorial.co.uk/events.php
If I have three entries then the rows get repeated. I have tried various things but nothing seems to work.
$tableName="fundraising_event";
$targetpage = "events.php";
$limit = 6;
$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT *, SUBSTRING(event_details,1,200) AS event_details_short FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
?>
<?php while($row = mysql_fetch_array($result)){?>
<div class="row-fluid">
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
</div>
<?php } ?>
The issue is that, in turn, you need to fill one event column with the current event from the database. So you need a 'selector for the next row to fill in the next event column.
You have a choice of filling each column in turn, Which is slightly more complex. Or filling the columns horizontally. Which this code attempts to do.
This is all untested code but hopefully shows the logic needed.
<?php $columnSelector = 0; // define a column selector ?>
<?php while($row = mysql_fetch_array($result)) :?>
<?php if ($columnSelector == 0): ?>
<div class="row-fluid">
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<?php elseif ($columnSelector == 1): ?>
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<?php elseif ($columnSelector == 2): ?>
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<?php endif; ?>
</div>
<?php $columnSelector++; // advance to the next row?>
<?php $columnSelector %= 3; // reset to zero every third event?>
<?php endwhile; ?>

Categories