Jump to content

[SOLVED] Help with limiting results


almystersv

Recommended Posts

Hi guys,

 

Im having trouble getting my results to be displayed on a number of pages. i.e. results 1-10 on page 1, 11-20 on page 2 etc. I have got the results working on page 1 but then the same code but altered slightly does not work for page 2.

 

Here is the code I have on page 1 that displays the results 1- 10.

 

<?php

$URN = 1;

while($row = mysql_fetch_array($result))

if($URN <= 10)

{

?>

  <tr>

    <td height="27"><div align="center"><?php echo $row['URN']?></div></td>

    <td><?php echo $row['productName']?></td>

<td><?php echo $row['productType']?></td>

    <td><?php echo $row['description']?></td>

<td>£<?php echo $row['price']?></td>

<td width="34"><a href="addtobasketMain.php?URN=<?php echo $row['URN']?> &productName=<?php echo $row['productName']?> &description=<?php echo $row['description']?> &price=<?php echo $row['price']?>" > [+] </a></td>

  </tr>

<?php

$URN ++;

}

else

{

}

?>

 

I need to modify this to show the results for 11-20 etc.

 

Your help will be greatly appreciated.

 

Cheers

Link to comment
Share on other sites

Maybe this will give you an idea on what you can do:

<?php
// database connection stuff //

// this will get the page number from the URL //
$page_number = $_GET['page'];

// now you want to find out how many results you
// want to display per page.
// You may want to use mysql_num_rows and then
// divide by $number_of_pages to get $results_per_page
// if you're going to do that, take the ceil of $number_of_pages
$number_of_pages = 20;
$results_per_page = 10;

// find the starting result of the page
// if each page has $results_per_page results, it's easy to 
// find the starting point given $page_number
$starting_point = ($page_number - 1) * $result_per_page + 1;

// find ending result
$ending_point = $page_number * $result_per_page

// here is what your query should look like
$query = "SELECT * FROM table_name LIMIT $starting_point, $ending_point";

// rest up to you, this is just to start you off in the right direction of thinking 
?>

 

Hope this helps

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.