Jump to content

PHP+SQL Issue


haseebahmed

Recommended Posts

What this Code does is that it helps you achieve Facebok/Twitter Style Pagination. Running Part 1 it loads First 3 ROWS of the MySQL database. Running Part 2 loads the next 3 ROWS of the database. Then you can run Part 2 as many times as you want & it would always load the next 3 ROWS of the database.The Normal Code for this is:-

PART1:<?php$query ="SELECT * FROM table ORDER BY id DESC LIMIT 3";$result = mysqli_query($dbc,$query);while($row=mysqli_fetch_array($result)){$row_id=$row['id'];<!-- SOME CODE TO BE EXECUTED -->}?>PART2:<?php$query ="SELECT * FROM table WHERE id>".$row_id." ORDER BY id DESC LIMIT 3";$result = mysqli_query($dbc,$query);while($row=mysqli_fetch_array($result)){$row_id=$row['id'];<!-- SOME CODE TO BE EXECUTED -->}?>

Now in my version of the code. I want to order things according to CPC & CPC can be any integer from 10-1. Part 1 of the code is no issue, but how can i make a Part 2 so that every time the next 3 ROWS from the database are listed?Sample Table:ID -- Name -- Country -- CPC01 -- Jhony -- USA -- 1002 -- Tomy -- UAE -- 703 -- Bond -- UK -- 304 -- Ben10 -- USA -- 805 -- Juliana -- UAE -- 106 -- Medina -- UK -- 5

PART 1:<?php$query ="SELECT * FROM table ORDER BY cpc DESC LIMIT 3";$result = mysqli_query($dbc,$query);while($row=mysqli_fetch_array($result)){$row_id=$row['cpc'];<!-- SOME CODE TO BE EXECUTED -->}?>

Please help as soon as possible :-) Thanks!

Link to comment
Share on other sites

hi you need to pass which page you are on and how many you want to view per page and then do something like this:$page = intval($page);if ($page < 1) $page = 1;$start = (($page - 1) * $per_page);then in your SQL you would add this where you set your order:" ORDER BY `table`.`field` LIMIT $start , $per_page ";

Link to comment
Share on other sites

hi you need to pass which page you are on and how many you want to view per page and then do something like this:$page = intval($page);if ($page < 1) $page = 1;$start = (($page - 1) * $per_page);then in your SQL you would add this where you set your order:" ORDER BY `table`.`field` LIMIT $start , $per_page ";
Thanks Alot!! This might be just what i wanted :-D Would tryout & let you know :-)EDIT: Thanks Alot! It worked like a charm :-) Moderator can close the thread now.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...