I am trying to set up a record pagination system for my home page it does work but not quite how I would like it to.
I would like to have a user enter www.mydomain.com/index.php and have the first five records display and the navigation start from there. But I have to declare a starting point so I did the following:
$records = $_GET['records'];
This is my problem, I need to have the "records" as part of the URL or else I get a notice
Notice: Undefined index: records in C:\wamp\www\phpPractice\index2.php on line 9
However, I can't think of any other way of making this work.
I have posted the relevant code.
//determine the number of records to be displayed per page
$per_page = 5;
//variable in order to declare a starting point
$records = $_GET['records'];
//Count the records in the database
$record_count = mysql_num_rows(mysql_query("SELECT * FROM myblogposts"));
//if the index.php page is found through a search engine set the starting point as below
if (!$records) {
$records = 0;
}
$sqlCommand = mysql_query("SELECT * FROM myblogposts ORDER BY reported_date DESC LIMIT $records, $per_page");
//now loop through all data
$blogPage = '';//this declares a compound variable for posting content to the html page
while($row = mysql_fetch_assoc($sqlCommand)){
$id = $row['bpid'];
$title = $row['title'];
$pic = $row['picURL'];
$picAlt = $row['picALT'];
$picTitle = $row['picTitle'];
$content = $row['article_body'];
$date = $row['reported_date'];
$category = $row['catid'];
//set up page navigation
$prev = $records - $per_page;
$next = $records + $per_page;
if (!($records <=0)) { //show previous button but show nothing if on the first page
//declare variable and echo below to html page
echo '<a href="index2.php?records='.$prev.'">Prev</a> ';
}
//set variable for first page and show page numbers in a for loop
$i = 1;
for ($x=0; $x < $record_count; $x=$x + $per_page)
{
echo ' <a href="index2.php?records='.$x.'">' .$i. '</a>';
$i++;
}
if (!($records >=$record_count - $per_page)) {//show next button but show nothing if on the last page
//declare variable and echo below to html page
echo '<a href="index2.php?records='.$next.'"> Next</a>';
}
Any help would be appreciated.
Thanks
Gerry