I get this array from a mysql query:
array (size=9)
0 =>
array (size=2)
'room_category' => string 'MALE GENERAL WARD' (length=17)
'vacant_beds' => string 'MG-8,MG-2,MG-4,MG-6,MG-7' (length=24)
1 =>
array (size=2)
'room_category' => string 'FEMALE GENERAL WARD' (length=19)
'vacant_beds' => string 'FG-4,FG-1,FG-2,FG-3' (length=19)
2 =>
array (size=2)
'room_category' => string 'MOTHER CHILD WARD' (length=17)
'vacant_beds' => string 'MC-2,MC-4,MC-5,MC-6' (length=19)
3 =>
array (size=2)
'room_category' => string 'TWIN' (length=4)
'vacant_beds' => string 'TW-A1,TW-A2,TW-B2,TW-C1,TW-C2' (length=29)
4 =>
array (size=2)
'room_category' => string 'NICU' (length=4)
'vacant_beds' => string 'NICU-6,NICU-1,NICU-7,NICU-3,NICU-8,NICU-4,NICU-5' (length=48)
5 =>
array (size=2)
'room_category' => string 'CLASSIC' (length=7)
'vacant_beds' => string 'CL-6,CL-8,CL-4,CL-5' (length=19)
6 =>
array (size=2)
'room_category' => string 'DELUXE' (length=6)
'vacant_beds' => string 'DLX-5,DLX-6' (length=11)
7 =>
array (size=2)
'room_category' => string 'EXECUTIVE' (length=9)
'vacant_beds' => null
8 =>
array (size=2)
'room_category' => string 'AC GENERAL WARD' (length=15)
'vacant_beds' => string 'AG-5,AG-1,AG-2,AG-3,AG-4' (length=24)
Now I want to display this array in html table.
I have tried this, where I can achieve it only partly.
I want the string part that is string 'MG-8,MG-2,MG-4,MG-6,MG-7' to be in separate columns.
echo "<table>";
foreach($rows as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
from which I get table like below
MALE GENERAL WARD MG-8,MG-2,MG-4,MG-6,MG-7
FEMALE GENERAL WARD FG-4,FG-1,FG-2,FG-3
MOTHER CHILD WARD MC-2,MC-4,MC-5,MC-6
TWIN TW-A1,TW-A2,TW-B2,TW-C1,TW-C2
NICU NICU-6,NICU-1,NICU-7,NICU-3,NICU-8,NICU-4,NICU-5
CLASSIC CL-6,CL-8,CL-4,CL-5
DELUXE DLX-5,DLX-6
EXECUTIVE
AC GENERAL WARD AG-5,AG-1,AG-2,AG-3,AG-4
Every item of your comma-separated string can be received by exploding string by ,. But as every string can have different number of elements - first you need to find the value of max elements. Thus you need to iterate $rows twice - first find max count of elements and second - echo tds. So we can do this:
echo "<table>";
$new_rows = [];
$max_count = 0;
foreach ($rows as $key => $row) {
$elements = [];
if (!empty($row['vacant_beds'])) {
$elements = explode(',', $row['vacant_beds']);
if (sizeof($elements) > $max_count) {
$max_count = sizeof($elements);
}
}
$new_rows[] = [
'name' => $row['room_category'],
'elements' => $elements,
];
}
foreach ($new_rows as $row) {
echo '<tr>';
// echo name
echo '<td>' . $row['name'] . '</td>';
// main part - items in elements we will wrap into `td`:
foreach ($row['elements'] as $e) {
echo '<td>' . $e . '</td>';
}
// if number of elements in `$row['elements']` is less
// than `$max_count` - we should add empty `<td>`
if (sizeof($row['elements']) < $max_count) {
echo str_repeat('<td></td>', $max_count - sizeof($row['elements']));
}
echo '</tr>';
}
echo "</table>";
Handle the needed key(in your case - vacant_beds) for further processing:
echo "<table border='1'>";
foreach($rows as $row) {
echo "<tr>";
foreach($row as $key => $row2){
echo "<td>" .
(($key == 'vacant_beds')? implode("</td><td>", explode(",", $row2)) : $row2)
. "</td>";
}
echo "</tr>";
}
echo "</table>";
i want to echo the array value with the key of type , here is my code,
foreach ($sam as $key => $sa) {
foreach ($sa as $s) {
echo $s['type'];
}
}
and this is the content of array $sam
array (size=1)
0 =>
array (size=5)
'type' => string 'days' (length=4)
'bookable' => string 'no' (length=2)
'priority' => int 10
'from' => string '1' (length=1)
'to' => string '1' (length=1)
my serier of foreach code results Warning: Illegal string offset 'type' and i dont know why? can you help me echo type thank you.
You only need one foreach:
foreach ($sam as $s) {
echo $s['type'];
}
I try to make a table html, with 2 table PHP,
I don't manage to put my value which is in my 4th row, 3th column in the 4th row, 4th column.
My PHP/html:
<table BORDER=1px>
<tr>
<?php
foreach($tableau1 as $value)
{
echo '<th>'.$value.'</th>';
}
?>
</tr>
<?php
foreach($tableau as $valu)
{
echo '<tr>';
foreach($tableau1 as $val => $ke)
{
foreach($valu as $vava => $keke)
{
if($val==$vava){echo '<td>'.$keke.'</td>';}
}
}
echo '</tr>';
}
?>
</table>
My PHP table (first:table , second:table1)
array (size=3)
6 =>
array (size=2)
'marque' => string 'marque 6' (length=8)
'modele' => string 'modele 6' (length=8)
3 =>
array (size=4)
'marque' => string 'marque 3' (length=8)
'modele' => string 'modele 3' (length=8)
2 => string 'bois art3' (length=9)
4 => string 'beton art3' (length=10)
5 =>
array (size=3)
'marque' => string '-lepetit' (length=8)
'modele' => string 'modele 5' (length=8)
4 => string 'beton art5' (length=10)
array (size=4)
'marque' => string 'marque' (length=6)
'modele' => string 'modèle' (length=6)
2 => string 'bois' (length=4)
4 => string 'beton' (length=5)
Response to first answer:
:) I already tried it but it's not good I get too much cell in all row and I don't understand why ...
I think you need to print an empty <td></td> when no data for the cell.
<?php
foreach($tableau as $valu)
{
echo '<tr>';
foreach($tableau1 as $val => $ke)
{
$found = false;
foreach($valu as $vava => $keke)
{
//echo '<td>' . $val . ' ' . $vava . ' ' . $keke . '</td>';
if($val==$vava){
echo '<td>'.$keke.'</td>';
$found = true;
}
}
if (!$found) { echo '<td></td>'; }
}
echo '</tr>';
}
?>
Added a boolean to flag when data was added to the column and then moved the
echo '<td></td>';
outside of the inner loop.
I have a PHP array, actually a MySQL row constructed with CodeIgniter's Active Record.
So I have an array which var_dumps like this :
array (size=10)
0 =>
array (size=4)
'user_id' => string '2' (length=1)
'puzzle_id' => string '17' (length=2)
'birth' => string '2014-01-26 16:08:25' (length=19)
1 =>
array (size=4)
'user_id' => string '2' (length=1)
'puzzle_id' => string '16' (length=2)
'birth' => string '2014-01-26 02:07:05' (length=19)
2 => .....
this is constructed like this :
$this->db->order_by("birth" , "desc");
$rows = $this->db->get("my_table" , $limit)->result_array();
foreach($rows as $row)
{
$row['testindex'] = "testvalue";
}
return $rows;
so why does my array NOT have the "testindex" indices ?
Thanks for any help !
Because that's not how PHP and foreach() in particular works.
$row in your code is a copy of the corresponding element in $rows, not the actual element. Modifying a copy doesn't modify the original.
You'd want to do this:
for ($i = 0, $c = count($rows); $i < $c; $i++)
{
$rows[$i]['testindex'] = 'testvalue';
}
Try this (PHP 5+):
foreach($rows as &$row)
{
$row['testindex'] = "testvalue";
}
I think I can do this with foreach too.
Like this :
foreach($rows as $key => $value)
{
$rows[$key]['testindex'] = "testvalue";
}
I have a data structure like this:
array (size=4)
'active' =>
array (size=1)
170 =>
object(stdClass)[2847]
public 'item' => string '170' (length=3)
'complete' =>
array (size=1)
8 =>
object(stdClass)[2849]
public 'item' => string '8' (length=1)
'dropped' =>
array (size=1)
10 =>
object(stdClass)[2850]
public 'item' => string '10' (length=2)
'total' =>
array (size=1)
188 =>
object(stdClass)[2851]
public 'item' => string '188' (length=3)
I am using this loop to iterate the datastruct and access the value in item.
foreach($ecounts as $key => $value){
if($key == 'total'){
foreach($value as $i){
$te = $i->item;
}
}elseif($key == 'active'){
foreach($value as $i){
$ae = $i->item;
}
}elseif($key == 'dropped'){
foreach($value as $i){
$de = $i->item;
}
}elseif($key == 'complete'){
foreach($value as $i){
$ce = $i->item;
}
}
}
I am sure there is a smarter way to access the item value. The additional foreach() loop inside each if statement seems overkill, but I could not find a better way to accomplish.
Thank you for insights.
Maybe you can decide the name of the variable before you start the additional loops.
Like
foreach($ecounts as $key => $value){
$var = ($key == 'total' ? 'te' : $key == 'active' : 'ae' ? $key == 'dropped' : 'de' ? $key == 'complete' : 'ce');
foreach($value as $i){
${$var} = $i->item;
}
}
Read http://php.net/manual/en/language.variables.variable.php for more documentation.