Posted By

jakepaint on 07/23/07


Tagged

menu php dynamic expanding


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

basicmagic
zingo
kobylinski
vali29
JimiJay
LordBumpet
nb109
ninjacharlee


Dynamic PHP Menu


 / Published in: PHP
 

URL: www.jakepaint.com/menufun/

This menu reads a directory and spits out the contents as an expanding menu. Great for a dynamic site with quick changing content.

  1. <?php
  2. function getfileinfo($thedirectory) {
  3. $files = scandir($thedirectory);
  4.  
  5. $namelisting = str_replace(".txt","",$files);
  6. //THIS TAKES OFF THE FILENAME EXTENSION FOR A CLEANER MENU. THIS IS FOR .txt files
  7. unset($namelisting[0], $namelisting[1]);
  8. //THIS REMOVES THOSE 1ST TWO PESKY DOTS THAT SHOW UP " . " & " .. "
  9. return($namelisting);
  10. ?>
  11.  
  12. //THIS IS THE DYNAMIC MENU
  13. <ul>
  14. <li><a href="breakfast.php">Breakfast</a>
  15. <?php
  16. if ($page == "breakfast") {
  17. echo '<ul>';
  18. $breakfastitems = getfileinfo("content/breakfast/");
  19. foreach ($breakfastitems as $value) {
  20. echo '<li><a href="'.$_SERVER['PHP_SELF'].'?food='.$value.'">'.$value.'</a></li>';
  21. }
  22. echo '</ul>';
  23. }
  24. echo '</li>';
  25. ?>
  26. </li>
  27.  
  28. <li><a href="lunch.php">Lunch</a>
  29. <?php
  30. if ($page == "lunch") {
  31. echo '<ul>';
  32. $lunchitems = getfileinfo("content/lunch/");
  33. foreach ($lunchitems as $value) {
  34. echo '<li><a href="'.$_SERVER['PHP_SELF'].'?food='.$value.'">'.$value.'</a></li>';
  35. }
  36. echo '</ul>';
  37. }
  38. ?>
  39. </li>
  40. </ul>
  41.  
  42. //THIS GOES INTO THE PAGE
  43. //ASSUMING THE .TXT FILES ARE IN /CONTENT dir
  44. <?php
  45. if (isset($_GET['food'])) {
  46.  
  47. //ADD THE EXTENSION BACK ON
  48.  
  49. $pagecontent = "content/" . $page . "/" . $_GET['food'] . ".txt";
  50.  
  51. include($pagecontent);
  52.  
  53. } else {
  54. echo 'I like to eat breakfast.';
  55. }
  56. ?>

Report this snippet  

You need to login to post a comment.