Deviant Logo

PHP MySQL Help please! I would like to display the results of a PHPMySQL query on multiple pages like googles

post details top
May 29th, 2010
post details top


results where you can navigate to different pages of results instead of having them all on one page.

I am currently using this code:

$resource=mysql_query("SELECT * FROM tutorials WHERE subcat = '$sub' AND status='2'");
while($row=mysql_fetch_array($resource))
{
$rating = ($row['rating'] / $row['votes']);
echo ("
“.$row['submitted'].” “.$row['name'].”

“);
echo (“
Thumbnail Image

“);
echo (“ “.$row['description'].”

“);
echo (“
  “.$row['views'].” views | Rating “.$rating.” | “.$row['votes'].” votes | “.$row['reviews'].” reviews

“);
}
?>

Could someone convert the code above to be capable of doing this? I can’t figure out how to do it… thanks :)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

2 Comments

  • John P

    Look at this page http://www.htmlfrenzy.com/read.php?8,284 may be it will be helpful for you.

  • diagnostix

    I can’t see all your source code, but i’ll give it a try….

    1. the first thing i would do is to get the total number of records you have in the database. You can do this by calling a query something like:

    ’select count([your_primary_field]) as numrecs from tutorials’

    $numofrecs = $row['numrecs'];

    2. decide on how many records you want to show on a page. so if you ‘numrecs’ is 31, and you want 10 per page, you will need say 4 pages. right?

    $recperpage = 10;
    $numberofpages = ceil ($numofrecs/$recperpage);

    3. You need to get another number which is the starting point where you want to start counting the next ‘$recperpage’ records. How you will work that out is by..

    $starting_record = ($current_page_num* $recperpage)+1;

    so if you are on page 3, you have done (3*10)+1 records, hence your starting_record is 31st record.

    4. You can then modify your select statement to use LIMIT property….

    SELECT * FROM tutorials WHERE subcat = ‘$sub’ AND status=’2′ LIMIT $starting_record, $recperpage;

    Hope that gets you started. It’s not the cleanest of codes, but it should do.

Leave a Reply

Search

  • Partner links