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.
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>
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.
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; ?>