Jump to content

Spry Menu and MySQL


bravo14

Recommended Posts

In dreamweaver CS3 there are some Spry items and when they get added to a page the code looks like

 

<ul id="MenuBar1" class="MenuBarVertical">
  <li><a class="MenuBarItemSubmenu" href="#">Item 1</a>
      <ul>
        <li><a href="#">Item 1.1</a></li>
        <li><a href="#">Item 1.2</a></li>
        <li><a href="#">Item 1.3</a></li>
      </ul>
  </li>
  <li><a href="#">Item 2</a></li>
  <li><a class="MenuBarItemSubmenu" href="#">Item 3</a>
      <ul>
        <li><a class="MenuBarItemSubmenu" href="#">Item 3.1</a>
            <ul>
              <li><a href="#">Item 3.1.1</a></li>
              <li><a href="#">Item 3.1.2</a></li>
            </ul>
        </li>
        <li><a href="#">Item 3.2</a></li>
        <li><a href="#">Item 3.3</a></li>
      </ul>
  </li>
  <li><a href="#">Item 4</a></li>
</ul>

Link to comment
https://forums.phpfreaks.com/topic/146716-spry-menu-and-mysql/#findComment-770309
Share on other sites

At present this is how the table looks with the menu options in, if there is a better way then please let me know

 

menu_id  menu  parent_menu_id 

      1      Admin              0

      2      1st XI            0

      3      Fathoms          0 

      4      Badgers          0

      5      Results            2

      6      Reports            2

      7      Committee        1

      8      Selection Policy 1

 

There may be a better way, may be two tables, but I am not sure, any advice much appreciated

Link to comment
https://forums.phpfreaks.com/topic/146716-spry-menu-and-mysql/#findComment-770359
Share on other sites

try

<?php
function build_menu($data, $id=0, $pre='  '){
foreach ($data[$id] as $row){
	if (isset($data[$row['menu_id']])){
		echo "$pre<li><a class=\"MenuBarItemSubmenu\" href=\"#\">".$row[menu]."</a>\n";
		echo "$pre  <ul>\n";
		build_menu($data, $row['menu_id'], $pre.'    ');
		echo "$pre  </ul>\n$pre</li>\n";
	} else echo "$pre<li><a href=\"#\">$row[menu]</a></li>\n";
}
}
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('test');
$res = mysql_query('SELECT * FROM menus') or die(mysql_error());
while ($row = mysql_fetch_assoc($res)) $data[$row['parent_menu_id']][] = $row;
//print_r($data);
echo '<ul id="MenuBar1" class="MenuBarVertical">',"\n";
build_menu($data);
echo '</ul>';
?>

Link to comment
https://forums.phpfreaks.com/topic/146716-spry-menu-and-mysql/#findComment-770432
Share on other sites

  • 1 month later...

is there any way in which you can comment the code so that I can se what has been done

 

<?php
// create build menu function
function build_menu($data, $id=0, $pre='  '){
// for every bit of data, call it $row
foreach ($data[$id] as $row){
// if $data[$row['menu_id']] exists...
	if (isset($data[$row['menu_id']])){
// then do this...
// display hyperlink/s in a list
		echo "$pre<li><a class=\"MenuBarItemSubmenu\" href=\"#\">".$row[menu]."</a>\n";
// build the menu
		echo "$pre  <ul>\n";
		build_menu($data, $row['menu_id'], $pre.'    ');
		echo "$pre  </ul>\n$pre</li>\n";
// if $data[$row['menu_id']] doesn't exist...
// just show the rest of the hyperlinks in a list
	} else echo "$pre<li><a href=\"#\">$row[menu]</a></li>\n";
}
}
// connect to database
mysql_connect('localhost', 'root', '') or die(mysql_error());
// select the database
mysql_select_db('test');
// select menu items from the `menus` table
$res = mysql_query('SELECT * FROM menus') or die(mysql_error());
// pull table records until there is none and assign them to arrays
while ($row = mysql_fetch_assoc($res)) $data[$row['parent_menu_id']][] = $row;
//print_r($data);
// build the multi layer menu
echo '<ul id="MenuBar1" class="MenuBarVertical">',"\n";
build_menu($data);
echo '</ul>';
?>

Link to comment
https://forums.phpfreaks.com/topic/146716-spry-menu-and-mysql/#findComment-809676
Share on other sites

Hi Everyone,

 

Using the above example, how shall i proceed in adding links to the "nodes" of the menu so that when i click on "admin" i would get redirected to admin.php etc etc...?

 

For example:

 

menu_id      menu               parent_menu_id            LINKS 
      1      Admin              0                         admin.php
      2      1st XI             0                         1st.php
      3      Fathoms            0                         fathoms.php
      4      Badgers            0                         etc.php
      5      Results            2                         etc.php

 

Thanks in advance!  ???

Link to comment
https://forums.phpfreaks.com/topic/146716-spry-menu-and-mysql/#findComment-811979
Share on other sites

Hi Everyone,

 

Using the above example, how shall i proceed in adding links to the "nodes" of the menu so that when i click on "admin" i would get redirected to admin.php etc etc...?

 

For example:

 

menu_id      menu               parent_menu_id            LINKS 
      1      Admin              0                         admin.php
      2      1st XI             0                         1st.php
      3      Fathoms            0                         fathoms.php
      4      Badgers            0                         etc.php
      5      Results            2                         etc.php

 

Thanks in advance!  ???

 

Manage to solve this one now.

 

<?php
// create build menu function
function build_menu($data, $id=0, $pre='  '){
// for every bit of data, call it $row
   foreach ($data[$id] as $row){
// if $data[$row['menu_id']] exists...
      if (isset($data[$row['menu_id']])){
// then do this...
// display hyperlink/s in a list
         echo "$pre<li><a class=\"MenuBarItemSubmenu\" href=\"$row[link]\">".$row[menu]."</a>\n";
// build the menu
         echo "$pre  <ul>\n";
         build_menu($data, $row['menu_id'], $pre.'    ');
         echo "$pre  </ul>\n$pre</li>\n";
// if $data[$row['menu_id']] doesn't exist...
// just show the rest of the hyperlinks in a list
      } else echo "$pre<li><a href=\"$row[link]\">$row[menu]</a></li>\n";
   }
}
// connect to database
mysql_connect('localhost', 'root', '') or die(mysql_error());
// select the database
mysql_select_db('test');
// select menu items from the `menus` table
$res = mysql_query('SELECT * FROM menus') or die(mysql_error());
// pull table records until there is none and assign them to arrays
while ($row = mysql_fetch_assoc($res)) $data[$row['parent_menu_id']][] = $row;
//print_r($data);
// build the multi layer menu
echo '<ul id="MenuBar1" class="MenuBarVertical">',"\n";
build_menu($data);
echo '</ul>';
?>

 

If the column added to the menu table was called 'link' then all i had to do was add $row[link] to the href statement.

 

Link to comment
https://forums.phpfreaks.com/topic/146716-spry-menu-and-mysql/#findComment-811993
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.