2013-10-12 168 views
0

有人可以幫我清理這個功能並使其正常工作嗎?循環功能不能正常工作

它應該看起來像這樣: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> 
      '; 
    } 
} 
+3

兩條鏈接都重定向到同一頁面。至少對於我來說。 – Kaarel

+0

對不起,我忘了你需要登錄後纔會更新屏幕截圖。 –

+1

與您的問題不完全相關,但由於安全問題,mysql_ *函數已被棄用。改爲使用PDO或mysqli,併爲分頁查詢使用準備好的語句,這很容易受到SQL注入的影響。 – Snowburnt

回答

2

首先,我看不出其中$資源的定義,這可以解釋爲什麼按主題和話題日期沒有顯示任何東西。也許你忘了更改變量名稱?

第二,您選擇特定類別的名稱,因此只會有一個結果行。當您循環顯示第一個主題時,cat_name [1]和cat_name [2]將顯示爲空。既然你知道你只是選擇一個類別,只需將結果分配給一個變量而不是數組。 cat_name代替cat_name []

你說:

SELECT * FROM categories WHERE cat_id='$cat_id' 

假設CAT_ID是你永遠不會比一個結果多表的主鍵。

當你把它分配給cat_name []你是這樣的:

while($row2 = mysqli_fetch_array($qc2)) 
    { 
     $cat_name[]=$row2['cat_name']; 
    } 

它將獲取一行並將其分配給$ cat_name [0]。

,問題就出現在這裏:

<td>'.$cat_name[$i].'</td> 

循環$ I內部遞增,它將在這種情況下,經過3次。 $ cat_name [0]將返回「web開發」。但$ cat_name [1]和$ cat_name [2]不會有任何結果。或者只需調用$ cat_name [0]而不是$ cat_name [$ i],或者將值賦給$ cat_name並在循環中調用它。

+0

沒關係,我現在明白了。但是,如果我在頁腳上方調用函數方式時出於某種原因將它顯示在頁腳下,可以幫助我解決這個問題嗎? –

+0

謝謝!現在所有的數據都能正確顯示,只要我在下面顯示的頁腳上面調用函數時,任何想法爲什麼?我可以粘貼整個文件,如果你喜歡, –

+0

你沒有給我們足夠的信息來解決這個問題。它可能與如何設置div定位或其他HTML格式問題有關。 你能告訴我們完成的輸出HTML嗎? – Snowburnt