Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] struktura kategorii
Forum PHP.pl > Forum > PHP
kallosz
  1. <?
  2. class menu {
  3.    protected $menu = null;
  4.    protected function level1($id, $ss) {
  5.        global $dbh, $sys_lang;
  6.        $zap = "select * from menu WHERE pid=$id ORDER BY p ASC";
  7.        $sth = $dbh->prepare($zap);
  8.        $sth->execute();
  9.        $dane = $sth->fetchAll();
  10.        $ss    =    ($ss == 1) ? ' id="qm0" class="qmmc" style="width: 600px; float: left;"' : '';
  11.        $menu = '<ul'.$ss.'>'."\n";
  12.        foreach($dane as $k => $e) {
  13.            $menu .="<li>\n";
  14.            if($this->ile($e['id']) > 0) {
  15.                $menu .= $this->generujlink($e['link'],$e['tytul'.$sys_lang],1,$e['blank'],$this->ile($e['id']),$e['id']);
  16.                $menu .= $this->parentel($e['id']);
  17.            } else {
  18.                $menu .= $this->generujlink($e['link'],$e['tytul'.$sys_lang],0,$e['blank']);
  19.            }
  20.            
  21.            $menu .= '</li>'."\n";
  22.            if($ss == 1) {
  23.            $menu .='<li><span class="qmdivider qmdividery"></span></li>'."\n";
  24.            }
  25.        }
  26.        $menu.="</ul>\n";
  27.        
  28.        $this->menu = $menu;
  29.    }
  30.    
  31.    protected function parentel($id) {
  32.        global $dbh, $sys_lang;
  33.        $zap = "select * from menu WHERE pid={$id} ORDER BY p ASC";
  34.        $sth = $dbh->prepare($zap);
  35.        $sth->execute();
  36.        $dane = $sth->fetchAll();
  37.        $menu2 = "<ul>\n";
  38.        foreach($dane as $k => $e) {
  39.            $menu2 .="<li>\n";
  40.            if($this->ile($e['id']) > 0) {
  41.                $menu2 .= $this->generujlink($e['link'],$e['tytul'.$sys_lang],1,$e['blank'],$this->ile($e['id']),$e['id']);
  42.                $menu2 .= $this->parentel($e['id']);
  43.            } else {
  44.                $menu2 .= $this->generujlink($e['link'],$e['tytul'.$sys_lang],0,$e['blank']);
  45.            }
  46.            $menu2 .= "</li>\n";
  47.        }
  48.        $menu2.="</ul>\n";
  49.        return $menu2;
  50.    }
  51.  
  52.    public function ile($id) {
  53.        global $dbh;
  54.        $zap = "SELECT COUNT(*) from menu WHERE pid={$id}";
  55.        $sth = $dbh->prepare($zap);
  56.        $sth->execute();
  57.        $baza = $sth->fetch();
  58.        return $baza[0];
  59.    }
  60.    
  61.    protected function generujlink($link, $tytul, $e = 0, $t = 0, $ile = 0, $id = 0) {
  62.        global $sf;
  63.        if(preg_match("/^kt[([0-9]*)]/", $link, $kt)) {
  64.            $link = $sf.'?kt='.$kt[1];
  65.        }
  66.        if(empty($link)) {
  67.            if($ile > 0) {
  68.                $link = $sf.'?kt=menu&id='.$id;
  69.            } else {
  70.                $link = '#';
  71.            }
  72.        }
  73.        $e = ($e == 1) ? 'class="qmparent" ' : '';
  74.        $t = ($t == 1) ? ' target="_blank"' : '';
  75.        $link = '<a '.$e.'href="'.$link.'"'.$t.'>'.$tytul.'</a>';
  76.        return $link;
  77.    }
  78.    
  79.    public function __construct($id = 0, $ss = 1) {
  80.        $this->level1($id, $ss);
  81.    }
  82.    
  83.    public function getmenu() {
  84.        return $this->menu;
  85.    }
  86. }
  87. ?>
napisałem taki speudo kod ktory sluzy do twozenia struktury kategorii.
moje pytanie jest nastepujace. czy idzie to jakos zoptymalizowac? chodzi o ilosc zapytan do bazy...
Pilsener
Ja robiłem wg tego:
http://blog.mwojcik.pl/2008/02/17/drzewa-k...-php-metoda-ip/
Działa pięknie i szybko, więc z tym akurat nie widzę problemu. Problem zaczyna się, kiedy trzeba zbudować system uprawnień.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.