Populate menu from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Populate menu from database

Post by Jim_Bo »

Hi,

I am tryin to populate a menu like so:

Code: Select all

div id="masterdiv">

<div class="menutitle" onClick="SwitchMenu('sub1')">Shorts</font></div>
<span class="submenu" id="sub1">
- <a href="Shorts">Shorts</a><br>
- <a href="Shorts">Long Shorts</a><br>
</span>
<div class="menutitle" onClick="SwitchMenu('sub2')">T-Shirts</div>
<span class="submenu" id="sub2">
- <a href="T-Shirts">Iron Maiden</a><br>
- <a href="T-Shirts">ACDC</a><br>
</span>

</div>
from 2 tables .. I have tried tones of ways to make it work properly but no go ..

Here is what I have at the moment:

Code: Select all

echo "<div id=\"masterdiv\">";

$sql = mysql_query("SELECT m.*, s.* 
FROM maincat as m 
LEFT JOIN subcat as s 
ON m.mainid = s.catid 
ORDER BY m.mainid ASC") or die(mysql_error()); 

while($row = mysql_fetch_array($sql)){ 
$mainid = $row['mainid']; 
$catname = $row['catname']; 
$name = $row['name']; 
$link = $row['link']; 

echo "<div class=\"menutitle\" onClick=\"SwitchMenu('sub$mainid')\">$catname</div>"; 
echo "<span class=\"submenu\" id=\"sub$mainid\">"; 
echo "- <a href=\"$link\">$name</a><br>"; 
echo "</span>"; 
} 


echo "</div>";
But thats messing it up and outputting html like so:

Code: Select all

<div id="masterdiv">

<div class="menutitle" onClick="SwitchMenu('sub1')">T-Shirts</div>
<span class="submenu" id="sub1">- <a href="shirts">Metalica</a><br>
</span>
<div class="menutitle" onClick="SwitchMenu('sub1')">T-Shirts</div>
<span class="submenu" id="sub1">- <a href="shirt">Iron Maiden</a><br>
</span>
<div class="menutitle" onClick="SwitchMenu('sub1')">T-Shirts</div>
<span class="submenu" id="sub1">- <a href="eminem">Eminem</a><br>
</span>
<div class="menutitle" onClick="SwitchMenu('sub2')">Pants</div>
<span class="submenu" id="sub2">- <a href="jeans">Jeans</a><br>
</span>
<div class="menutitle" onClick="SwitchMenu('sub2')">Pants</div>
<span class="submenu" id="sub2">- <a href="shorts">Long Shorts</a><br>
</span>
<div class="menutitle" onClick="SwitchMenu('sub3')">Jackets</div>
<span class="submenu" id="sub3">- <a href="">Waist Coat</a><br>
</span>

</div>
Its listing a new heading for each record found in subcat and only listing one subcat product under each maincat ..

How should this be done?

Thanks
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

I hope you'll don't mind, i entered few changes:

Code: Select all

echo '<div id="masterdiv">';

$sql = mysql_query("SELECT m.*, s.*
FROM maincat as m
LEFT JOIN subcat as s
ON m.mainid = s.catid
ORDER BY m.mainid ASC") or die(mysql_error());

while($row = mysql_fetch_array($sql)){
        extract($row);

echo '<div class="menutitle" onClick="SwitchMenu('sub'.$mainid.'')">'.$catname.'</div> \n
<span class="submenu" id=" sub'.$mainid.'"> \n
- <a href="'.$link.'">'.$name.'</a><br> \n
</span> \n';
}


echo '</div>';
The biggest change is the "extract" function, i won't explain about her, but the manul will :)

http://php.bet/extract

What i change is the quoting from [""] to [''], now you don't need to use slashes.

Another change is the " \n" witch means "<br />" but only in the Php \ Html source.

Hope i helped, Tal.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Thanks for your efforts ..

But it creates the same kinda effect that I was getting before ..

There was a syntax error in line:

echo '<div class="menutitle" onClick="SwitchMenu(sub'.$mainid.')">'.$catname.'</div> \n

The code creates the same effect except that nw it has \n\n under each maincat heading ..


any other ideas?

P.s Thanks for the extract($row); function ... can see it saving me some tying


Thanks
Last edited by Jim_Bo on Wed Dec 28, 2005 2:31 pm, edited 1 time in total.
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

Say, what you're trying to do is to make an order in the Html source?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

In the maincat table there are 3 records

T-Shirts
Pants
Jackets

in the subcat table under T-Shirts there are 3 records, 2 under Pants and 1 under Jackets

As you can see it is listing a heading for each record found under the subcat listings .. Rather than one heading for each maincat and listing the subcats under when a particular maincat is clicked on ..


Thanks
Last edited by Jim_Bo on Wed Dec 28, 2005 9:23 pm, edited 1 time in total.
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

Ohh, Look, in this sitation there can be couple of reasonse:

* The query got wrong syntex
* The id already exists

so what can you do?

* Write a simple query and check if this problem is happening again
* Make sure that ID field is the "Primary key" and got "auto_increment" extra.

hope it helps, Tal.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

See here is the generated html:

Code: Select all

<div id="masterdiv"> 

<div class="menutitle" onClick="SwitchMenu('sub1')">T-Shirts</div> 
<span class="submenu" id="sub1">- <a href="shirts">Metalica</a><br> 
</span> 
<div class="menutitle" onClick="SwitchMenu('sub1')">T-Shirts</div> 
<span class="submenu" id="sub1">- <a href="shirt">Iron Maiden</a><br> 
</span> 
<div class="menutitle" onClick="SwitchMenu('sub1')">T-Shirts</div> 
<span class="submenu" id="sub1">- <a href="eminem">Eminem</a><br> 
</span> 
<div class="menutitle" onClick="SwitchMenu('sub2')">Pants</div> 
<span class="submenu" id="sub2">- <a href="jeans">Jeans</a><br> 
</span> 
<div class="menutitle" onClick="SwitchMenu('sub2')">Pants</div> 
<span class="submenu" id="sub2">- <a href="shorts">Long Shorts</a><br> 
</span> 
<div class="menutitle" onClick="SwitchMenu('sub3')">Jackets</div> 
<span class="submenu" id="sub3">- <a href="">Waist Coat</a><br> 
</span> 

</div>
Now what is should be generating is:

Code: Select all

<div id="masterdiv"> 

<div class="menutitle" onClick="SwitchMenu('sub1')">T-Shirts</div> 
<span class="submenu" id="sub1">- <a href="shirts">Metalica</a><br> 
<span class="submenu" id="sub1">- <a href="shirt">Iron Maiden</a><br>
<span class="submenu" id="sub1">- <a href="eminem">Eminem</a><br>
</span> 
<div class="menutitle" onClick="SwitchMenu('sub2')">Pants</div> 
<span class="submenu" id="sub2">- <a href="jeans">Jeans</a><br> 
<span class="submenu" id="sub2">- <a href="shorts">Long Shorts</a><br>
</span> 
<div class="menutitle" onClick="SwitchMenu('sub3')">Jackets</div> 
<span class="submenu" id="sub3">- <a href="">Waist Coat</a><br> 
</span> 

</div>
Can you see the problem in how it is generating a new main heading for each subcat found?


Thanks
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

Now i know your problem, you need 2 while loops, one who will take the div, and the other for the menu content, it very simple, try it your self :)
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Oh I have ... Heaps of ways ..

The problem is that it breaks up the <span></span> when u shift the loop ..

Beleave me I have tried heaps of ways to make it work ... :(


Thanks
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

Code: Select all

echo '<div id="masterdiv">';

$sql = mysql_query("SELECT m.*, s.*
FROM maincat as m
LEFT JOIN subcat as s
ON m.mainid = s.catid
ORDER BY m.mainid ASC") or die(mysql_error());

while($row = mysql_fetch_array($sql)){
        extract($row);

echo '<div class="menutitle" onClick="SwitchMenu('sub'.$mainid.'')">'.$catname.'</div>';

while($row2 = mysql_fetch_array($sql)){
        extract($row2);

echo '
<span class="submenu" id=" sub'.$mainid.'">
- <a href="'.$link.'">'.$name.'</a><br>
</span>';

     }
}


echo '</div>';
Hope it will work..
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Fixed syntax errors .. but still not working correct:

Code: Select all

echo '<div id="masterdiv">'; 

$sql = mysql_query("SELECT m.*, s.* 
FROM maincat as m 
LEFT JOIN subcat as s 
ON m.mainid = s.catid 
ORDER BY m.mainid ASC") or die(mysql_error()); 

while($row = mysql_fetch_array($sql)){ 
        extract($row); 

echo '<div class="menutitle" onClick="SwitchMenu(sub'.$mainid.')">'.$catname.'</div>'; 

while($row = mysql_fetch_array($sql)){ 
        extract($row); 

echo '<span class="submenu" id="sub'.$mainid.'"> 
- <a href="'.$link.'">'.$name.'</a><br> 
</span>'; 

 } 
} 

echo '</div>';
Visit the link posted above to view the results ..

Generated html:

Code: Select all

<div id="masterdiv">
 <div class="menutitle" onClick="SwitchMenu(sub1)">T-Shirts</div>
 <span class="submenu" id="sub1"> - <a href="shirt">Iron Maiden</a><br>
 </span><span class="submenu" id="sub1"> - <a href="eminem">Eminem</a><br>
 </span><span class="submenu" id="sub2"> - <a href="jeans">Jeans</a><br>
 </span><span class="submenu" id="sub2"> - <a href="shorts">Long Shorts</a><br>
 </span><span class="submenu" id="sub3"> - <a href="">Waist Coat</a><br>
 </span>
</div>
Thanks
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

Code: Select all

echo '<div id="masterdiv">';

$sql = mysql_query("SELECT m.*, s.*
FROM maincat as m
LEFT JOIN subcat as s
ON m.mainid = s.catid
ORDER BY m.mainid ASC") or die(mysql_error());

$sql2 = mysql_query(Your query + where id = this div id);

while($row = mysql_fetch_array($sql)){
        extract($row);

echo '<div class="menutitle" onClick="SwitchMenu(sub'.$mainid.')">'.$catname.'</div>';

while($row2 = mysql_fetch_array($sql2)){
        extract($row2);

echo '<span class="submenu" id="sub'.$mainid.'">
- <a href="'.$link.'">'.$name.'</a><br>
</span>';

}
}

echo '</div>';

You need to fix the $sql2 and i think you're done :)
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

if you mean this:

Code: Select all

echo '<div id="masterdiv">'; 

$sql = mysql_query("SELECT m.*, s.* 
FROM maincat as m 
LEFT JOIN subcat as s 
ON m.mainid = s.catid 
ORDER BY m.mainid ASC") or die(mysql_error()); 

while($row = mysql_fetch_array($sql)){ 
        extract($row); 
		
$sql2 = mysql_query("SELECT * FROM subcat WHERE catid = '$mainid'"); 

echo '<div class="menutitle" onClick="SwitchMenu(sub'.$mainid.')">'.$catname.'</div>'; 

while($row2 = mysql_fetch_array($sql2)){ 
        extract($row2); 

echo '<span class="submenu" id="sub'.$mainid.'"> 
- <a href="'.$link.'">'.$name.'</a><br> 
</span>'; 

} 
} 

echo '</div>';
Still no go .. :(



Cheers
Last edited by Jim_Bo on Wed Dec 28, 2005 3:30 pm, edited 1 time in total.
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

i'm too tired right now, [in israel 11:30 [night], and i was all day in a party] so i can't think clearly right now [and i'm still don't talk about translating from hebrew to english]

This way is the right one, you need to "play" with you queries until you'll find the right one.

i'm going to my sweet bad :P

Good night, Tal.
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Thanks for yer efforts!

Working Ver:

Code: Select all

echo '<div id="masterdiv">'; 

$sql = mysql_query("SELECT * FROM maincat ORDER BY catname ASC") or die(mysql_error()); 

while($row = mysql_fetch_array($sql)){ 
        extract($row); 
		
$sql2 = mysql_query("SELECT * FROM subcat WHERE catid = '$mainid' ORDER BY name ASC"); 

    echo "<div class=\"menutitle\" onClick=\"SwitchMenu('sub$mainid')\">$catname</div>"; 
	echo "<span class=\"submenu\" id=\"sub$mainid\">"; 

while($row2 = mysql_fetch_array($sql2)){ 
        extract($row2); 

    echo "- <a href=\"$link\">$name</a><br>"; 
} 	
	echo "</span>"; 
} 

echo '</div>';

Thanks
Last edited by Jim_Bo on Wed Dec 28, 2005 3:59 pm, edited 1 time in total.
Post Reply