PHP News Page Help

Associate
Joined
7 Sep 2003
Posts
1,897
Location
Bo'ness
I have created a simple php script that grabs news entries from a mysql database.

Currently this script selects all information from the database and displays it in a blog style format.

I am wondering how I could implement different pages so that the news page would only display the first 10 entries then there would be a previous button that would display the next 10 entries and so on.

Thanks in advance.
 
you'd bascially went php to check how many news entries there are into total, divide by 10 and then round up to nearest whole number. this will give you the amount of "pages" required. then get php to display the amount as links using the for loop, so starting from 1, so when i is less then the amount of pages display the value of i as a <a href=blah.php">i</a><br /> and then increase i.

then instaed of having pages you just change the url to tell php which set is currently wanted to be viewed, ie blah.php?set=1 etc.

then get php to check what set is in the url, in this case 1, and display all the news articles that would be in set 1, so 1,2,3,4,5,6,7,8,9,10. if it was set 5 or something it would display 51,52,53,54,55,56,57,58,59,60. remember to make it so if no info is in the url, ie the user is just visiting blah.php that php then has a default set to display, or some message about choosing a set etc.

thats bascially what you would need to look into doing, ask away if you need help.
 
Last edited:
Example

Code:
$Limit = 50; //Number of results per page
If($_GET['page'] == "") $page=1; //If no page number is set, the default page is 1

//Get the number of results
$SearchResult=mysql_query("SELECT * FROM table") or die(mysql_error());
$NumberOfResults=mysql_num_rows($SearchResult);

$NumberOfPages=ceil($NumberOfResults/$Limit);

//display your stuff


// page numbers

$Nav="";
For($i = 1 ; $i <= $NumberOfPages ; $i++) {
If($i == $page) {
$Nav .= "<B>$i</B>&nbsp;";
}Else{
$Nav .= "<A HREF=\"news.php?page=" . $i . "\">$i</A>&nbsp;";

}
}
Echo "<BR><BR>" . $Nav;

Of course layout the pagination as you like :)

EDIT : Wow, OcUK need to change the colour scheme for PHP Highlighting. Maybe set it a white background?
 
I bought up the issue in the 'OcUK Forum Changes' thread a while ago but it's been the same for years.
 
Back
Top Bottom