richo89 Posted March 10, 2009 Share Posted March 10, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/ Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 print "<a href='$page_name?start=$next&column_name=$column_name&searchterm=$searchtm'><font face='Verdana' size='2'>NEXT</font></a>"; You forgot to put the search term back in the url. Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781142 Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 you beat me to it premiso. You just need to make sure that you pass the search query back through the url richo. I dont think you need it in a session though. Not sure why you are putting the searches in the session. Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781143 Share on other sites More sharing options...
richo89 Posted March 10, 2009 Author Share Posted March 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781211 Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 Can you post your new code? More than likely it isnt on that line the error is. Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781221 Share on other sites More sharing options...
sasa Posted March 10, 2009 Share Posted March 10, 2009 print "<a href='$page_name?start=$next&column_name=$column_name&searchterm=$searchtm&searchtype=$searchty'><font face='Verdana' size='2'>NEXT</font></a>"; add searchtype too Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781243 Share on other sites More sharing options...
richo89 Posted March 10, 2009 Author Share Posted March 10, 2009 Thank you every so much Sasa. The above code has worked perfectly and it now remembers the query. Thank you once again I am very grateful. Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781252 Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 Sasa can you repost the code you gave to him. I would like to see what you posted but all I see is the horizontal scroll bar lol, Darn IE7 lol Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781281 Share on other sites More sharing options...
sasa Posted March 10, 2009 Share Posted March 10, 2009 print "<a href='$page_name?start=$next&column_name=$column_name&searchterm=$searchtm&searchtype=$searchty'><font face='Verdana' size='2'>NEXT</font></a>"; add searchtype too Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781296 Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 oh lol. i got ya now. I thought this was a new method or something that i hadnt heard of lol. Quote Link to comment https://forums.phpfreaks.com/topic/148763-solved-paging-forgets-query/#findComment-781322 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.