Jump to content

Database Results on Multiple Pages


Trekky1700

Recommended Posts

Hello, this is my first post.

 

I have been using PHP for a few months now, and am working on my second PHP website. It's more of a test than anything, with databases. But I've run into a problem  :'(. I've made a site where users can add "posts" to the database (think the site fmylife.com).

 

I want results from the database to display in pages within the page, just like on the site. I used a tutorial for a php search that displays results from the database in pages, but can't get it to work. It implements well, but the pages won't change.

 

Here's the code:

<?php
mysql_connect("l-------","-------"); 
mysql_select_db("--------"); 
$limit = '10';
$query = "select * from ------- order by date DESC";
$numresults = mysql_query ($query) or die ("Couldn't execute query: Reason; ".mysql_error());
$row_num_links_main =mysql_num_rows ($numresults);
$row_count += $row_num_links;
if(!isset($s)) 
         $s=0;

$query .= " LIMIT $s,$limit" ;
$numresults = mysql_query ($query) or die ("Couldn't execute query: Reason; ".mysql_error());

while($row = mysql_fetch_array($numresults)) {
  
  echo "<tr><td class=td_main><table class=posts><tr><td width=93% class=tdsubmission>{$row['submission']}</td></tr>
         <tr class=tr_info><td>Submitted by {$row['nickname']} at {$row['date']}.</td>
         <td>Flags: {$row['flagged']}</td></tr></table>";
	 $count++ ;
  }


$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp ";
  }

// calculate number of pages needing links
  $pages=intval($row_num_links_main/$limit);

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

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

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $row_num_links_main) { $a = $row_num_links_main ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $row_num_links_main</p>";

?>

 

Thank you in advance for any help/insight you can provide  :).

Link to comment
https://forums.phpfreaks.com/topic/199090-database-results-on-multiple-pages/
Share on other sites

1. How does the value $s change? I can't find the line where it changes.

2. To get the number of entries, it'll be faster to use COUNT. The order by doesn't affect anything so I left it out.

SELECT COUNT(*) AS num_rows FROM table;

 

I have no idea, I can give you a link to the tutorial I used parts of.

http://www.designplace.org/scripts.php?page=1&c_id=25

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.