开发者

PHP Code efficiency in menu

I currently have some VERY long winded code for the menu I use on my site. My website It's almost 2000 lines long lol. I think I may be able to use a switch but I've tried and cannot implement it to work properly.

On each menu when you click a button it stays highlighted, telling the user that they are on that page. The only way I could get this to work was like so...

if($subject == 'art') {
echo '<div id="spacer2"><br></div>';
echo '<div class="idName2" id="menu2">';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=all&listtype='.$listtype.'">All</a>';
echo '<div id="spacer2"><br></div>';
echo '</div>开发者_如何学Go';
echo '<div class="idName3" id="menu3">';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=art&listtype='.$listtype.'">Art</a>';
echo '</div>';
echo '<div class="idName2" id="menu2">';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=biology&listtype='.$listtype.'">Biology</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=english&listtype='.$listtype.'">English</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=chemistry&listtype='.$listtype.'">Chemistry</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=mathematics&listtype='.$listtype.'">Mathematics</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=history&listtype='.$listtype.'">History</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=religion&listtype='.$listtype.'">Religion</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=geography&listtype='.$listtype.'">Geography</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=music&listtype='.$listtype.'">Music</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=philosophy&listtype='.$listtype.'">Philosophy</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=psychology&listtype='.$listtype.'">Psychology</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=economics&listtype='.$listtype.'">Economics</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=sociology&listtype='.$listtype.'">Sociology</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=technology&listtype='.$listtype.'">Technology</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=electronics&listtype='.$listtype.'">Electronics</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=food&listtype='.$listtype.'">Food</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=law&listtype='.$listtype.'">Law</a>';
echo '<a href="browse.php?alphabet='.$alphabet.'&subject=politics&listtype='.$listtype.'">Politics</a>';
echo '</div>';
}

elseif($subject == 'biology') {

and then there's however many 'elseifs' as there are menu items, which ends up totalling to 2000 lines of code which is obviously very inefficient and it also makes it unbelievably time consuming to change anything... can someone point me in the right direction in what I need to do please!


Navigation is always a pain. Here is a simple solution that might help.

Put everything in an array, like this:

$menu = array(
    'All' => '/browse ...',
    'Art' => '/browse ... art',
    'Biology' => '/browse ... biology',
    // etc.
)

Then, you can build all the links based on the current subject:

$subject = 'Art';

foreach ($menu as $title => $url) {
    if ($title == $subject) {
        echo "<b>$title</b><br>\n";
    } else {
        echo "<a href=\"$url\">$title</a><br>\n";
    }
}

This is a rather simplistic solution, but it can be extended to create a more complicated menus structure.

Another solution you could look at is Zend_Navigation. http://framework.zend.com/manual/en/zend.navigation.introduction.html


What about something like:

$subjects = array('art', 'biology', 'english', 'chemistry' /*, etc */);
foreach ($subjects as $current_subject)
{
    if ($current_subject == $subject)
    {
        //write it the id="menu3" way
    }
    else
    {
        //write it another way
    }
}

You can transform 'biology' to 'Biology' using the ucfirst function. Also note that there can't be two or more elements with the same ID (menu2 in your case).


You should use an array to represent your menus. Even better, you could place it in a resource file (xml would be preferable), but explaining that exceeds the scope of this answer (nonetheless, you should look into it). Using an external resource for the menu has the added benefit that you can change your menu structure without modifying any code.

Sticking to the simple approach, this is how you should represent your menu without an external resource:

$menus = array('Art', 'Biology', 'English', ...);

You can generate the "A"-"Z" submenus with a simple range command: range('A', 'Z')

So your code echoing the menus would be something like:

<?php
  $current_letter = $_REQUEST['letter'];
  $current_menu = $_REQUEST['menu'];
  $letters = range('A', 'Z');

  foreach($letters as $letter) :
    $class = $current_letter == $letter ? 'class="active"' : '';
?>
  <a <?php echo $class; ?> href="browse.php?letter=<?php echo $letter; ?>&menu=<?php echo $current_menu; ?>&listtype=<?php echo $listtype"><?php echo $letter; ?></a>

<?php endforeach; ?>

This displays the top menubar (A-Z links). For the side menu, here is how to display your categories:

<?php    
  foreach($menus as $menu) :
    $class = $current_menu == $menu ? 'class="active"' : '';
?>
  <a <?php echo $class; ?> href="browse.php?letter=<?php echo $current_letter; ?>&menu=<?php echo $menu; ?>&listtype=<?php echo $listtype"><?php echo $menu; ?></a>

<?php endforeach; ?>

Haven'tested it, but apart from possible typos this approach should work for you.

Btw, please don't throw things at me for not validating input and using $_REQUEST directly. It should be done, but that is a whole other topic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜