2017-06-17 45 views
0

我正在嘗試構建博客,但現在它們首先按類別排序,然後按ID排序。現在,我有2個循環,一個用於類別,一個用於博客。博客的循環處於其中一個類別中。博客無法正確排序

我希望這些博客可以按id排序。所以,無論何時發佈博客,我都希望它出現在第一位,而不管它是什麼類別。你能爲我提供關於如何修改我的代碼的建議:

<?php 
      include_once("../forum/connect.php"); 
    $sql = "SELECT * FROM blogcategories ORDER BY category DESC"; 
    $res = mysql_query($sql) or die(mysql_error()); 
    $blogs = ""; 
    if(mysql_num_rows($res) > 0){ 
     while($row = mysql_fetch_assoc($res)){ 
      $id = $row['id']; 
      $category = $row['category']; 

      $sql2 = "SELECT * FROM blog WHERE blogcategories='".$id."' ORDER BY id DESC"; 
      $res2 = mysql_query($sql2) or die(mysql_error()); 
      if(mysql_num_rows($res2) > 0){ 

       while($row2 = mysql_fetch_assoc($res2)){ 

       $tid = $row2['id']; 
       $title = $row2['title']; 
       $image = $row2['image']; 
       $excerpt = $row2['excerpt'];        
      $blogs .= " 


      <hr> 
       <h1>".$title."</h1> 
       <img src=".$image." alt=".$title."> 
       <p class='description'>".$excerpt."</p> 

     <!--------BUTTON READ MORE--------> 

     <div id='hovers'> 
     <a href='blog_view.php?cid=".$id."&tid=".$tid."' class='button' target='_blank'> 
      <span class='contentbut'> Read More</span> 
     </a> 
    </div> 
    ";   
     } 
     } 
    } 

    echo $blogs; 
} 
    ?> 

回答

0

認沽結果與任何其他數據結構和排序。使用php標準庫。

+0

你有什麼建議嗎? – Kaloyan

+0

其實查詢結果應該排序。我認爲當你使用第一個查詢的結果來操作第二個時出現問題。 – OsamaKhalid

+0

嘗試在迴應博客之前回顯結果。 – OsamaKhalid