0

I am new to php and i am trying to make a dinamic menu ( if i add a php file to the directory to be automaticaly added to the menu) my issue is that i want to exclude 3 specific pages from there like app.php, mdx.php and script.php this is the code i am using to get the menu up and running :

<?php
$dir = ".";
$htmlFiles = glob("$dir/*.{html,htm,php}", GLOB_BRACE);

// Sort in ascending order - this is default
echo '<ul>';
foreach($htmlFiles as $htmlFile)
{
  echo '<li><a href="'.basename($htmlFile).'">'.mb_strtoupper(basename($htmlFile,".php")).'</a></li>';

}
echo '</ul>';

?>

How do i do that ?

  • Does this answer your question? [Deleting an element from an array in PHP](https://stackoverflow.com/questions/369602/deleting-an-element-from-an-array-in-php) – Simondebn Feb 01 '20 at 17:51

3 Answers3

0

First of all, I would put the PHP scripts that are not visible or very important, i.e. the main PHP scripts, in a different folder.

I would also put the PHP scripts that should influence the menu into another folder.

e.g.

app/app.php
app/mdx.php
app/script.php
menu/menu1.php
menu/menu2.php
menu/menu3.php

$dir = "../menu/";
$htmlFiles = glob("$dir/*.{html,htm,php}", GLOB_BRACE);

Or look at this answer: https://stackoverflow.com/a/12284228/4173464

db1975
  • 775
  • 3
  • 9
  • "Or look at this answer: https://stackoverflow.com/a/12284228/4173464" That aproach only lets me substract only one item from the array and i need multiple items to be substracted. – Dorin Crisitan Feb 01 '20 at 17:55
0

Maybe something like that?

<?php
$dir = ".";
$cutFiles = ['app.php', 'mdx.php','script.php']; //file You don't want 
$htmlFiles = glob("$dir/*.{html,htm,php}", GLOB_BRACE);

$htmlFiles = array_diff($html_Files, $cutFiles); //computed differences

// Sort in ascending order - this is default
echo '<ul>';
foreach($htmlFiles as $htmlFile)
{
  echo '<li><a href="'.basename($htmlFile).'">'.mb_strtoupper(basename($htmlFile,".php")).'</a></li>';

}
echo '</ul>';

?>
0

The answer of 'Pinks Not Dead' was pretty close thank you very much! What I was trying to achieve is to make a menu that is positioned in a logical order so i could not use asort... that solved my issue thank you!

<?php
$dir = ".";
$cutFiles = ['./index.php','./Contact.php','./About.php']; //files You don't want
$htmlFiles = glob("$dir/*.{html,htm,php}", GLOB_BRACE);

$htmlFiles = array_diff($htmlFiles, $cutFiles); //computed differences

// Sort in ascending order - this is default
echo '<ul>';
echo '<li><a href="index.php">HOME</a></li>';
foreach($htmlFiles as $htmlFile)
{
  echo '<li><a href="'.basename($htmlFile).'">'.mb_strtoupper(basename($htmlFile,".php")).'</a></li>';

}
echo '<li><a href="About.php">ABOUT US</a></li>';
echo '<li><a href="Contact.php">CONTACT</a></li>';
echo '</ul>';

?>