Transform array into divs?

0

I have a page with php that brings me a list of records, however it brings a array and I would like every item of it to come in divs or table with lines.

Below 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);

It returns 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 would like something like this to happen:

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

Thank you!

    
asked by anonymous 16.03.2017 / 21:58

1 answer

3

Just make a foreach and then add tr :

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

Try this 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 array_map :

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

This way 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>"
}

Try this here.

Another option, if you are using MySQL, is to use CONCAT , so you can do:

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

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

    
16.03.2017 / 22:09