Question

I have a menu where I get the name of the files in a directory and put them in the menu. This is the code I use:

<ul>
    <?php   foreach(glob("overzicht/projects/{*.php,*.html}", GLOB_BRACE) as $file) {
                echo "<li><a>".pathinfo($file, PATHINFO_FILENAME)."</a></li>";
         } 
    ?>
</ul>

Now I want to get a link in the menu, so the li would be like this

<li><a href="filename.php"> filename </a></li>

I tried this but then it won't work

<ul>
    <?php   foreach(glob("overzicht/projects/{*.php,*.html}", GLOB_BRACE) as $file) {
                echo "<li><a href="".pathinfo($file, PATHINFO_FILENAME).".php">".pathinfo($file, PATHINFO_FILENAME)."</a></li>";
            } 
    ?>          
</ul>

What am I doing wrong or in what why should I do this?

edit

I also want the first letter of the menu to be a capital. Is this also possible to do?

Was it helpful?

Solution

Links

The problem with your code is that you haven't escaped the quotation marks " correctly in your echo...

foreach(glob("./path/to/file/{*.ext}", GLOB_BRACE) as $file) {
    echo "<li><a href=\"".pathinfo($file, PATHINFO_FILENAME).".php\">".pathinfo($file, PATHINFO_FILENAME)."</a></li>";
}

OR use single quotes '

foreach(glob("./path/to/file/{*.ext}", GLOB_BRACE) as $file) {
    echo '<li><a href="'.pathinfo($file, PATHINFO_FILENAME).'.php">'.pathinfo($file, PATHINFO_FILENAME).'</a></li>';
}

Upper case first letter(s)

As for the second part of your question if you're looking to just make the firs letter upper-case then (as ali A suggested) you can use the php function ucfirst. If you want to put the file name to title case then you can use ucwords instead.

$a = "filename";
$b = "this file name";

echo ucfirst($a); // Filename
echo ucwords($a); // Filename
echo ucfirst($b); // This file name
echo ucwords($b); // This File Name

OTHER TIPS

for first letter to be capital you can use : ucfirst()

for your pathinfo, make sure $file is not empty. post the error here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top