有人可以帮我清理这个功能并使其正常工作吗?循环功能不能正常工作
它应该看起来像这样:http://gyazo.com/04c31a3edaabeca5f5c6376f1cb607ca.png 除了在类别下它应该只列出与cat_id匹配的类别,它可以工作。
但是页面看起来很奇怪。 这是它看起来像现在:http://gyazo.com/f217603bede98210dce21328e1aab34f.png
function getcatposts($cat_id)
{
$sql = mysql_query("SELECT COUNT(*) FROM `topics`");
if(!$sql){
echo 'Error: ',mysql_error();
}
$r = mysql_fetch_row($sql);
$numrows = $r[0];
$rowsperpage = 10;
$totalpages = ceil($numrows/$rowsperpage);
echo '
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="info">
<th>Title</th>
<th>Username</th>
<th>Date</th>
<th>Category</th>
</tr>
</thead>';
if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql = mysql_query("SELECT * FROM `topics` ORDER BY topic_id DESC LIMIT $offset, $rowsperpage");
if(!$sql){
echo 'Error: '.mysql_error();
}
$link = mysqli_connect("localhost", "lunar_lunar", "", "lunar_users");
$qc = mysqli_query($link, "SELECT * FROM topics WHERE topic_cat='$cat_id'");
while($row = mysqli_fetch_array($qc))
{
$topic_title[]=$row['topic_subject'];
$topic_id[]=$row['topic_id'];
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2))
{
$cat_name[]=$row2['cat_name'];
}
for ($i=0; $i < count($topic_id); $i++)
{
echo '
<tbody><tr>
<td><a href="topic.php?id='.$topic_id[$i].'">'.$topic_title[$i].'</a></td>
<td><a href="../public.php?id='.$res['topic_by'].'">'.getOwner($res['topic_by']).'</td></a>
<td>'.$res['topic_date'].'</td>
<td>'.$cat_name[$i].'</td>
</tr></tbody>
';
}
}
两条链接都重定向到同一页面。至少对于我来说。 – Kaarel
对不起,我忘了你需要登录后才会更新屏幕截图。 –
与您的问题不完全相关,但由于安全问题,mysql_ *函数已被弃用。改为使用PDO或mysqli,并为分页查询使用准备好的语句,这很容易受到SQL注入的影响。 – Snowburnt