Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array to an unordered list

Tags:

arrays

html

php

I'm trying to map some arrays values to an unordered () list.

    <?php
            $files = scandir($dir);
            //remove "." and ".."
            print_r($files);
    ?>

    <ul>
        <?php foreach($files as $file): ?>
            <li><?= $file ?></li>
        <?php endforeach; ?>
    </ul>

It does iterate through the array correctly as it gives bullets for <li> elements. However no string output is seen next to those bullets. Also when I print_r the array the values are there.

The output looks like this with the correct number of bullets but no text next to them:

.
.
.

What am I doing wrong here? Thanks in advance.

like image 523
Code Bunny Avatar asked Apr 14 '12 06:04

Code Bunny


People also ask

How do you display an array in UL?

let ul = document. createElement("ul"); for (i = 0; i <= favLanguages. length; i++); { let li = document. createElement("li"); // create li element.

Are arrays ordered or unordered?

Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations.

How do you list an array in HTML?

let list = document. getElementById( "myList" ); Step 3: Now iterate all the array items using JavaScript forEach and at each iteration, create a li element and put the innerText value the same as the current item, and append the li at the list.

How to create unordered list in JavaScript?

// Create an unordered list var list = document. createElement('ul'); // Create a fragement var fragment = document. createDocumentFragment(); // Create a list item for each wizard // and append it to the fragment wizards. forEach(function (wizard) { var li = document.


3 Answers

<?php foreach($files as $file): ?>
    <li><?php echo $file ?></li>
<?php endforeach; ?>

OR

<?php foreach($files as $file): ?>
    <li><? echo $file ?></li>
<?php endforeach; ?>
like image 34
sarwar026 Avatar answered Sep 27 '22 21:09

sarwar026


I'm not sure about this, but this might work:

<?php
  $files = scandir($dir);
  //remove "." and ".."
  print_r($files);
?>

<ul>
  <?php foreach($files as $file) { ?>
  <li><?php echo $file; ?></li>
  <?php } ?>
</ul>

That's what I would use, and if this doesn't work, it might be because your $dir variable contains nothing (has an error). One reason why your original code might not have worked, because I don't think the <? ?> tags are compatible on every server. Also, from what I know, there is no <?=$var ?> thing in php. I thought it only exists in ASP and the like.

EDIT: In answer to your question about the inferiority of curly braces, they are the commonly accepted standard in PHP. This might be different in the C/C++/C# Family, I don't know.

like image 44
Lucas Avatar answered Sep 27 '22 22:09

Lucas


function array2ul($array) {
    $out = "<ul>";
    foreach($array as $key => $elem){
        if(!is_array($elem)){
            $out .= "<li><span>" . $key . ": " . $elem . "</span></li>";
        } else {
            $out .= "<li><span>" . $key . "</span>" . array2ul($elem) . "</li>";
        }
    }
    $out .= "</ul>";
    return $out; 
}
like image 170
Jonas Avatar answered Sep 27 '22 23:09

Jonas