Web Hosting Talk







View Full Version : PHP/mySQL Help Needed [Dividing contents into Pages]


zahid
03-16-2005, 10:04 AM
Hi,

Table1 has the following fields:

id(auto increment), item, description
and there are total 25 records(rows).

Now i want to print these records (9 records per page, in table), and will access pages like this: view.php?page=1

Each page must look like this:


ITEM ITEM ITEM
description description description

ITEM ITEM ITEM
description description description

ITEM ITEM ITEM
description description description


Pages: 1, 2 , 3, 4, 5



Thanks in Advance,
Zahid

azizny
03-16-2005, 11:41 AM
I got some time for the logic:

Start with a variable: LIMIT(HOW MANY ITEMS TO SHOW),SHOW(THE STARTING NUMBER)

mysqlfetch array three coloums, until three rows done... (if there is empty leave.
*start from SHOW to LIMIT, e.g.:


mysql_query("SELECT items LIMIT $SHOW,$LIMIT");


after showing them just display the number of pages (total records divded by the LIMIT).

if current page starting count is the same as the current SHOW, highlight it.

Pass the variables SHOW and LIMIT in the url GET variable when someone clicks on the numbers...

Peace,

zahid
03-16-2005, 12:13 PM
so, i will need to run
mysql_query("SELECT ................."); twice?

once for Total records, and once for showing item (from - to range) ?

will not it double the system load and time to execute?

stormraven
03-16-2005, 12:30 PM
Here's some logic to get you going.

First, start with one query to figure out how many records there are..


mysql_query("SELECT COUNT(*) as num_records from Table1");


Then figure out the number of pages to show..

$num_pages = intval($num_records) / 9;


Also, figure out what page you're on..

$cur_page = intval($_REQUEST['page_num']); // Defaults to 0 if not set


Then do your select to get the data..

$cur_page_start = ($cur_page - 1) * 9;
$cur_page_end = ($cur_page) * 9;

mysql_query("SELECT * from Table1 LIMIT $cur_page_start, $cur_page_end");


Then all you need to do is iterate through the data and display it in rows / columns. This should limit the data coming back from mysql to a bare minimum.

Shouldn't be that difficult..

azizny
03-16-2005, 12:42 PM
You should've searched for (start, display) or paging on the forum... many people have already posted this before...

found something of interest:

http://www.webhostingtalk.com/showthread.php?s=&threadid=318088&highlight=start+display

Peace,

zahid
03-16-2005, 01:34 PM
thanks, and its almost done :)

you people are quick and great :D