2015-12-27 81 views
1

我有這個類用於電子商務Web應用程序的類別菜單中使用的megamenu。如何獲得相同的列?

class categories { 

    var $categorie; 
    var $tabs; 
    var $subcategorie; 
    var $website; 

    var $coloane = 3; 
    var $randuri = 10; 


    function tabs(){ 

     global $pdoconnect; 

     $stmt = $pdoconnect->query("SELECT * FROM taburi WHERE vizibil = '1' ORDER BY ordine ASC"); 
     $tabs = $stmt->fetchAll(PDO::FETCH_ASSOC); 
     $tabs_c = $stmt->rowCount(); 

     $return = ""; 
     if($tabs_c > 0){ 
      $i = 1; 
      $return .= "\n<ul class=\"tabs\">\n"; 
      foreach($tabs as $t){ 
       $return .= "\t<li class=\"level0\">\n"; 
       $return .= "\t\t<div class=\"tab-title\">\n"; 
       $return .= "\t\t\t<span class=\"tab-image\">\n"; 
       $return .= "\t\t\t\t".'<img src="'.$this->website.'/media/taburi/'.$t['logo'].'" />'."\n"; 
       $return .= "\t\t\t</span>\n"; 
       $return .= $t['nume']; 
       $return .= "\t\t</div>\n"; 
       $return .= "\t".'<div class="level1 hmm-megamenu-box">'."\n"; 
       $return .= $this->categorie($t['id']); 
       $return .= "\t</div>\n"; 
       $return .= "\t</li>\n"; 
      } 
      $return .= "</ul>"; 
     } 
     return $return; 
    } 

    function categorie($tab){ 
     global $pdoconnect; 

     $return = ""; 
     $stmt = $pdoconnect->prepare("SELECT * FROM categorii WHERE tab =:tab AND vizibil='1'"); 
     $stmt->bindValue(':tab', $tab, PDO::PARAM_INT); 
     $stmt->execute(); 

     $categorie = $stmt->fetchAll(PDO::FETCH_ASSOC); 
     $categorie_c = $stmt->rowCount(); 

     if($categorie_c > 0){ 
      $i = 0; 
      $return .= "<div class=\"hmm-megamenu-block\">\n"; 
      foreach ($categorie as $cat) { 
       $return .= "\t<div class=\"hmm-megamenu-column\">\n"; 
       // if($i % 2 == 0 && !empty($i) && $this->countSubcategorii($cat['categorie']) <= $this->randuri){ 
       // $return .= "</div><div class=\"hmm-megamenu-column\">\n"; 
       // } 
       $return .= "\t\t\t<a class=\"head-list\">".$cat['categorie']."<i class=\"fa fa-chevron-right\" style=\"font-size: 8px; margin-left: 5px;\"></i></a>\n"; 
       $return .= $this->subcategorie($cat['categorie']); 
       $return .= "\t\t</div>\n"; 
       $i++; 
      } 

      $return .= "</div>"; 

     } 
     return $return; 

    } 


    function subcategorie($cat){ 
     global $pdoconnect; 

     $return = ""; 
     $stmt = $pdoconnect->prepare("SELECT * FROM categorii WHERE categorie=:categorie AND subcategorie IS NOT NULL"); 
     $stmt->bindValue(':categorie', $cat, PDO::PARAM_STR); 
     $stmt->execute(); 

     $subcategorie = $stmt->fetchAll(PDO::FETCH_ASSOC); 

     $subcategorie_c = $stmt->rowCount(); 

     if($subcategorie_c > 0){ 
      foreach ($subcategorie as $scat) { 
       $return .= "\t".'<a href="'.$this->website.'/c/'.strtolower($scat['seo']).'">'.$scat['subcategorie'].'</a>'."\n"; 
      } 
     } 
     return $return; 
    } 
} 

眼下的菜單項看起來像這樣

enter image description here

,但期望的結果應該是

enter image description here

我怎樣才能得到這樣的結果只是用PHP?

+6

是不是真的更多的是CSS問題? – adeneo

+0

不是,我需要這樣做只是用php – speedy

+1

而這個評論顯示我,你應該在實際編碼任何東西之前閱讀很多,年輕的padawan ^^ – Sirko

回答

0

解決

function categorie($tab){ 
    global $pdoconnect; 

    $return = ""; 
    $stmt = $pdoconnect->prepare("SELECT * FROM categorii WHERE tab =:tab AND vizibil='1'"); 
    $stmt->bindValue(':tab', $tab, PDO::PARAM_INT); 
    $stmt->execute(); 

    $categorie = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    $categorie_c = $stmt->rowCount(); 
    $return  .= "<div class=\"hmm-megamenu-block\">\n"; 
    if($categorie_c > 0){ 
     $i = 1; 
     $return .= "\t<div class=\"hmm-megamenu-column\">\n"; 
     foreach ($categorie as $cat) { 
      if($i % 3 == 0 && !empty($i) && $this->countSubcategorii($cat['categorie']) <= $this->randuri){ 
       $return .= "</div><div class=\"hmm-megamenu-column\">\n"; 
      }else if($i % 3 == 0 && !empty($i) && $this->countCategorii($tab) >= $this->coloane){ 
       $return .= "</div><div class=\"hmm-megamenu-column\">\n"; 
      }else if($i % 2== 0 && $this->countCategorii($tab) == 2){ 
       $return .= "</div><div class=\"hmm-megamenu-column\">\n"; 
      } 
      $return .= "\t\t\t<a class=\"head-list\">".$cat['categorie']."<i class=\"fa fa-chevron-right\" style=\"font-size: 8px; margin-left: 5px;\"></i></a>\n"; 
      $return .= $this->subcategorie($cat['categorie']); 
      $i++; 

     } 

     $return .= "</div>"; 


    } 
    $return .= "</div>"; 
    return $return; 

} 
1

我懷疑問題是風格和HTML標記的兩倍。

從風格的角度來看,你當前的佈局或多或少是一個網格。你需要在CSS中更流暢的東西。

要做到這一點,您可能需要預先計算列,以便您知道將總共Y個項目的X個組均勻分配到3列。這使您可以處理一個組可能有更多項目不適合菜單高度(並且需要溢出)的情況​​。

相關問題