有人可以幫我清理這個功能並使其正常工作嗎?循環功能不能正常工作
它應該看起來像這樣: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