Table Array Array

I am trying to print a table using PHP / HTML. The data stored inside the array is as follows:

Array ( [id] => 1 [first_name] => mike [last_name] => lastname ) 

My code is as follows. It works and there are no errors, but the output is not as expected. Here is the PHP / HTML code:

 <table> <tr> <th>1</th> <th>2</th> <th>3</th> </tr> <?php foreach ($res as $item): ?> <tr> <td><?php echo $item['id'] ?></td> <td><?php echo $item['first_name'] ?></td> <td><?php echo $item['last_name'] ?></td> </tr> <?php endforeach; ?> </table> 

As a result, I get the first character of the elements:

 1 2 3 1 1 1 mmm lll 

Not sure what I am doing wrong? I am very grateful for the explanation.

UPDATE:

PHP CODE that has no errors:

 <?php foreach ($result as $row) { echo '<tr>'; echo '<td>' . $row['id'] . '</td>'; echo '<td>' . $row['first_name'] . '</td>'; echo '<td>' . $row['last_name'] . '</td>'; echo '</tr>'; } ?> 

And this is my array with only one "string" in it:

Variable $ result output using print_r ($ result) wrapped in <PRE> tags

 Array ( [id] => 3 [first_name] => Jim [last_name] => Dude ) 

Here is the result I get:

Actual result of the table:

 ID First Name Last Name 3 3 3 JJJ DDD 

However, if I have 0 in the array or more than 1 (which means 2 or more), it works fine. THIS ONLY DOES NOT WORK WHEN I HAVE ONLY ONE "ROW" OF ELEMENTS INSIDE THE MASS. For example, this array works fine:

  Array ( [0] => Array ( [id] => 3 [first_name] => Jim [last_name] => Dude ) [1] => Array ( [id] => 4 [first_name] => John [last_name] => Dude2 ) ) 

I get this result:

 ID First Name Last Name 3 Jim Dude 4 John Dude2 

I'm not quite sure what I'm doing wrong. The idea does not know how many elements in the table read it in the $result variable, and then use this variable to print all the elements inside the HTML table. A table can contain O elements, 1 row of elements, 1 or several rows of elements.

+7
source share
7 answers

Do not remove the foreach loop. Without seeing your code where you are building your array, I assume you are doing this:

 $res = array('id' => 1, 'first_name'=>'mike', 'last_name'=>'lastname'); 

when in fact you probably want to do this:

 $res[] = array('id' => 1, 'first_name'=>'mike', 'last_name'=>'lastname'); 

brackets [] add an array of all values ​​to your array. Thus, you can scroll through each of them as a set of values, rather than individual values.

+3
source

The data structure is not a matrix, but an array:

Array ( [id] => 1 [first_name] => mike [last_name] => lastname )

You need to do something like this what you want:

 Array( [0] => Array ( [id] => 1 [first_name] => mike [last_name] => lastname ), [1] => Array ( [id] => 1 [first_name] => mike [last_name] => lastname ) ........ ); 

Also, the code seems beautiful ...

+2
source

This is how I solved a similar problem with my page. I wanted the page to display an array of all classes, which teaches a particular teacher how to use session variables. I hope that, despite the fact that the subject may not have a relationship for you, the general meaning will help people.

 <?php echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>"; echo "<tr style='font-weight: bold;'>"; echo "<td width='150' align='center'>Class</td>"; echo "</tr>"; foreach ($query_class_array as $row) { echo '<td width="150" align=center>' . $row['Class'] . '</td>'; echo '</tr>'; } ?> 
+1
source
 <? Php echo "<table border="2">"; foreach($ array as $ values) { echo "<tr><td>"; echo "$ values"; echo "</td></tr>"; } ?> 

I wrote this to print single array values ​​into a table

+1
source

If the array is of the std object type, you cannot use [], use β†’ instead to get specific objects

 <!DOCTYPE html> <html> <body> <table> <tr> <th>id</th> <th>First name</th> <th>Last name</th> <th>email</th> </tr> <?php foreach($data as $item) {?> <tr> <td><?php echo $item->id; ?></td> <td><?php echo $item->first_name; ?></td> <td><?php echo $item->last_name ;?></td> <td><?php echo $item->email;?></td> </tr> <?php }?> </table> </body> </html> 
+1
source

This will display any multidimensional array in a more readable format than print_r ... It can be updated in different colors to an adjacent cell to quickly scroll through the array ... the space where you don't see the array, name (key) or value will tell you key / value, if you vacuum it ... not nice visually, but efficiently.

 function show_array_as_table($array,$key_color='blue',$value_color="black",$fontfamily="arial",$fontsize="12pt") { echo "<table style='color:{$color}; font-family:{$fontfamily}; font-size:{$fontsize}'>"; foreach($array as $key => $value) { echo "<tr>"; // key cell echo "<td title=\"{$key}\" style='width:150pt; height:25pt; text-align:right; vertical-align:top; background-color:cccccc; color:{$key_color}'><b>{$key}</b></td>"; // value cell (will contain a table if the value is an array) echo "<td style='width:300pt; height:25pt; text-align:justify; vertical-align:top; color:{$value_color}'>"; if (is_array($value)) { show_array_as_table($value,$key_color,$color,$fontfamily,$fontsize); } else { echo "<i>{$value}</i>"; } echo "</td>"; echo "</tr>"; } echo "</table>"; } 
0
source

This will also work:

 echo "<table>"; foreach ($result as $key){ echo " <tr> <td>'".$key['id']."'</td> <td>'".$key['first_name']."'</td> <td>'".$key['last_name']."'</td> </tr></table> "; } 
0
source

All Articles