我正在一个将新闻拆分到不同页面的网站上工作,我已经为此编写了一些 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>';