Trasnformar array in Divs?

Asked

Viewed 24 times

0

I have a page php who brings me a registration list, but he brings a array and would like every item of it to come in divs or table with rows.

Down with what I’m doing:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('user_id');
$query->from($db->quoteName('#__tabela'));          
$db->setQuery($query);
$column= $db->loadColumn();
print_r($column);

He returns to me:

Array ( [0] => 703 [1] => 704 [2] => 719 [3] => 716 [4] => 719 [5] => 722 [6] => 724 [7] => 725 [8] => 729 [9] => 730 [10] => 732 [11] => 733 [12] => 735 [13] => 736 [14] => 737 [15] => 744 )

I wish something like this would come:

<tr>703</tr>
<tr>704</tr>
<tr>719</tr>
...

Thank you!

1 answer

3

Just make a foreach and then add the tr:

foreach($column as $value){
    echo '<tr>' . $value . '</tr>';
}

Test it out here

This will display the value between tr.


However if you want an array with the "prefix" and "suffix" of <tr> and </tr> you can use the array_map:

$array = array_map(function($value) { return '<tr>'.$value.'</tr>'; }, $column);

Thus using a:

var_dump($array);

You will have:

array(6) {
  [0]=>
  string(12) "<tr>703</tr>"
  [1]=>
  string(12) "<tr>704</tr>"
  [2]=>
  string(12) "<tr>719</tr>"
  [3]=>
  string(12) "<tr>716</tr>"
  [4]=>
  string(12) "<tr>719</tr>"
  [5]=>
  string(12) "<tr>722</tr>"
}

Test it out here.


Another option, if you are using Mysql, is to useCONCAT, that way you can make:

SELECT CONCAT('<tr>', Coluna, '</tr>') as Coluna FROM tabela

This will already return the "Column" data between the tr, no need for PHP treatment.

  • What do I want is one in each tr, the one from SELECT CONCAT as use? vlw!!

  • he returned everything in sequence using foreach: 703704719716719722724725729730732733735736737744

  • Probably because it was rendered by the browser, give it a F12 or CTRL+U, and check that there will be tr.

Browser other questions tagged

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