2017-06-17 48 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