Jump to content

Menu Sorting.


tylerrowsell

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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