Member Avatar for mak96
<?php //get the variables $var = $_POST['query'] ; $trimmed = trim($var); $table = $_POST['table']; //make connection $conn = mysql_connect('localhost','root','123') or die("Error:".mysql_error()); mysql_select_db('sutilities',$conn) or die("Error:".mysql_error()); //delete temp search table $del_table = "drop table search"; #mysql_query($del_table) or die ("Error:".mysql_error()); // Create a MySQL table in the selected database #mysql_query("CREATE TABLE search( #id INT NOT NULL AUTO_INCREMENT, ##PRIMARY KEY(id), # table_queried VARCHAR(30), # trimmed varchar(200))") # or die(mysql_error()); mysql_query("insert into search (table_queried, trimmed) values ('$table','$trimmed')") or die ("Error:".mysql_error()); // find out how many rows are in the table $query = "SELECT COUNT(*) FROM $table"; $result = mysql_query($query, $conn) or die("Error:".mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 10; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db if ($table == "canteen"){ $sql="select canteen.price, canteen.url, canteen.item, canteen.shop from canteen where canteen.item like '%$trimmed%'". "or canteen.shop like '%$trimmed%' LIMIT $offset, $rowsperpage"; } if ($table == "timetables"){ $sql="select timetables.grade, timetables. url, timetables.section, timetables.idtimetables from timetables where timetables.grade like '%$trimmed%'". "or timetables.section like '%$trimmed%'". "or timetables.idtimetables like '%$trimmed%' LIMIT $offset, $rowsperpage"; } if ($table == "tc_sell"){ $sql="select tc_sell.name, tc_sell.age, tc_sell.grade, tc_sell.hp_no, tc_sell.email, tc_sell.price from tc_sell where tc_sell.name like '%$trimmed%'". "or tc_sell.age like '%$trimmed%'". "or tc_sell.grade like '%$trimmed%'". "or tc_sell.hp_no like '%$trimmed%'". "or tc_sell.email like '%$trimmed%'". "or tc_sell.item like '%$trimmed%'". "or tc_sell.price like '%$trimmed%' LIMIT $offset, $rowsperpage"; } if ($table == "tc_buy"){ $sql="select tc_buy.name, tc_buy.age, tc_buy.grade, tc_buy.hp_no, tc_buy.email, tc_buy.price from tc_buy where tc_buy.name like '%$trimmed%'". "or tc_buy.age like '%$trimmed%'". "or tc_buy.grade like '%$trimmed%'". "or tc_buy.hp_no like '%$trimmed%'". "or tc_buy.email like '%$trimmed%'". "or tc_buy.item like '%$trimmed%'". "or tc_buy.price like '%$trimmed%' LIMIT $offset, $rowsperpage"; } $result = mysql_query($sql, $conn) or die("Error:".mysql_error()); $count=0; while ($list = mysql_fetch_assoc($result)) { if ($table == "canteen"){ $count=$count+1; echo "<table><tr><td>".$count."</td><td>".$list['item']."</td><td>".$list['price']."</td><td>".$list['shop']."<br></td></tr></table>"; } } $range = 3; if ($currentpage > 1) { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $totalpages)) { if ($x == $currentpage) { echo " [<b>$x</b>] "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } } } if ($currentpage != $totalpages) { $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } ?>

when i click the 'next' button on the results page, i get the following error:
PHP Notice: Undefined index: query in C:\inetpub\wwwroot\search(i).php on line 67
PHP Notice: Undefined index: table in C:\inetpub\wwwroot\search(i).php on line 69

could u pls point out whr i am going wrong?
how do i retain the form variables?? any suggestions? any other idea??


thx a lot in advance!!
Regards,
Makrand

Member Avatar for sDJh

Hi Makrand,

these messages aren't errors but warnings to force you to improve your coding style. If you don't fancy that (like I do) you can set your PHP.INI variable "error_reporting" to "E_ALL^E_NOTICE". This stops php to reports these warnings (http://de.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting).
If you don't have access to the PHP.INI have a look here: http://de.php.net/manual/en/function.error-reporting.php or here http://php.net/manual/en/function.ini-set.php. For me both works, but sometimes one of these may causes some fuzz.

Hope that helps - Simon

Member Avatar for mak96

Hi Makrand,

these messages aren't errors but warnings to force you to improve your coding style. If you don't fancy that (like I do) you can set your PHP.INI variable "error_reporting" to "E_ALL^E_NOTICE". This stops php to reports these warnings (http://de.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting).
If you don't have access to the PHP.INI have a look here: http://de.php.net/manual/en/function.error-reporting.php or here http://php.net/manual/en/function.ini-set.php. For me both works, but sometimes one of these may causes some fuzz.

Hope that helps - Simon

Thx Simon for the suggestion! I'll try that and inform you!:)

Member Avatar for juniorbillgates

kaisar ur site is very good for the beginners

Member Avatar for honeymehta

hi cn any one help me to develop the code for pagination according to search result

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.