Jump to content

[SOLVED] a simple pagination script


confused_aswell

Recommended Posts

<?php
$num = $over_num;
$page = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] >= 1 ? $_GET['page'] : 1;

$per_page = $num;

$count_q = mysql_query("SELECT COUNT(id) FROM home ORDER BY id DESC");
$count_r = mysql_fetch_row($count_q);
$count = $count_r[0];

if($page > ceil($count/$per_page)) $page = 1;


$limit_min = ($page-1)*$per_page;
$query = mysql_query("SELECT * FROM home ORDER BY id DESC LIMIT {$limit_min}, {$per_page}");
   
$sql = mysql_query("SELECT * FROM home ORDER BY id DESC");
$num_rows = mysql_num_rows($sql);  

$limit2 = $per_page;

$q = "SELECT id FROM home";
$r = mysql_query($q);
$num = ceil(mysql_num_rows($r) / $limit2);
if($num != 1)
{
  for($i = 1; $i <= $num; $i++)
  {
    /*if($page / $limit2 != $i)*/ echo "[<a href='?p=list&page=".$i."'>Page ".$i."</a>] ";
    //else echo "[<b>".$i."</b>] ";
  }
}
echo "<br /><br />";
  //while($row = mysql_fetch_array($sql)){
  while($row = mysql_fetch_assoc($query)) {
  
     //echo out DB info here

}

$limit2 = $per_page;

$q = "SELECT id FROM home";
$r = mysql_query($q);
$num = ceil(mysql_num_rows($r) / $limit2);
if($num != 1)
{
  for($i = 1; $i <= $num; $i++)
  {
    /*if($page / $limit2 != $i)*/ echo "[<a href='?p=list&page=".$i."'>Page ".$i."</a>] ";
    //else echo "[<b>".$i."</b>] ";
  }
}
?>

Link to comment
Share on other sites

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 11

 

Warning: Division by zero in /home/phcleani/public_html/display_records.php on line 14

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 21

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 27

 

Warning: Division by zero in /home/phcleani/public_html/display_records.php on line 27

 

Hi

 

I have changed the sql queries to orders_id (id)and zen_orders (table) but I got these errors.

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 38

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/phcleani/public_html/display_records.php on line 48

 

Warning: Division by zero in /home/phcleani/public_html/display_records.php on line 48

Report to moderator Logged

 

Thanks

 

Phil

 

<?php require_once('Connections/zen.php'); ?>
<?php
$num = $over_num;
$page = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] >= 1 ? $_GET['page'] : 1;

$per_page = $num;

$count_q = mysql_query("SELECT COUNT(orders_id) FROM zen_orders ORDER BY orders_id DESC");
$count_r = mysql_fetch_row($count_q);
$count = $count_r[0];

if($page > ceil($count/$per_page)) $page = 1;


$limit_min = ($page-1)*$per_page;
$query = mysql_query("SELECT * FROM zen_orders ORDER BY id DESC LIMIT {$limit_min}, {$per_page}");
   
$sql = mysql_query("SELECT * FROM zen_orders ORDER BY id DESC");
$num_rows = mysql_num_rows($sql);  

$limit2 = $per_page;

$q = "SELECT orders_id FROM zen_orders";
$r = mysql_query($q);
$num = ceil(mysql_num_rows($r) / $limit2);
if($num != 1)
{
  for($i = 1; $i <= $num; $i++)
  {
    /*if($page / $limit2 != $i)*/ echo "[<a href='?p=list&page=".$i."'>Page ".$i."</a>] ";
    //else echo "[<b>".$i."</b>] ";
  }
}
echo "<br /><br />";
  //while($row = mysql_fetch_array($sql)){
  while($row = mysql_fetch_assoc($query)) {
  
     //echo out DB info here

}

$limit2 = $per_page;

$q = "SELECT orders_id FROM zen_orders";
$r = mysql_query($q);
$num = ceil(mysql_num_rows($r) / $limit2);
if($num != 1)
{
  for($i = 1; $i <= $num; $i++)
  {
    /*if($page / $limit2 != $i)*/ echo "[<a href='?p=list&page=".$i."'>Page ".$i."</a>] ";
    //else echo "[<b>".$i."</b>] ";
  }
}
?>

Report to moderator    Logged 

Link to comment
Share on other sites

Here is the page that will call the info from the database.

 

Thanks,

 

Phil

<?php require_once('Connections/zen.php'); ?>
<?php
mysql_select_db($database_zen, $zen);
$query_Recordset1 = "SELECT * FROM zen_orders";
$Recordset1 = mysql_query($query_Recordset1, $zen) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

<?php
mysql_free_result($Recordset1);
?>

Link to comment
Share on other sites

Here is the PHPFreaks pagination tutorial that I have tried to upload

 

This is the error I am getting - Fatal error: SQL in /home/phcleani/public_html/display_records4.php on line 42

 

Line 42 is - $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); 

 

<?php   
// database connection info   
$conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR);   
$db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);   
  
// find out how many rows are in the table    
$sql = "SELECT COUNT(*) FROM numbers";   
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_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    
$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";   
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);   
  
// while there are rows to be fetched...   
while ($list = mysql_fetch_assoc($result)) {   
   // echo data   
   echo $list['id'] . " : " . $list['number'] . "<br />";   
} // end while   
  
/******  build the pagination links ******/  
// range of num links to show   
$range = 3;   
  
// if not on page 1, don't show back links   
if ($currentpage > 1) {   
   // show << link to go back to page 1   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";   
   // get previous page num   
   $prevpage = $currentpage - 1;   
   // show < link to go back to 1 page   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";   
} // end if    
  
// loop to show links to range of pages around current page   
for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) {   
   // if it's a valid page number...   
   if (($x > 0) && ($x <= $totalpages)) {   
      // if we're on current page...   
      if ($x == $currentpage) {   
         // 'highlight' it but don't make a link   
         echo " [<b>$x</b>] ";   
      // if not current page...   
      } else {   
         // make it a link   
     echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";   
      } // end else   
   } // end if    
} // end for   
            
// if not on last page, show forward and last page links       
if ($currentpage != $totalpages) {   
   // get next page   
   $nextpage = $currentpage + 1;   
    // echo forward link for next page    
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";   
   // echo forward link for lastpage   
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";   
} // end if   
/****** end build pagination links ******/  
?>  

Link to comment
Share on other sites

Hi

 

I thought I'd take another look at the sql query and guess what, I found an error - durh! It's always the small things that get you.

 

Thanks for your reply and your help of course. I will need some help in making the alternate rows background a different color, I don't know if you can help with that, butI am just glad it's working.

 

All the best,

 

Phil

 

PS I will mark this thread solved.

Link to comment
Share on other sites

Just throw a ternary assignment into the loop.  Something like this:

 

$color = ($color == '#FFFFFF')? '#000000' : '#FFFFFF';

 

Then just throw the $color into the bgcolor value or however you have the html/css setup. 

 

Color will initially be assigned white because it will initially not be anything.  Next loop iteration since it's white, it will be assigned black.  Just keeps switching back and forth like a uh, switch.  You can, of course, change what colors you want to use. 

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.