Hi than,

Just wanted your help.
I have been stuck on a place.

I just want to create a script that can echo "next" and "previous" or both based on total number of content.

I have parameter in url like :
mysite.com/index.php?start=8 (page 1)
mysite.com/index.php?start=16 (page 2)
mysite.com/index.php?start=24 (page 3) and so on.....

what i want is fetch total number of content from database and print "next" and previous, for example :

if total no of rows in database are : 32
Item showing per page : 8
getting value "start" from url using $_GET['start'].

If start=0 or start='empty' then echo "next" but only if contents are more then 8, If start=16 then echo "next" and "previous" both, AND If start=24 then echo "next" and "previous" and if start=32 then only echo "Previous".

If possible, Please help me with example code...

Your help will be very thankful...

Recommended Answers

All 2 Replies

There are many such samples in this forum. Did you try a search for pagination?

Why not use page=0, page=1, etc. instead of start=8, start=16, etc.?
This way the number of items you show per page can change, which is better user experience if your users want to load more items on a page.

$items_per_page = (!empty($_GET['show'])) ? $_GET['show'] : 8; // user selection or default to 8
$start_at = $_GET['page'] * $items_per_page;
$end_at = $start_at + $items_per_page - 1;

// Then your queries could be
$sql1 = "SELECT field, names FROM your_table WHERE condition LIMIT $start_at, $items_per_page"; // get your data
$sql2 = "SELECT count(*) AS num_results FROM your_table WHERE condition"; // count how many total results are in the table

// Process your queries

// Finally, print your links
if($start_at > 0) echo "PREVIOUS LINK HTML";
echo "You are on page ".$page+1;
if($end_at < $result_of_sql2) echo "NEXT LINK HTML"; // if your last item

This code is to lay out the theory, I haven't tested it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.