2013-07-30 66 views
-2

下面是我在做什麼我有兩個表一個是類別和其他的軟件如何通過避免重複顯示兩個表php mysql中的數據?

分類表看起來像這樣

cid  name 
1  Anti-malware 

和軟件表看起來像這樣

id  title  catid 
1  Avast  1 

And catid is equal cid can say can catid is id of category

這裏是我當前的代碼

require('config/db.php'); 
$cateq="SELECT * FROM softwares A INNER JOIN categories B ON A.catid=B.cID "; 
    $cateresult=mysql_query($cateq); 


    define('MAX_TITLES_PER_CAT',5); 

    $cat=''; 
    $tcount=0; 
    while ($row =mysql_fetch_array($cateresult)){ 
        $title=$row['title']; 
        $softid=$row['id']; 
        $cateid=$row['cid']; 
        $check_pic = "admin/images/icons/".$softid."/image01.jpg"; 

        if (file_exists($check_pic)) { 
          $icon = "<img src=\"$check_pic\" class=\"home_icon\" />"; 
          } else { 
           $icon = "<i class=\"icon-archive\"></i>"; 
           } 

        if ($row['name']!=$cat) { 

          if ($cat!='') echo '</div>'; 
          $cat=$row['name']; 
           $tcount=MAX_TITLES_PER_CAT; 
           echo "<div class=\"widget white_box\"><h3 class=\"widget_title\"><a href=\"categories.php?cid=$cateid\">".$cat." <i class=\"icon-chevron-right\"></i></a></h3><ul>"; 
           } 


         if ($tcount--<=0) continue; 
         echo '<li>'.$icon.' <a href=\'software-profile.php?pid='.$softid.'\'>'.$title.'</a></li>'; 


         } 
        if ($cat!='') echo '</ul></div>'; 

,這裏是我所得到 http://postimg.org/image/d7zn2k6ev/full/ 其重複某些類別在不同的div,而不是在同一個div所有的結果顯示結果。

+0

這個問題可以通過對數據進行排序來解決:'SELECT * FROM softwares A INNER JOIN categories B ON A.catid = B.cID ORDER BY B.cID' – Barranka

+0

you right right resolved – user2635066

回答

0

解析查詢結果時,假定行按類別排序(if ($row['name']!=$cat)),但沒有ORDER BY子句,沒有訂單保證。

ORDER BY name添加到您的查詢結尾。

+0

OMG that was it I was fighting兩天感謝很多 – user2635066