| | |
Problem with page navigation
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 48
Reputation:
Solved Threads: 3
•
•
•
•
Actually am using this for search.In my db I am storing few words A-Z but not in alphbetic order.so when i wanted to search 'A',then all data from my db related to 'A' must display.Here am able to display A1 to A10 in 1st page.In 2nd page it should start with A11 but it is displaying the data from my db whose Id=11.Am I clear to you now?
Let's say that $trimmed is your search for A, it should return A1 - A10 then A11-A20 (or w/e) then if where id 11 dispname is TALC 'N' POWDER - it'll pick it up because it has an A in it. the %A% means anything before the A and anything after the A so TALC would get in but ZINC wouldn't. So if you just want to narrow your search to A--- on disp name.
Use this:
php Syntax (Toggle Plain Text)
<?php include ('conn.php'); // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $page = $_GET['page']; } else { // if $_GET['page'] is not defined then $page == 1 $page = 1; } // counting the offset //$searchresult = array(); $var = $_POST['keyword'] ; $trimmed = mysql_real_escape_string(trim($var)); // rows to return $str = "SELECT * FROM `medlist` WHERE `dispname` LIKE '$trimmed%'"; echo $str.'<br>'; $res= mysql_query($str); if(!$res){die(mysql_error());} $num_rows= mysql_num_rows($res); echo $num_rows.'<br>'; // how many rows to show per page $rowsPerPage =10; $maxPage = ceil($num_rows/$rowsPerPage); //$page=(isset($_GET['page']))?$_GET['page']:1; //$offset=($page-1)*$rowsPerPage; if ($page < 1) { $page = 1; } elseif ($page > $maxPage) { $page = $maxPage; } $max = 'LIMIT ' . ($page - 1) * $rowsPerPage . ',' . $rowsPerPage; $query = "SELECT * FROM `medlist` WHERE `dispname` LIKE '$trimmed%' $max"; $result = mysql_query($query); //<a href=page.php?search=$string&offset=$offset> if(!$result){die(mysql_error());} while($data = mysql_fetch_array($result)) { echo ' <tr> <td colspan="3" > <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>'; echo'</tr>'; } echo "<tr><td align='left' colspan='2'>Result Pages:"; for($i=1;$i<=$maxPage;$i++) { echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i>".$i." | "; } echo "</td> <td>Showing page <strong>".$page."</strong> of <strong>".$maxPage."</strong> pages </td> </tr>"; ?>
Does that help?
so what exactly was wrong with the code i typed up for you.
the reason you getting the problem like i told you earlier is that you are not sending the search term along with each link in the script. the $_POST['keyword'] which contains the term on the first page is reset once you click a link. making it not show the same results from before with the different limit.
here is my code i made for you:
(others, please check for errors. i haven't found any, but i like to miss little things.)
the reason you getting the problem like i told you earlier is that you are not sending the search term along with each link in the script. the $_POST['keyword'] which contains the term on the first page is reset once you click a link. making it not show the same results from before with the different limit.
here is my code i made for you:
(others, please check for errors. i haven't found any, but i like to miss little things.)
PHP Syntax (Toggle Plain Text)
<?php if (isset($_POST['submit'])) { $var = trim($_POST['keyword']); header('Location: pageno.php?keyword=' . $var); die(); } ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>PgNo</title> <link href="links.css" rel="stylesheet" type="text/css" /> </head> <body> <table border="1" width="100%"> <tr> <td align="center"> <form method="post" action="pagno.php"> <input type="text" size="40%" id="keyword" name="keyword" /> <input type="submit" name="submit" value="Search" /> </form> </td> </tr> <tr> <td class="imag" id="def">| <a href="home.html"> Home </a> | <a href="dc.php"> D & C </a>| <a href="st.php"> S & T</a> | <a href="medi.php"> Med</a> |<a href="hc.html"> HC</a> |<a href="news.html"> News </a>|</td> </tr> </table> <table border="1" align="center"> <tr> <th align="left"> Search Results :</th> </tr> <tr> <td align="center"> <?php //make sure you add your database connection here //Set Rows Per Page $rowsPage = 10; if (isset($_GET['keyword'])) { $word = mysql_real_escape_string($_GET['keyword']); if ($word !== '') { if (isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; } $start = ($rowsPage * $page) - $rowsPage; $sql = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'"; $query = mysql_query($sql); $total = mysql_num_rows($query); $pages = ceil($total/$rowsPage); if ($pages > 1) { $limit = ' LIMIT ' . $start . ', ' . $rowsPage; } else { $limit = ''; } $sql = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'" . $limit; $query = mysql_query($sql); echo '<table border="0" cellspacing="0" cellpadding="3">'; while ($row = mysql_fetch_assoc($query)) { echo '<tr>'; echo '<td><a href="' . $row['MedName'] . '">' . $row['dispname'] . '</a></td>'; echo '</tr>'; } echo '</table>'; echo '<center>Pages: '; $i = 1; while ($i < count($pages)) { echo '<a href="pagno.php?keyword=' . $word . '&page=' . $i . '">' . ($page == $i ? '<font color="#FF0000">' . $i . '</font>' : $i) . '</a> '; $i++; } echo '</center>'; } } ?> </td> </tr> </table> </body> </html>
Last edited by kkeith29; Jun 4th, 2008 at 2:11 am.
•
•
Join Date: May 2008
Posts: 90
Reputation:
Solved Threads: 0
Sam,In the code we have I found the problem.We have 2 queries
When I print them,In 1st page they are printing :
In 2nd page they are printing
So the problem here is in 2nd page the keyword is not there so it is displaying Id 11 from db.I changed the a href tag which is in the for loop :
I have added keyword and offset values but even this doesnt bring any change in the output.
What can be done next?
PHP Syntax (Toggle Plain Text)
$str = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%".$trimmed."%'"; $query = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%".$trimmed."%'".$max ;
PHP Syntax (Toggle Plain Text)
SELECT * FROM `medlist` WHERE `dispname` LIKE '%a%' & SELECT * FROM `medlist` WHERE `dispname` LIKE '%a%' limit 0,10
PHP Syntax (Toggle Plain Text)
SELECT * FROM `medlist` WHERE `dispname` LIKE '%%' & SELECT * FROM `medlist` WHERE `dispname` LIKE '%%' limit 10,10
PHP Syntax (Toggle Plain Text)
echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i&trimmed=$trimmed&offset=$offset>".$i." | ";
What can be done next?
Suhasini
finally i got sick of this stuff and tested it on my server.
i created the table and filled it with random values.
here is the test script url: http://www.banditssoftball.org/pagno.php
to find a result, type in a number into the search box.
i made a mistake on my last script i posted for you. i used count() for some reason, use to it i guess, when i was echoing out the pages.
i created the table and filled it with random values.
here is the test script url: http://www.banditssoftball.org/pagno.php
to find a result, type in a number into the search box.
i made a mistake on my last script i posted for you. i used count() for some reason, use to it i guess, when i was echoing out the pages.
Last edited by kkeith29; Jun 4th, 2008 at 4:07 am.
yes, the one i typed for you (with minor changes).
here it is:
here it is:
PHP Syntax (Toggle Plain Text)
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>PgNo</title> <link href="links.css" rel="stylesheet" type="text/css" /> </head> <body> <table border="1" width="100%"> <tr> <td align="center"> <form method="get" action="pagno.php"> <input type="text" size="40%" id="keyword" name="keyword" /> <input type="submit" value="Search" /> </form> </td> </tr> <tr> <td class="imag" id="def">| <a href="home.html"> Home </a> | <a href="dc.php"> D & C </a>| <a href="st.php"> S & T</a> | <a href="medi.php"> Med</a> |<a href="hc.html"> HC</a> |<a href="news.html"> News </a>|</td> </tr> </table> <table border="1" align="center"> <tr> <th align="left"> Search Results :</th> </tr> <tr> <td align="center"> <?php //Make sure you input your database connection here //Set Rows Per Page $rowsPage = 10; if (isset($_GET['keyword'])) { $word = mysql_real_escape_string($_GET['keyword']); if ($word !== '') { if (isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; } $start = ($rowsPage * $page) - $rowsPage; $sql = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'"; $query = mysql_query($sql); $total = mysql_num_rows($query); $pages = ceil($total/$rowsPage); if ($pages > 1) { $limit = ' LIMIT ' . $start . ', ' . $rowsPage; } else { $limit = ''; } $sql = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'" . $limit; $query = mysql_query($sql); echo '<table border="0" cellspacing="0" cellpadding="3">'; while ($row = mysql_fetch_assoc($query)) { echo '<tr>'; echo '<td><a href="' . $row['MedName'] . '">' . $row['dispname'] . '</a></td>'; echo '</tr>'; } echo '</table>'; echo '<center>Pages: '; $i = 1; while ($i <= $pages) { echo '<a href="pagno.php?keyword=' . $word . '&page=' . $i . '">' . ($page == $i ? '<font color="#FF0000">' . $i . '</font>' : $i) . '</a> '; $i++; } echo '</center>'; } } ?> </td> </tr> </table> </body> </html>
![]() |
Similar Threads
- Change IE "Action Canceled" page?? (Web Browsers)
- PHP Navigation help please (PHP)
- CSS position problem (HTML and CSS)
- Scrolling tables problem (HTML and CSS)
- Router is slower than adapter (Network Security)
- Parse error at last line of page's code (PHP)
- Navigation Bar that extends the length of the page (HTML and CSS)
- Problem - IE Synchronize navigation (Web Browsers)
- Mouse over buttons on .js file? Urgent-help (JavaScript / DHTML / AJAX)
Other Threads in the PHP Forum
- Previous Thread: 2 tier & 3 tier architectures in php?
- Next Thread: Serious problem with attchments-please help
| Thread Tools | Search this Thread |
ajax apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion regex remote script search server session sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validator variable video virus votedown web window.onbeforeunload=closeme; xml youtube






