TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   My dynamic menu (http://www.talkphp.com/advanced-php-programming/2009-my-dynamic-menu.html)

lesP 01-19-2008 10:06 AM

My dynamic menu
 
Hi there!

I have written a dynamic menu. In one tabel I have the titles of the menu. Ex: Home & shop etc.

In another table I have the subtitles. Ex: When you click on shop, all the subtitles connected to shop should appear right underneath the title. Like this:

Home
Shop(active)
Good shop
Medium Shop
Bad shop
Contact

I think you understand. I connect those subtitles and titles with a id_parent, so all subtitles have the same id_parent as their respective title. But instead of the subtitles just appearing right under its title, it appears under every title in the menu. It extracts the right subtitles from the database, but print them under all titles. I think the problem lies in, how the udskriv_menu(); is chosen, but I do not know how to solve it...

Here is my title:

php Code:
$query=mysql_query("select * from a_spil order by titel asc");
while ($result=mysql_fetch_array($query)){



           echo '<div style="width:100%;height:19px;_height:24px;padding-top:5px;_padding-top:1px;">';
  echo '<div style="float:left;display:inline;" style="margin-top:4px;">';
   echo '<a href="index.php?mode='.$result[mode].'&mode2=spil&action=vis_spil&spil='.$result[id].'&overordnet='.$result[id_parent].'" title="Vuelta a Espaņa" style="margin:4px 0px 0px 17px;" class="menupunkt">';
    echo $result[titel];
   echo '</a>';
  echo '</div>';
  echo '<div style="float:right;margin:5px 0px 0px 0px;_margin:9px 0px 0px 0px;"><img src="grafik/menupunkt_pil.gif" style="margin-right:16px;"></div>';
 echo '</div>';
 echo '<div style="height:1px;width:100%;"><img src="grafik/menupunkt_skillelinie.gif" alt="" /></div>';
// If any title has been chosen, now the subtitles will appear in this form.
//overordnet is the id_parent of the title

if ($_REQUEST['overordnet']=="3333"){
 udskriv_menu();
// echo '<div style="height:1px;width:100%;"><img src="grafik/menupunkt_skillelinie.gif" alt="" /></div>';

 }
}
Here is my function:
php Code:
function udskriv_menu(){

 $aquery=mysql_query("select * from a_kategorier where id_parent='$_REQUEST[overordnet]' order by sortering asc");
 while($result=mysql_fetch_array($aquery)){
   $aquery2=mysql_query("select * from a_spil where id_parent='$id_parent'");
while ($result2=mysql_fetch_array($aquery2)){

  echo '<div style="width:100%;height:19px;_height:24px;padding-top:5px;_padding-top:1px;">';
   echo '<div style="float:left;display:inline;" style="margin-top:4px;">';
    echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="index.php?mode='.$_REQUEST['mode'].'&mode2='.$result["struktur"].'&action=vis_'.$result["action2"].'&spil='.$result2["id"].'&id='.$result["id"].'" title="'.$result["titel"].'" style="margin:4px 0px 0px 17px;" class="menupunkt">';
     echo $result["titel"];
    echo '</a>';
   echo '</div>';
   echo '<div style="float:right;margin:5px 0px 0px 0px;_margin:9px 0px 0px 0px;"><img src="grafik/menupunkt_pil.gif" style="margin-right:16px;"></div>';
  echo '</div>';
  echo '<div style="height:1px;width:100%;"><img src="grafik/menupunkt_skillelinie.gif" alt="" /></div>';
  $nummer++;
  $antal_emner='0';
  udskriv_undermenu($result["id"],1);

}
}
}

lesP 01-19-2008 10:09 AM

Oops. I didnt see this "post code" function. Should I post me code in such one?

xenon 01-19-2008 11:18 AM

I can't see a reason why you couldn't.

Advice: In the future, try to separate the HTML from the PHP as much as possible. Don't embed large blocks of html into echo/print statements. It's hard to read/debug and especially hard to change the presentation if you needed to do so.

lesP 01-19-2008 11:28 AM

Quote:

Originally Posted by xenon (Post 8945)
I can't see a reason why you couldn't.

Couldn't what?

What happens is that when a title is "clicked", the menu is shown like this:

Home
Good shop
medium shop
bad shop
Shop
Good shop
medium shop
bad shop

And this should only be under shop.

xenon 01-19-2008 02:11 PM

I meant shouldn't.

Now, for the problem. I don't really understand what you're trying to pull there, but here's an idea of what it should look like:

PHP Code:

// go through and display the categories
while( $category mysql_fetch... )
{
    echo 
$category['name'];

    
// if this category is selected, print its subs
    
if( isset( $_GET['category'] ) && $_GET['category'] == $category['id'] )
    {
        while( 
$subcategory mysql_fetch... )
        {
            echo 
$subcategory['name'];
        }
    }



lesP 01-21-2008 09:44 PM

Nice. Except it does not do the $subcategory

xenon 01-21-2008 09:53 PM

woot?

Of course, if you needed limitless categories and subs, you would write a recursive function to do just that. Or use the MySQL way, as pointed out around here somewhere (nice approach, btw).

lesP 01-21-2008 10:28 PM

Can you show with some code?


All times are GMT. The time now is 09:22 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0