Jump to content

newbie needs desperate help on multy page php/database


mark bowen

Recommended Posts

???

Hi all.  could you help please?  i have come across some code from a snipet somewhere and am trying to adapt my results into the coding.

have basically got the display but when i click on e.g. 'show 20 results per page' it still only shows the default 10 results.  also when i click on next page or 'page 3' it still only shows the first 10 results and i cant see what i'm doing wrong....

[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php

include ("**********");

if (!($limit)){
$limit = 10;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$query='Personal Care';
$numresults = mysql_query("SELECT * FROM stock where category2='". $query ."'"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
echo("No results found matching your query - $query"); // bah, modify the "Not Found" error for your needs.
exit();}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.

else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.
?>

<head>
<html><title>Search Results for <?=$query?></title>
<style type="text/css">
<!--
.style1 {
color: #FF0000;
font-weight: bold;
}
-->
</style>
</head>
<body>
<center><h2>Search Results for <?=$query?></h2></center>
<table width="100%" border="0">
<tr>
  <td width="50%" align="left">
Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
  </td>
  <td width="50%" align="right">
Page <b><?=$current?></b> of <b><?=$total?></b>
  </td>
</tr>
<tr>
  <td colspan="2" align="right">&nbsp;

  </td>
</tr>
<tr>
  <td colspan="2" align="right">
Results per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a>
  </td>
</tr>
</table>
<?
//Go back into PHP mode.

// Now we can display results.
$results = mysql_query("select * from stock where category2='". $query ."' order by Prod_IntPartNo ASC LIMIT $page, $limit");
while ($row = mysql_fetch_array($results)){
?>
<table width="477" height="167" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="2" bgcolor="#FECD18"><strong><?=$row["pagename"]?></strong></td>
      </tr>
           <tr>
        <td width="180" rowspan="5"><img src="images/Stock/PRODUCT_<?=$row["Prod_IntPartNo"]?>_THUMB.jpg"></td>
        <td width="203"></td>
      </tr>
      <tr>
        <td height="27" class="style7 main"><strong>Product Code: <?=$row["Prod_IntPartNo"]?> </strong></td>
      </tr>
      <tr>
        <td height="29" class="style7 main"><strong>Brand:</strong></td>
      </tr>
      <tr>
        <td height="35"><span class="style6 style1">RRP: £<?=$row["rrp"]?></span></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
</table>
<?
}
?>
<p align="center">
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b> \n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>\n");}
?>
</p>
</body>
</html>[/code]
Link to comment
Share on other sites

It's a little hard to read, and a little messy.  It's sometimes hard taking third party scripts and adapting them to your purposes.

All I can do is offer you an example of how I handle pagination right now (working on a class for it when I get better with OOP), but for now, I replicate the same code I have been using, and reform it.

I went back through my code, and just commented it better for you, this is what I use for every situation with pagination, adn it's always been fairly easy to integrate.
[code] <?php
// this is required for the pagination to work, it does a test for the previous and next links,
// The mysql escape string is for safety, you can take it out if you choose.
if ($_GET['rownumberprev']) {
$rownumber = mysql_real_escape_string($_GET['rownumberprev']);
}elseif ($_GET['rownumbernext']) {
$rownumber = mysql_real_escape_string($_GET['rownumbernext']);
}else { // if it wasn't found it set's the current rownumber to 0
$rownumber = 0;
}
// You have to set a limit for this to work
$limit = 20;
// run query
// This is the key to making this work.  BEFORE running your actual query with limitation you ahve to run a
// full query, so you can get the total number of rows completely.
$selectfull = "SELECT * FROM eventreport;"; // run full query
$queryfull = mysql_query($selectfull);
$total_rows = mysql_num_rows($queryfull); // this is required to get total number of rows
// run original query, with limitations
$select = "SELECT * FROM eventreport LIMIT $rownumber, $limit;"; // rownumber and limit required
$query = mysql_query($select); // run query
echo "<hr />";
echo "<br />";
while ($row = mysql_fetch_array($query)) { // do fetch call (so you can output data)
echo $row['name'] . " ";
echo "<a href=\"viewalleventinfo.php?id={$row[id]}\" title=\"View All Information\">View</a>";
echo " ";
echo "<a href=\"edit.php?id={$row[id]}\" title=\"Edit Event\">
Edit</a>";
echo " ";
echo "<a href=\"deleteevent.php?id={$row[id]}\" title=\"Delete\">Delete</a>";
echo "<hr />";
}// end running query
// This is the critical part, this is what makes the actual pagination links show up.
if ($rownumber != 0) {
$rownumberprev = $rownumber - $limit;
// adjust following links to point back to the current page with server self, or the page or something
echo "<a href=\"viewevents.php?rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - $limit)) {
$rownumbernext = $rownumber + $limit;
// same here change the link
echo "<a href=\"viewevents.php?rownumbernext={$rownumbernext}\">Next Page</a>";
}

?>[/code]


[quote]This looks as though it could be a problem with a setting called 'Register Globals'.

Regards
Huggie[/quote]
The following (or something similar) will work without register globals being on. (Good point huggie, I didn't notice that, still don't)
Link to comment
Share on other sites

Give this a try...

[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<?php
include ("**********");
$limit = isset($_GET['limit']) ? $_GET['limit'] : '10'; // Default results per-page.
$page = isset($_GET['page']) ? $_GET['page'] : '0' // Default page value.

$query='Personal Care';
$numresults = mysql_query("SELECT * FROM stock where category2='". $query ."'"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
echo("No results found matching your query - $query"); // bah, modify the "Not Found" error for your needs.
exit();}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.

else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.
?>

<head>
<html><title>Search Results for <?=$query?></title>
<style type="text/css">
<!--
.style1 {
color: #FF0000;
font-weight: bold;
}
-->
</style>
</head>
<body>
<center><h2>Search Results for <?=$query?></h2></center>
<table width="100%" border="0">
<tr>
  <td width="50%" align="left">
Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
  </td>
  <td width="50%" align="right">
Page <b><?=$current?></b> of <b><?=$total?></b>
  </td>
</tr>
<tr>
  <td colspan="2" align="right">&nbsp;

  </td>
</tr>
<tr>
  <td colspan="2" align="right">
Results per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a>
  </td>
</tr>
</table>
<?
//Go back into PHP mode.

// Now we can display results.
$results = mysql_query("select * from stock where category2='". $query ."' order by Prod_IntPartNo ASC LIMIT $page, $limit");
while ($row = mysql_fetch_array($results)){
?>
<table width="477" height="167" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="2" bgcolor="#FECD18"><strong><?=$row["pagename"]?></strong></td>
      </tr>
          <tr>
        <td width="180" rowspan="5"><img src="images/Stock/PRODUCT_<?=$row["Prod_IntPartNo"]?>_THUMB.jpg"></td>
        <td width="203"></td>
      </tr>
      <tr>
        <td height="27" class="style7 main"><strong>Product Code: <?=$row["Prod_IntPartNo"]?> </strong></td>
      </tr>
      <tr>
        <td height="29" class="style7 main"><strong>Brand:</strong></td>
      </tr>
      <tr>
        <td height="35"><span class="style6 style1">RRP: £<?=$row["rrp"]?></span></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
</table>
<?
}
?>
<p align="center">
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b> \n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>\n");}
?>
</p>
</body>
</html>[/code]

Regards
Huggie
Link to comment
Share on other sites

[quote]Good point huggie, I didn't notice that, still don't[/quote]

A quick look at the query string to navigate between pages, shows that the variables being passed are $limit and $page.  Yet a search for $_GET['limit'] or $_GET['page'] in the code yields no results, implying that the original coder is relying on 'Register Globals' being switched on.

Then look at what the user says...

[quote]when i click on e.g. 'show 20 results per page' it still only shows the default 10 results.  also when i click on next page or 'page 3' it still only shows the first 10 results[/quote]

This gives the game away totally, despite having the value in the string, the query isn't picking it up, it's applying the default every time meaning it's not getting to the script.

Regards
Huggie
Link to comment
Share on other sites

[quote author=HuggieBear link=topic=123670.msg511398#msg511398 date=1169566457]
This looks as though it could be a problem with a setting called 'Register Globals'.

Regards
Huggie
[/quote]

;D Thank you, Thank you, Thank you, Thank you, Thank you, Thank you, Thank you, Thank you, Thank you, Thank you,  ;D

and thanks to all who offered their advise it is greatly appreciated.  after all my head scratching it was the register globals.....cant believe i missed it but couldn't see the woods for the trees......

thanks once again for the help.  no doubt i will be back when i start to do a bit more with it.  i know its dangerous using other peoples sample code but hopefully when i get used to it more i'll be able to be as good as some of you guys on here.....hopefully..... 8)

mark
Link to comment
Share on other sites

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.