Send array from php to html <li>
I need to show arrayList in html <li> How do I post this variable with all files from php to my html list.
I've tried like this, but I don't know what's 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>
Thank you indeed.
source to share
Your final loop foreach
should load the data into a variable with everything wrapped in a div and then output the content of each iteration into <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, if you want the link to actually display, you need something between opening and closing tags, unless you specifically put them in, but having the filename there makes sense.
source to share
you want your display code and your html to be as separate as possible. And you want to be sure to esacape any html characters. eg,
$listItems = "";
foreach ($listaHtml as $i)
{
$i = htmlentities($i);
$listItems = $listItems."<li>$i</li>";
}
$listOutput = "<ul>$listItems</ul>";
Then in your template, just
<?php echo $listOutput; ?>
source to share