Jump to content

[SOLVED] PHP Pagination


richo89

Recommended Posts

I have a cars database whereby a user selects a make/model and the results are displayed below.

 

However, my results display perfectly but when I try to click next page I just get an empty page.

 

Any ideas?

 

code:

<?PHP
    
if($_POST['submit']) //If submit is hit
{
    
session_start();

//Create variables to store data from .html file
$_SESSION['cat'] = $_POST['cat'];
$searchmake = $_SESSION['cat'];

$_SESSION['subcat'] = $_POST['subcat'];
$searchmodel = $_SESSION['subcat'];

$_SESSION['searchpricemin'] = $_POST['searchpricemin'];
$minprice = $_SESSION['searchpricemin'];

$_SESSION['searchpricemax'] = $_POST['searchpricemax'];
$maxprice = $_SESSION['searchpricemax'];

$page_name="cmSearch.php"; //  If you use this code with a different page ( or file ) name then change this 

@$column_name=$_GET['column_name']; // Read the column name from query string. 


$start=$_GET['start'];                                // To take care global variable if OFF
if(!($start > 0)) {                         // This variable is set to zero for the first page
$start = 0;
}

$eu = ($start - 0); 
$limit = 6;                                 // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 

echo "Searching for ". $_SESSION['cat'] ; //retrieve data
echo " Between £". $_SESSION['searchpricemin'] ; //retrieve data
echo " and £". $_SESSION['searchpricemax'] ; //retrieve data


/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2=" SELECT * FROM cars WHERE make LIKE '$searchmake' AND model LIKE '$searchmodel' AND price BETWEEN '$minprice' AND '$maxprice'";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM cars WHERE make LIKE '$searchmake' AND model LIKE '$searchmodel' AND price BETWEEN '$minprice' AND '$maxprice' ORDER BY price ASC";
if(isset($column_name) and strlen($column_name)>0){
$query = $query . " order by $column_name";
}
$query = $query. " limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();

//////////////// Now we will display the returned records in side the rows of the table///////// 

//Move the start table tag out of while loop
echo "<table border=0 width=100%>"; 
while($noticia = mysql_fetch_array($result)) 
{ 
if($bgcolor=='#ffffff'){$bgcolor='#ffffff';} 
else{$bgcolor='#ffffff';} 

echo "<tr >"; 
echo "<td align=left colspan=5 BGCOLOR=#F8F8F8 id='title'> <font face='Verdana' color='#A00000' size='3'> <center><b><u>Cars For Sale</center></b></u></font></td>"; 
echo "<tr >"; 
echo "<td align=left rowspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'><img src='$noticia[picture]'</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>$noticia[make] $noticia[model]</font></td>";  
echo "<tr >"; 
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>$noticia[Reg] reg - $noticia[colour] - $noticia[description] - $noticia[miles] miles</font></td>";  
echo "<tr >";  
echo "<td align=left colspan=3 bgcolor=$bgcolor id='title'> <font face='Verdana' color='red' size='3'><u>Price:£$noticia[price]</u></font></td>";

echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'><a href='blank.php?carNo=".$noticia['carNo']."'>More Info???</a></font></td>";  
echo "</tr>"; 
} 
echo "</table>";  

/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0) { 
print "<a href='$page_name?start=$back&column_name=$column_name&searchmake=$searchmake&searchpricemin=$searchpricemin&searchpricemax=$searchpricemax'><font face='Verdana' size='2'>PREV</font></a>"; 
} 
//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $nume;$i=$i+$limit){
if($i <> $eu){
echo " <a href='$page_name?start=$i&column_name=$column_name&searchmake=$searchmake&searchpricemin=$searchpricemin&searchpricemax=$searchpricemax'><font face='Verdana' size='2'>$l</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$l</font>";}        /// Current page is not displayed as link and given font color red
$l=$l+1;
}


echo "</td><td  align='right' width='30%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume) { 
print "<a href='$page_name?start=$next&column_name=$column_name&searchmake=$searchmake&searchmodel=$searchmodel&minprice=$minprice&maxprice=$maxprice'><font face='Verdana' size='2'>NEXT</font></a>";}
echo "</td></tr></table>";
}
?> 

Link to comment
https://forums.phpfreaks.com/topic/152791-solved-php-pagination/
Share on other sites

I have removed the POST Submit and also ammended the Next Page Href so it reads

///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume) { 
print "<a href='$page_name?start=$next&column_name=$column_name&cat=$searchmake&subcat=$searchmodel&searchpricemin=$minprice&searchpricemax=$maxprice'><font face='Verdana' size='2'>NEXT</font></a>";}
echo "</td></tr></table>";

 

It now works perfectly :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.