Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Nested PHP/MySQL 2-Levels meni

[es] :: PHP :: Nested PHP/MySQL 2-Levels meni

[ Pregleda: 1835 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Milan Kragujevic
Software Engineer

Član broj: 231903
Poruke: 2220
*.dynamic.isp.telekom.rs.

Sajt: https://milankragujevic.c..


+201 Profil

icon Nested PHP/MySQL 2-Levels meni22.04.2011. u 23:14 - pre 158 meseci
Pozdrav!
Imam meni(u htmlu) koji izgleda ovako:
Code (html):
<li>
                    <a href="#">Glavna stavka</a>
                    <ul>
                 <a href="abc">Pod-stavka</a>
                    </ul>
                </li>

E, sad meni treba da tako izvuče iz baze.
Guglao sam i nemam rešenja.
Ako bi neko hrteo da napravi, bio bnih zavalan!
 
Odgovor na temu

japan

Član broj: 34328
Poruke: 480
*.dynamic.sbb.rs.



+13 Profil

icon Re: Nested PHP/MySQL 2-Levels meni22.04.2011. u 23:22 - pre 158 meseci
http://blogs.sitepoint.com/hierarchical-data-database/
 
Odgovor na temu

Milan Kragujevic
Software Engineer

Član broj: 231903
Poruke: 2220
*.dynamic.isp.telekom.rs.

Sajt: https://milankragujevic.c..


+201 Profil

icon Re: Nested PHP/MySQL 2-Levels meni23.04.2011. u 10:09 - pre 158 meseci
Nije mi jasno.
Odnosno, jeste, ali kako da umesto
Root <br>
Pod-meni1 <br/>
dobijem
<li>Root
<ul>
Pod-meni1
</ul>
</li>

????
 
Odgovor na temu

Br@nkoR
http://localhost

Član broj: 2597
Poruke: 1603

Sajt: localhost


+23 Profil

icon Re: Nested PHP/MySQL 2-Levels meni23.04.2011. u 12:54 - pre 158 meseci
Npr.
Code (php):

$items = array(
           array(
             'id' => 1,
             'parent' => 0,
             'title' => 'food',
           ),
           array(
             'id' => 2,
             'parent' => 1,
             'title' => 'fruit',
           ),
           array(
             'id' => 3,
             'parent' => 2,
             'title' => 'green',
           ),
           array(
             'id' => 4,
             'parent' => 3,
             'title' => 'pear',
           ),
           array(
             'id' => 5,
             'parent' => 2,
             'title' => 'red',
           ),
           array(
             'id' => 6,
             'parent' => 5,
             'title' => 'cherry',
           ),
           array(
             'id' => 7,
             'parent' => 2,
             'title' => 'yellow',
           ),
           array(
             'id' => 8,
             'parent' => 7,
             'title' => 'bannana',
           ),
           array(
             'id' => 9,
             'parent' => 1,
             'title' => 'meat',
           ),
           array(
             'id' => 10,
             'parent' => 9,
             'title' => 'beef',
           ),
           array(
             'id' => 11,
             'parent' => 9,
             'title' => 'pork',
           )
         );
         
function makeTree(&$items) {
  $new = array();
  foreach ($items as &$item) {
    $new[$item['id']] = &$item;
    $new[$item['parent']]['childs'][] = &$item;
  }
  return $new[0]['childs'];
}

$tree = makeTree($items);

function printTree($items) {
  echo '<ul>';
  foreach($items as $item) {
    if(isset($item['childs']) AND count($item['childs']) > 0) {
      echo '<li><a href="open.php?id='.$item['id'].'">' . $item['title'].'</a>';
      printTree($item['childs']);
      echo '</li>';
    } else {
      echo '<li><a href="open.php?id='.$item['id'].'">' . $item['title'].'</a></li>';
    }
  }
  echo '</ul>';
}
printTree($tree);
 



[Ovu poruku je menjao Br@nkoR dana 23.04.2011. u 18:22 GMT+1]
Banned - Not available
 
Odgovor na temu

Milan Kragujevic
Software Engineer

Član broj: 231903
Poruke: 2220
*.dynamic.isp.telekom.rs.

Sajt: https://milankragujevic.c..


+201 Profil

icon Re: Nested PHP/MySQL 2-Levels meni23.04.2011. u 13:23 - pre 158 meseci
Neće da radi...
Kaže
Code:
Fatal error: Cannot use string offset as an array in /var/www/index.php on line 64

Evo koda
Code (php):

$menu = mysql_query("SELECT id, parent, title FROM " . $prefix . "menu") or die ('Doslo je do greske prilikom prikazivanja podataka iz MySQL baze! <br/><b>' . mysql_error() . '</b>');

$items = mysql_fetch_assoc($menu);

mysql_close($connection);
/* !!!Konekcija na MySQL */



function makeTree(&$items) {
  $new = array();
  foreach ($items as &$item) {
    $new[$item['id']] = &$item;
  }
  foreach ($items as &$item) {
    $new[$item['parent']]['childs'][] = &$item;
  }
  return $new[0]['childs'];
}

$tree = makeTree($items);

function printTree($items) {
  echo '<li>';
  foreach($items as $item) {
    if(isset($item['childs']) AND count($item['childs']) > 0) {
      echo '<a href="stranica.php?stranicaID='.$item['url'].'&menuID='.$item['id'].'">' . $item['title'].'</a>';
      echo '<ul>';
      printTree($item['childs']);
      echo '</ul>';
      echo '</li>';
    } else {
      echo '<li><a href="stranica.php?stranicaID='.$item['url'].'&menuID='.$item['id'].'">' . $item['title'].'</a></li>';
    }
  }
  echo '</li>';
}
 

Ovo je linija 64 :
Code (php):
$new[$item['parent']]['childs'][] = &$item;


A print_r($items); daje
Code:

Array (
[id] => 1
[parent] => 0
[title] => O meni
)

Naravno, u bazi ima još podataka:
Code:

+----+--------+------------+
| id | parent | title      |
+----+--------+------------+
|  1 | 0      | O meni     |
|  2 | 1      | Biografija |
|  3 | 1      | CV         |
+----+--------+------------+


[Ovu poruku je menjao Milan Kragujevic dana 23.04.2011. u 14:58 GMT+1]
 
Odgovor na temu

Br@nkoR
http://localhost

Član broj: 2597
Poruke: 1603

Sajt: localhost


+23 Profil

icon Re: Nested PHP/MySQL 2-Levels meni23.04.2011. u 16:25 - pre 158 meseci
Umesto
Code (php):
$items = mysql_fetch_assoc($menu);


Stavi npr.
Code (php):

$items = array();
while($i = mysql_fetch_assoc($menu))
$items[] = $i;
 

Banned - Not available
 
Odgovor na temu

Milan Kragujevic
Software Engineer

Član broj: 231903
Poruke: 2220
*.dynamic.isp.telekom.rs.

Sajt: https://milankragujevic.c..


+201 Profil

icon Re: Nested PHP/MySQL 2-Levels meni23.04.2011. u 16:34 - pre 158 meseci
Hvala, radi!
 
Odgovor na temu

[es] :: PHP :: Nested PHP/MySQL 2-Levels meni

[ Pregleda: 1835 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.