Jump to content

Recommended Posts

My website is a Cars database where a user searches from a list of vehicles.

 

I have created a pagination which when user types 'ford' for example pages my results. However when I click 2nd page it forgets the initial search query?

 

<?PHP
session_start();

//Create variables to store data from .html file
$_SESSION['searchtype'] = $_GET['searchtype'];
$searchty = $_SESSION['searchtype'];
$_SESSION['searchterm'] = $_GET['searchterm'];
$searchtm = $_SESSION['searchterm'];

require "config.php";           // All database details will be included here 

$page_name="demo_paging4.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 = 8;                                 // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 

//Variables for query!!!!
//$searchtype=$_GET['searchtype'];
//$searchterm=$_GET['searchterm'];

/////////////// 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 `$searchty` LIKE '%$searchtm%'";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////

echo "Search term is:'$searchtm'";

/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
echo "<TABLE width=50% align=center  cellpadding=0 cellspacing=0> <tr>";
echo "<td  bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$page_name?column_name=make'>make</a></font></td>";
echo "<td  bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='4'><a href='$page_name?column_name=model'>model</a></font></td>";

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM cars WHERE `$searchty` LIKE '%$searchtm%'";
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/////////
while($noticia = mysql_fetch_array($result))
{
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}

echo "<tr >";
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>$noticia[make]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='2'>$noticia[model]</font></td>"; 

echo "</tr>";
}
echo "</table>";
////////////////////////////// End of displaying the table with records ////////////////////////

/////////////// 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'><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'><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'><font face='Verdana' size='2'>NEXT</font></a>";} 
echo "</td></tr></table>";


?>
</body>

</html>

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/
Share on other sites

The line of code

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

 

Should pickup the name from the query string.

 

However my error is the following:

Unknown column '' in 'where clause'
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in B:\xampp\htdocs\assignment\demo_paging4.php on line 36
Search term is:''Unknown column '' in 'where clause'
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in B:\xampp\htdocs\assignment\demo_paging4.php on line 57

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.