3

I need to show arrayList in a html < li > How I send this variable with all the files from php to my html list.

I tryed like this but I don't know whats wrong.. :'(

This is my php code:

$directorioInicial = "./";    //Especifica el directorio a leer
$rep = opendir($directorioInicial);    //Abrimos el directorio

$listaHtml = array();

while ($todosArchivos = readdir($rep)) {  //Leemos el arreglo de archivos     contenidos en el directorio: readdir recibe como parametro el directorio abierto
   if ($todosArchivos != '..' && $todosArchivos != '.' && $todosArchivos != '' && strpos($todosArchivos, '.html') && !is_dir($todosArchivos)) {

    $listaHtml[] = $todosArchivos;       
  }
}

foreach ($listaHtml as $i) {

//    echo $i . "<br>";

}

And this is my html list:

 <div class="propiedadesCaja" id="acordeon">
        <ul>
            <li class="listaPaginas">

                <a class="listado" href="<?php echo $i; ?>" target="probando" ></a>  

            </li>
        </ul>
    </div>

Really Thank you.

3
  • I would use a php foreach result echo "<li>' .$i. '</li>" Commented May 27, 2015 at 14:01
  • What I need its to print the arrayList into html <li> but thank you Commented May 27, 2015 at 14:04
  • in that case can you not use str_replace to echo your array in html with the list you want? php.net/manual/en/function.str-replace.php I am trying to understand eactly what you want to do so I apologise if it's wrong Commented May 27, 2015 at 14:18

3 Answers 3

1

Your HTML can look like this:

<div class="propiedadesCaja" id="acordeon">
    <ul>
        <?php foreach($listaHtml as $i){ ?>
        <li class="listaPaginas">
            <a class="listado" href="<?php echo $i; ?>" target="probando">Text here</a>
        </li>
        <?php } ?>
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. You may also want to try href="<?=$i?>" as this is a shorter way to echo. Also, $listaHtml could be a key-value array that contains the URL and text to display. Then you would have foreach($listaHtml as $url => $text)
0

you want to keep your display code and your html as seperate as possible. And you want to be sure to esacape any html characters. for example,

    $listItems = "";
    foreach ($listaHtml as $i)
    {
      $i = htmlentities($i);
      $listItems = $listItems."<li>$i</li>";
    }

    $listOutput = "<ul>$listItems</ul>";

Then in your template, simply

<?php echo $listOutput; ?>

2 Comments

Doesn't work :( I try to load the variable and then call from html I tryed like this <a class="listado" href="<?php echo $listOutput; ?>" target="probando" ></a> And not found :(
put that <a> tag in the foreach loop
0

Your final foreach loop should load the data to a variable, with everything encased in the div and then output the content of each iteration within an <a>, thus

echo '<div class="propiedadesCaja" id="acordeon">';
foreach ($listaHtml as $i) {

   echo '<a class="listado" href="' . $i . '" target="probando" >' . $i . '</a>';

}
echo '</div>';

Remember that if you want the link to actually show up, then you need something in between the opening and closing a tags unless you're styling them specifically, but having the file name in there makes sense.

4 Comments

What I need its to print the arrayList into html <li> call php variable from html and print here not inside php thank you
Right, if I understand you correctly, what you need to do is actually load the data into a variable rather than simply output from the loop. Although you should clearly state that you need to have the create of the list's content and then display it separately. I've edited to explain that.
You've just been shown - it takes the PHP data and outputs it as HTML in an unordered list.
I need to show here <div class="propiedadesCaja" id="acordeon"><a class="listado" href="<?php echo $output; ?>" target="probando" ></a> </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.