PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
When I test my code it shows a different page to what it is suppose to. For instance when I click on the next link on the first page of the articles it shows ?page=1 in the link. Can someone please tell me why and how I can fix it? Thanks in advance.
require "config2.php";
if(!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 1;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM items LIMIT $from, $max_results") or die ("Could not select the database because: " . mysql_error());
while($row = mysql_fetch_array($sql)){
// Build your formatted results here.
echo $row['date_entered'].'<br>'.$row['article_item']."<br />";
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM items"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>";
if ($page == 1) {
echo "Previous ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
if ($page == $total_pages) {
echo " Next";
} else {
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Next</a>";
}
echo "</center>";
// and close the database connection
mysql_close();