| | |
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: May 2008
Posts: 90
Reputation:
Solved Threads: 0
I have problem with page navigation.My problem is that am getting all the records in one page. $rowsPerPage = 10; I have 30 records, all are showed in one page...not the 10 records.Here is my code.
PHP Syntax (Toggle Plain Text)
<?php include ('conn.php'); // how many rows to show per page $rowsPerPage = 10; // by default we show first page $pageNum = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; } // counting the offset $offset = ($pageNum - 1) * $rowsPerPage; $q = "show tables"; $r = mysql_query($q); //$searchresult = array(); $var = $_POST['keyword'] ; $trimmed = trim($var); // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } while($data = mysql_fetch_array($r)) { $table = $data[0]; // get the tablename $query = "select * from ".$table." where dispname like '%".$trimmed."%'"; $result = mysql_query($query); while($data = mysql_fetch_array($result)) { // $searchresult[] = $rows['name']; //$var =$rows['dispname']; echo ' <tr> <td> <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>'; echo'</tr>'; } } //$query = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage"; //$result = mysql_query($query) or die('Error, query failed'); while(list($val) = mysql_fetch_array($result)) { echo "$val <br>"; } echo '<br>'; $query = "select * from ".$table." where dispname like '%".$trimmed."%'"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows']; $maxPage = ceil($numrows/$rowsPerPage); $self = $_SERVER['PHP_SELF']; if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self?page=$page\">[Prev]</a> "; $first = " <a href=\"$self?page=1\">[First Page]</a> "; } else { $prev = ' [Prev] '; // we're on page one, don't enable 'previous' link $first = ' [First Page] '; // nor 'first page' link } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?page=$page\">[Next]</a> "; $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> "; } else { $next = ' [Next] '; // we're on the last page, don't enable 'next' link $last = ' [Last Page] '; // nor 'last page' link } echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last; ?>
Suhasini
•
•
Join Date: May 2008
Posts: 90
Reputation:
Solved Threads: 0
I changed my code.Here is my new code:
Error occured is :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 10' at line 1.
This error is some where around this line:if(!$result){die(mysql_error());}
What is the solution?
PHP Syntax (Toggle Plain Text)
<?php include ('conn.php'); // how many rows to show per page $rowsPerPage = 10; // by default we show first page $pageNum = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; } // counting the offset $offset = ($pageNum - 1) * $rowsPerPage; //$searchresult = array(); $var = $_REQUEST['keyword'] ; $trimmed = trim($var); // rows to return // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //$table ="results"; $query ="select count(*) from medlist where dispname like '%".$trimmed."%'"; $rsid = mysql_query($query); if(!$rsid){die(mysql_error());} $resArr = mysql_fetch_row($rsid); $numrows = $resArr[0]; //$query ="select * from medlist where dispname like '%".$trimmed."%' limit $strpos, $rowsPerPage"; $query = "select * from medlist where dispname like '%".$trimmed."%' LIMIT ". $start .", ". $rowsPerPage ; $result = mysql_query($query); if(!$result){die(mysql_error());} //$query = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage"; //$result = mysql_query($query) or die('Error, query failed'); while($val = mysql_fetch_row($result)) { print_r($val); } echo '<br>'; $maxPage = ceil($numrows/$rowsPerPage); $self = $_SERVER['PHP_SELF']; if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self?page=$page&keyword=$var\">[Prev]</a> "; $first = " <a href=\"$self?page=1&keyword=$var\">[First Page]</a> "; } else { $prev = ' [Prev] '; // we're on page one, don't enable 'previous' link $first = ' [First Page] '; // nor 'first page' link } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?page=$page&keyword=$var\">[Next]</a> "; $last = " <a href=\"$self?page=$maxPage&keyword=$var\">[Last Page]</a> "; } else { $next = ' [Next] '; // we're on the last page, don't enable 'next' link $last = ' [Last Page] '; // nor 'last page' link } echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last; ?>
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 10' at line 1.
This error is some where around this line:if(!$result){die(mysql_error());}
What is the solution?
Last edited by Suhacini; May 27th, 2008 at 2:54 am.
Suhasini
•
•
Join Date: May 2008
Posts: 90
Reputation:
Solved Threads: 0
From the above code I am not getting any error but,the link to next page is not enabled.Even though $limit=10; & $rowsPerPage =10; both are declared as '10'.If I have 15 records all of them are displayed in the same page,but not in the 2 different pages.
Anyone to help regarding this??
Anyone to help regarding this??
Last edited by Suhacini; May 27th, 2008 at 10:00 am.
Suhasini
•
•
Join Date: May 2008
Posts: 90
Reputation:
Solved Threads: 0
Changed the code again.
In Page1 data displayed is from A1 to A10. Now the problem is that if I go to Page 2 data displayed must be from A11 to A12 but in this case it displaying the data where Id=11 in my db.
What is wrong with this code?
PHP Syntax (Toggle Plain Text)
<?php include ('conn.php'); // by default we show first page $page = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $page = $_GET['page']; } // counting the offset $offset = ($page - 1) * $rowsPerPage; //$searchresult = array(); $var = $_POST['keyword'] ; $trimmed = trim($var); // rows to return $limit=10; $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; $query = "select * from medlist where dispname like '%".$trimmed."%' limit $offset,$rowsPerPage"; $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>"; ?>
What is wrong with this code?
Suhasini
•
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 1
well, you are doing this
before you do this:
i think it shoudl be other way around...
PHP Syntax (Toggle Plain Text)
// counting the offset $offset = ($page - 1) * $rowsPerPage;
before you do this:
PHP Syntax (Toggle Plain Text)
// how many rows to show per page $rowsPerPage =10;
i think it shoudl be other way around...
Last edited by Jorann; Jun 3rd, 2008 at 9:54 am.
•
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 1
This is what i once used in my code:
PHP Syntax (Toggle Plain Text)
$showfromthisid = ceil($pagenumber*10) -10; $maxshownmessages = 10; $result = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT $showfromthisid,$maxshownmessages") or die(mysql_error());
•
•
Join Date: Apr 2008
Posts: 48
Reputation:
Solved Threads: 3
•
•
•
•
Changed the code again.
In Page1 data displayed is from A1 to A10. Now the problem is that if I go to Page 2 data displayed must be from A11 to A12 but in this case it displaying the data where Id=11 in my db.PHP Syntax (Toggle Plain Text)
<?php include ('conn.php'); // by default we show first page $page = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $page = $_GET['page']; } // counting the offset $offset = ($page - 1) * $rowsPerPage; //$searchresult = array(); $var = $_POST['keyword'] ; $trimmed = trim($var); // rows to return $limit=10; $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; $query = "select * from medlist where dispname like '%".$trimmed."%' limit $offset,$rowsPerPage"; $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>"; ?>
What is wrong with this code?
Sam
•
•
Join Date: May 2008
Posts: 90
Reputation:
Solved Threads: 0
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?
Suhasini
![]() |
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 |
.htaccess apache api array autocomplete beginner binary body broken cakephp class cms code convert cron curl database dataentry date date/time display duplicates dynamic ebooks email emptydisplayvalue error execute explodefunction file firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include ip javasciptvalidation javascript joomla keywords limit link login mail matching mediawiki menu methods multiple mycodeisbad mysql network number object oop paypal pdf php phpincludeissue query random recursive redirect remote script search securephp server sessions shot source sp space speed sql subdomain subscription system table tag tutorial tutorials upload url validator variable vbulletin video web white youtube





