tylerrowsell Posted February 2, 2012 Share Posted February 2, 2012 First of all I am very new to php. I have coded in asp for years but have made the switch so I kinda know what I want to do but I am unsure on how to achieve it. I have a css menu that I am trying to populate through a database. The parent tabs are in a standard list. The first child submenu is in sepearate unordered list and anymore child tabs are within that list. Like this. <div id="ddtopmenubar" style="width: 960px"> <ul> <li><a href="?page=">HOME </a></li> <li><a href="?page=About" rel="">ABOUT </a></li> <li><a href="?page=Services" rel="SERVICES">SERVICES </a></li> <li><a href="?page=Governance" rel="GOVERNANCE">GOVERNANCE </a></li> <li><a href="?page=Events">EVENTS </a></li> <li><a href="?page=Contact">CONTACT </a></li> </ul> <ul id="SERVICES" class="ddsubmenustyle"> <li><a href="?page=Health">Health/Dental</a></li> <li><a href="?page=Identification">International Student Identification Card</a></li> <li><a href="?page=Employment">Student Employment</a></li> <li><a href="?page=Saver">Student Saver</a></li> <li><a href="#">Other Services</a> <ul> <li><a href="#">Emergency Student Loan Fund</a></li> <li><a href="#">Legal Services</a></li> <li><a href="#">Parking Permits</a></li> <li><a href="#">Homes4Students</a></li> </ul> </li> </ul> <ul id="GOVERNANCE" class="ddsubmenustyle"> <li><a href="?page=Council">Your Council</a></li> <li><a href="?page=Executive">Executive</a></li> <li><a href="?page=Committees">Committees</a></li> <li><a href="?page=Constitution">Constitution/By-Laws</a></li> <li><a href="?page=Minutes">Minutes</a></li> <li><a href="?page=Elections">Elections</a></li> </ul> </div> I am trying to populate this from a database list the results come ordered like this 1.0 2.0 3.0 3.1 3.2 3.3 3.4 3.4-1 3.4-2 3.4-3 3.4-4 4.0 4.1 4.2 4.3 4.4 4.5 4.6 5.0 6.0 where the "." show the 1st childs and the "-" show the 2nd child I tried to do this by splting each line and determining if it is a zero or not if it was a zero i put that paticular one into an array to be used in the sub menu however this did not work out. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/256224-menu-sorting/ Share on other sites More sharing options...
Proletarian Posted February 2, 2012 Share Posted February 2, 2012 Where's your PHP code for the retrieving, sorting, and displaying? Quote Link to comment https://forums.phpfreaks.com/topic/256224-menu-sorting/#findComment-1313606 Share on other sites More sharing options...
tylerrowsell Posted February 2, 2012 Author Share Posted February 2, 2012 I have tried a few things but nothing has quite worked. This is what I would try to do if I knew how to go about doing it. <?php $con = mysql_connect("localhost","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT * FROM Pages ORDER BY menuid ASC"); if (!$result) { // add this check. die('Invalid query: ' . mysql_error()); } while($row = mysql_fetch_array($result)){ $menu = explode(".",$row['menuid']); if(menu[1]="0"){ $checkvar=menu[1]; echo("<li>".$row['Title']."</li>"); }else{ //put $fow['Title'] into an array named the menu[0] value //count how many arrays there are } } //print the arrays in <ul></ul> ?> Quote Link to comment https://forums.phpfreaks.com/topic/256224-menu-sorting/#findComment-1313836 Share on other sites More sharing options...
tylerrowsell Posted February 4, 2012 Author Share Posted February 4, 2012 In case someone in the future needs this I have figured it out. <div id="ddtopmenubar" class="mattblackmenu" style="width: 960px"> <ul><?php $i=0; $con = mysql_connect("localhost","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT * FROM Pages ORDER BY menuid ASC"); if (!$result) { // add this check. die('Invalid query: ' . mysql_error()); } while($row = mysql_fetch_array($result)){ $menu = explode(".",$row['menuid']); if($menu[1]=="0"&&$menu[0]!="0"){ ?> <li><a rel="<?php echo($menu[0])?>"><?php echo($row['Name'])?></a></li> <? $countarray[$i]=$menu[0]; $i++; }else if($menu[1]==""&&$menu[0]!="0"){ ?> <li><a href="?page=<?php echo($row['Name'])?>" ><?php echo($row['Name'])?></a></li> <?php } }?> </ul> <script type="text/javascript"> ddlevelsmenu.setup("ddtopmenubar", "topbar") //ddlevelsmenu.setup("mainmenuid", "topbar|sidebar") </script> <?php for ($j=0; $j<$i; $j++){ ?> <ul id="<?php echo($countarray[$j])?>" class="ddsubmenustyle"> <?mysql_data_seek($result, 0); // set the pointer of the result set back to the beginning. while($row = mysql_fetch_assoc($result)) { $menu = explode(".",$row['menuid']); if($menu[0]==$countarray[$j]&& $menu[1]!="0"){ ?> <li><a href="?page=<?php echo($row['Name'])?>" ><?php echo($row['Name'])?></a></li> <?php } } ?></ul> <?php } ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/256224-menu-sorting/#findComment-1314267 Share on other sites More sharing options...
Drummin Posted February 4, 2012 Share Posted February 4, 2012 Hey that's cool that you got it working. Though I doubt I would personally store the menu positions in one field, (I would use id and parent_id) but never the less found it an interesting challenge and worked on it some last night but didn't get it fully working. Glad you figured it out. Quote Link to comment https://forums.phpfreaks.com/topic/256224-menu-sorting/#findComment-1314273 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.