我正在开发一个新闻分成不同页面的网站,我已经有了一些PHP代码,但这似乎无法正常工作。我网站上的新闻被正确分割,但是当一个页面只有足够的消息时,“下一个”按钮仍会弹出,您可以滚动空白页面。我对PHP比较陌生,所以我无法为此提出解决方案,希望有人可以提供帮助。
以下是代码:
else {
$conn = mysql_connect($host, $user, $pass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('nieuws');
$rec_limit = 3;
$sql = "SELECT count(id) FROM updates ";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT id, titel, inleiding, bericht, datum ".
"FROM updates ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
echo "<hr>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$intro = substr($row['bericht'], 0, 330);
echo '<b style="font-size:18px;font-family:Arial,Helvetica,sans-serif;color:#2B2B2B;">'.htmlspecialchars(stripslashes($row['titel'])).'</b> <br> '.
'<i style="font-size:13px;">'.htmlspecialchars(stripslashes($row['datum'])).'</i><br><br> '.
"{$row['inleiding']}".
'... <a href="'.$_SERVER['PHP_SELF'].'?id='.$row['id'].'">Lees verder</a><br><br><br>';
echo '<hr>';
}
if( $page > 0 )
{
$last = $page - 2;
echo "<a style=\"margin-left:0px;\"href=\"{$_SERVER['PHP_SELF']}?page=$last\">Vorige</a>";
echo "<a style=\"margin-left:645px;\" href=\"{$_SERVER['PHP_SELF']}?page=$page\">Volgende</a>";
}
else if( $page == 0 )
{
echo "<a style=\"margin-left:685px;\" href=\"{$_SERVER['PHP_SELF']}?page=$page\">Volgende</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a style=\"margin-left:0px;\" href=\"{$_SERVER['PHP_SELF']}?page=$last\">Vorige</a>";
}
echo '<br><br><br>';