Forum Moderators: coopster
Basically I have an array called sidebar_{name-of-the-section-goes-here}, which has a list of Items that need to be added to the sidebar navigation at the left of the page.
So, I created a sidebar that will generate a UL list with all the LI elements according to which section of the site we're in. The URL parameter ?section=home, for instance, would tell the sidebar that it needs to have all items from the $sidebar_home array. Perfect!
The problem I have is that I don't seem to be able to name dynamic variables properly. Here's a simpler version of my code:
<?php$sub_section = $_GET['section'];
for ($i = 0; $i < 5; $i++) {
$sidebar_var_name = "sidebar_" . $sub_section[$i];
echo $$sidebar_var_name . "<br />";
}
?>
Ok, this is just a simpler version, with no UL or LI tags, but you get the idea.
I don't know what's wrong with my syntax and why this isn't working. Any ideas? Thank you!
$sidebar_var_name = "sidebar_w";
echo $$sidebar_var_name . "<br />";
echo $sidebar_w . "<br />";
<?php// If $my_section = "Home".
$my_section = "home";for ($i = 0; $i < 10; $i++) {
$my_sidebar_var = "sidebar_" . $my_section;
$sidebar_item = ${$my_sidebar_var}[$i];echo "<li>" . $sidebar_item . "</li>" . "<br />";
}?>
So basically now the loop will call everytime the variable $sidebar_home[$1], which can be $sidebar_home[0], $sidebar_home[1], $sidebar_home[2], $sidebar_home[3] till... $sidebar_home[10]. Thanks for the help guys.