Jump to content

How could I paginate this?


Thauwa

Recommended Posts

mysql_connect("host", "user", "pass") or die(mysql_error()) ; 
mysql_select_db("database") or die(mysql_error()) ; 
//Retrieves data from MySQL 
$data = mysql_query("SELECT * FROM table") or die(mysql_error()); 
//Puts it into an array 
while($info = mysql_fetch_array( $data )) 
{ 
echo "<a href=\"/view.php?id={$info['id']}\">{$info["name"]}</a> <br>";
echo "Description: ".$info['description'] . " <br><hr>"; 
}

Could any one paginate this for me. It is too complicated for me. Please don't ban me for asking such a direct question, moderators, for I've gone nuts trying to paginate this simple echo.

Thanks in advance.

P.S. I've read many tuts on pagination and understood none :(

Link to comment
Share on other sites

First you'll need to find out how many rows the query will return in total. Without this number you can't figure out how many pages you'll have.

$numRows=mysql_num_rows($query);

 

Next we need a page size

$pageSize=10;

Let's set it to 10 items per page

 

Let's find out how many pages we're playing with

$pageCount=floor($numRows/$pageSize)

 

See if we've got any spilling over to the last page

$lastCount=$numRows % $pageSize;

 

Now let's have a starting page number:

$pageNum=1;

 

Now we can build the query

$query=mysql_query("SELECT * FROM table LIMIT ".($pageNum-1).",".$pageSize);

 

How's that for starters?

Link to comment
Share on other sites

I appreciate you helping me in many of my posts. Thanks.

In a tutorial, I got a code like this,

$pages_query = '';
if ($sort != 0) {
$pages_query .= '&sort='.$sort;
}
if ($artist) {
$pages_query .= '&artist='.urlencode($artist);
}
if ($a_match) {
$pages_query .= '&a_match='.urlencode($a_match);
}

// $pages_link is for page numbers only!
$pages_link = 'index.php?pageno={page_link}'.$pages_query;

// Fourth parameter is how many digits on each side of the current page number will be printed
$page_numbers_formatted = quick_page_numbers(0, $pageno, $pages, 5, $pages_link)."\n";

 

but it was too complex. Ummm...I feel ashamed of myself...........

**how do I create the next, prev, links.. :( :'(

Answer it only if you want to.

Link to comment
Share on other sites

Why feel ashamed? This is the place to come for if you need help.

 

My last question for help was last night with something REALLY simple.

 

Let's say you're on page 1 - the first page. Now we can't have a previous link because that'll put us onto a non-existant page of 0. Only if we've got enough data returned from the table can we have a next link also.

 

We've set the size of each page to 10 lines so if we've got more than 10 lines of data being returned we can use a next link.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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