Forum Moderators: coopster

Message Too Old, No Replies

Pages with PHP

Need pointing in the right direction

         

Jeigh

11:37 am on Jun 4, 2007 (gmt 0)

10+ Year Member



Hey, I'm fairly new to PHP and I've been working on my site. I've made a section where people can make profiles. I want to be able to add some profiles to a database and then show them on the site.

What I want done is so that say after 15 links to profiles are on a page, they will start moving to page 2, then 3 like the forum topics do. I was just wondering if anyone could point me in the right direction, where I could look at how to create somthing like this. I only want somthing very basic, pretty much just enough to get the job done.

Thanks.

Scally_Ally

12:03 pm on Jun 4, 2007 (gmt 0)

10+ Year Member



If you are using a mysql database you want to be looking at the "LIMIT" option. This lets you choose what records you are pulling from the database.
eg, "SELECT * FROM table LIMIT 30, 60", would return from the 30th result to the 60th. You can then tie this into you site by having a list of numbers representing pages (1, 2, 3, 4) and then processing a variable dependant of what the user clicks on to retrieve the set of reults you are after.

eg.
$resultsPerPage = 30;
$pageNum = $_GET["pageNum"];

$query = "SELECT * FROM table LIMIT ".($pageNum*$resultsPerPage).", ".(($pageNum*$resultsPerPage) + $resultsPerPage)."";

or something similar.

Ally

Jeigh

12:38 pm on Jun 4, 2007 (gmt 0)

10+ Year Member



Thanks for the reply.

I will be using a MySQL database, and that seems to make sense to me, as more things are added to the database the new one will be '1' and say the 30th one would then become number 31 and move to the next page?

Scally_Ally

1:37 pm on Jun 4, 2007 (gmt 0)

10+ Year Member



it should matter how much stuff you add to db, it should work as it grows.

eg.
Page 1
"SELECT * FROM table LIMIT 0, 29";
Page 2
"SELECT * FROM table LIMIT 30, 59";
Page 3
"SELECT * FROM table LIMIT 60, 89";
Page 4
"SELECT * FROM table LIMIT 90, 119";

Ally

Jeigh

6:45 am on Jun 5, 2007 (gmt 0)

10+ Year Member



Thanks, and would the code you've posted here pretty much be enough to get it working? Besides making the database of course.

Habtom

7:18 am on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a tip, the number of pages will not be fixed, as the number of rows you have in the DB won't always be known. Pagination is not very much tricky. The SQL Statment can be altered this way:

[In the URL, (for the second page for example) should partly look like the following:

www.example.com/results.php?page=2

// This tells which page you are trying to acess
$page = $_REQUEST['page'];

//Getting the end value, assuming you will have 30 list on a page
$page_end = $page * 30;
// Getting the beginning number
$page_begin = $page_end - 29;

//Setting the Query
$query = "SELECT * FROM table LIMIT ". $page_begin .", ". $page_end;

This might help.

Habtom