Take comma-separated values from the database and organize them into <li>

Asked

Viewed 1,024 times

-1

I have in my database, inside my table "db_home_content" the column "a2_content_x_imploders" ("x" would be the respective content number, as represented in the image) that contains some data separated by comma. Example: Site em HTML5, Alta velocidade, Responsivo.

I need to take this data and organize it, according to the amount, in <li>'as shown in the image. imagem demonstrativa Is there a function in PHP or Mysql that does the opposite of implode? If so, how does it work for my case?

  • I reversed the issue, because it runs away from the original problem and already answered. If you have any additional questions, ask the colleagues below in the comments, or open a new question just about the different part.

3 answers

3


Yes! Function explode().

Example:

$lista = "item1,item2,item3";
$lista = explode(",", $lista);
// Aqui lista passou a ser um array("item1", "item2", "item3");

To write the Lis, just use the foreach().

Example:

$htmlLista = "ul";
foreach ($lista as $item) {
   $htmlLista .= "li".$item."li";
}
$htmlLista .= "ul";
echo $htmlLista;
// Lembre-se de adicionar < e > nas tags, não consigo adicionar na resposta pois estes caracteres são removidos.
  • It worked as you wanted! Is it possible to pass each separate value to an input, in an administrative panel? Taking into account that the maximum inputs will be 8

  • You can create the inputs the same way the Lis were created, using foreach().

  • I already solved the problem. I thought it best to put them "implodidos" in the panel, with the input tags plugin. Thanks.

1

With Javascript, it is possible to separate the text by comma, and add the li:

var texto = 'Site em HTML5, Alta velocidade, Responsivo'.split(',');
texto.forEach(function(item) {
  var li = document.createElement("li");
  li.innerHTML = item;
  document.getElementById("siteFit").appendChild(li);
});
li {
  list-style-type: none;
}
<h3>Site Fit</h3>
<ul id="siteFit"></ul>

  • It would work well, but in this case, I will need to then pass each result as a different input value.

  • 3

    @Danielbonifácio You don’t even mention input in your question.

0

$conteudo = 'Site em HTML5, Alta velocidade, Responsivo';

$conteudoArray = explode(',', $conteudo);

echo '<ul>';
foreach($conteudoArray as $conteudo) {
    echo'<li>' . $conteudo . '</li>';
}
echo '</ul>';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.