Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] struktura kategorii, generowanie
kallosz
post
Post #1





Grupa: Zarejestrowani
Postów: 272
Pomógł: 19
Dołączył: 29.07.2006

Ostrzeżenie: (10%)
X----


  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...

Ten post edytował kallosz 30.10.2008, 07:06:27


--------------------
Pomogłem ?? Kliknij pomógł :) || kallosz.pl
-----------------------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
Pilsener
post
Post #2





Grupa: Zarejestrowani
Postów: 1 590
Pomógł: 185
Dołączył: 19.04.2006
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


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ń.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 04:07