Hi all,
I know there are plenty of topics and tutorials out there that cover this, but all seam to recommend different things and i'm nto sure what to use. I have a page, that displays categories of articles. When a category is clicked on, the news articles are displayed. What I want to do is paginate the display of the news articles.
Here is the code for my main page:
<?php
require_once("var.php"); // To get the bullet and background color info.
require_once("mysql_conn.php"); // To get the mysql connection
$catid = strtolower($GET['catid']);
$articleid = $GET['article'];
if(!empty($catid)){
$catid = mysql_real_escape_string($catid);
$query = "SELECT title, id, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM news WHERE category='{$catid}' ORDER BY id DESC LIMIT $news_limit";
$r = mysql_query($query);
if($r && mysql_num_rows($r) > 0){
while($row = mysql_fetch_assoc($r)){
$display .= '<tr bgcolor="' . $title_cell_color . '">';
$display .= ' <td><img src="' . $bullet . '" /> <b><a href="news.php?article=' . $row['id'] . '">' . ucfirst($row['title']) . '</a></b> Posted on ' . $row['date'] . '</td>';
$display .= '</tr>';
}
}else if(mysql_num_rows($r) === 0){
$display = '<tr><td>No News Articles Found In The "' . ucwords($catid) . '" Category</td></tr>';
}else{
$display = "<tr><td>MySQL ERROR:</td></tr><tr><td>" . mysql_error() . "</td></tr>";
}
}else if(!empty($articleid)){
$articleid = mysql_real_escape_string($articleid);
$query = "SELECT *, DATE_FORMAT(postdate, '%Y-%m-%d') as date FROM news WHERE id='{$_articleid}'";
$r = mysql_query($query);
if($r && mysql_num_rows($r) > 0){
while($row = mysql_fetch_assoc($r)){
$display .= '<tr bgcolor="' . $title_cell_color . '">';
$display .= ' <td><img src="' . $bullet . '" /> <b>' . ucfirst($row['title']) . '</b> Posted in <a href="news.php?catid=' . $row['category'] . '">' . $row['category'] . '</a> on ' . $row['date'] . '</td>';
$display .= '</tr>';
$display .= '<tr bgcolor="' . $news_cell_color . '">';
$display .= ' <td>' . $row['content'] . '</td>';
$display .= '</tr>';
}
}else if(mysql_num_rows($r) === 0){
$display = '<tr><td>The Article was not found. It could have been moved or deleted. <a href="index.php">Try another selection</a></td></tr>';
}else{
$display = "<tr><td>MySQL ERROR:</td></tr><tr><td>" . mysql_error() . "</td></tr>";
}
}else{
// Get Results for the Category.
$r = mysql_query("SELECT category, COUNT(title) as count FROM news GROUP BY category");
if($r && mysql_num_rows($r) > 0){
$display .= '<tr bgcolor="' . $title_cell_color . '">';
$display .= ' <td>Please Select a Category</td>';
$display .= '</tr>';
while($row = mysql_fetch_assoc($r)){
$display .= '<tr bgcolor="' . $news_cell_color . '">';
$display .= ' <td><a href="news.php?catid=' . $row['category'] . '">' . ucwords($row['category']) . '</a> (' . $row['count'] . ' Articles)</td>';
$display .= '</tr>';
}
}else if(mysql_num_rows($r) === 0){
$display = '<tr><td>No News Categories Were Found In The Database. Please <a href="add.php?action=newCat">Add</a> Some.</td></tr>';
}else{
$display = "<tr><td>MySQL ERROR:</td></tr><tr><td>" . mysql_error() . "</td></tr>";
}
}
echo "<br />
<table width='100%'>
" . $display . "
</table>
<br />";
?>
In my var.php file, I have a limit variable:
<?php
$news_limit="10"; // number of news that script shows
?>
As you can see from the above code, this variable is used to define how many news items are show on a page. At the moment, by default, it shows the 10 newest articles, and gives no option for the others. What I want is for all articles to be displayed, but paginated into pages of 10 articles.
Any suggestions on how to do this would be much appreciated.
thanks!
Ashley