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>";
Related
I have the following data inside one of my arrays :
array (size=3)
0 =>
array (size=4)
'ID' => string '1747' (length=4)
'OWNER' => string 'ME' (length=7)
'TABLENAME' => string 'MYFIRSTTABLE' (length=15)
'COLUMNNAME' => string 'MYFIRSTFIELD' (length=11)
1 =>
array (size=4)
'ID' => string '1756' (length=4)
'OWNER' => string 'ME' (length=7)
'TABLENAME' => string 'MYFIRSTTABLE' (length=15)
'COLUMNNAME' => string 'MYSECONDFIELD' (length=12)
2 =>
array (size=4)
'ID' => string '1757' (length=4)
'OWNER' => string 'ME' (length=7)
'TABLENAME' => string 'ANOTHERTABLE' (length=15)
'COLUMNNAME' => string 'ANOTHERFIELD' (length=16)
So far, everything is OK: I can loop through each rows and print them in a HTML table.
Something like this:
Now, I was wondering how to produce something like this:
I've tried some nested foreach, but without any success.
Thanks in advance for your help!
I think something like this should do what you want. It finds all the table names from the data (using array_unique on the output of array_column), then finds all the column names for each table (using array_filter, then array_values to re-index the keys of the array). It then loops over the tables and columns, outputting a table of table names and their column names:
$tables = array_unique(array_column($data, 'TABLENAME'));
$columns = array();
foreach ($tables as $tn) {
$columns[$tn] = array_values(array_filter($data, function ($v) use ($tn) {
return $v['TABLENAME'] == $tn;
}));
}
$max_columns = max(array_map(function ($v) { return count($v); }, $columns));
echo "<table>\n";
foreach ($tables as $tn) {
echo "<tr><td>$tn</td><td colspan=\"$max_columns\"></td></tr>\n";
echo "<tr><td></td>";
foreach ($columns[$tn] as $key => $column) {
echo "<td>{$column['COLUMNNAME']}</td>";
}
if ($key < $max_columns - 1) {
echo "<td colspan=\"" . ($max_columns - $key - 1) . "\"></td>";
}
echo "</tr>\n";
}
echo "</table>\n";
Output (for your sample data)
<table>
<tr>
<td>MYFIRSTTABLE</td>
<td colspan="2"></td>
</tr>
<tr>
<td></td>
<td>MYFIRSTFIELD</td>
<td>MYSECONDFIELD</td>
</tr>
<tr>
<td>ANOTHERTABLE</td>
<td colspan="2"></td>
</tr>
<tr>
<td></td>
<td>ANOTHERFIELD</td>
<td colspan="1"></td>
</tr>
</table>
Demo on 3v4l.org
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.
How do I display the following using the array shown below?
Pastors
key=>0, member_id, member_name
Deacons
key=>1, member_id, member_name
key=>2, member_id, member_name
Here is the array.
array (size=3)
0 =>
array (size=4)
'category_name' => string 'Pastors' (length=7)
'member_id' => string '3' (length=1)
'member_name' => string 'Tiny Marshall' (length=13)
'member_email' => string 'jconley#nowhere.com' (length=19)
1 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '1' (length=1)
'member_name' => string 'Jeremiah Conley' (length=15)
'member_email' => string 'jconley#nowhere.com' (length=19)
2 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '2' (length=1)
'member_name' => string 'Marcy Conley' (length=12)
'member_email' => string 'jconley#nowhere.com' (length=19)
Here is the code that I used to build the array:
while( $row = mysql_fetch_assoc( $result ) )
{
$staff[$i] = array
(
'category_name' => $row['category_name'],
'member_id' => $row['member_id'],
'member_name' => $row['member_name'],
'member_email' => $row['member_email'],
);
$i++;
}
This is the final solution:
$category_name = array();
foreach ($staff as $member) {
if (!in_array( $member['category_name'], $category_name ))
{
echo '<h1>' . $member['category_name'] . '</h1>';
$category_name[] = $member['category_name'];
}
echo $member['member_id'] . ', ' . $member['member_name'] . '<br />';
}
Use foreach to loop over your $staff array.
$pastors = array();
$deacons = array();
foreach ($staff as $member) {
if ($member['category_name'] == 'Pastors') {
$pastors[] = $member['member_id'] . ', ' . $member['member_name'];
} else if ($member['category_name'] == 'Deacons') {
$deacons[] = $member['member_id'] . ', ' . $member['member_name'];
}
}
documentation
Consider:
array (size=1)
0 =>
array (size=5)
'name' => string '15268459735 Farming and Projects XXXXX' (length=38)
'region' => string '2' (length=1)
'entitynumber' => string '2012/002086/24' (length=14)
'ownership' => string '6' (length=1)
'id' => string '26249' (length=5)
Why does the following still return the same array without the owner element?
foreach ($result as $row)
{
$row['owner'] = 1;
}
Try passing the array by reference:
foreach ($result as &$row) {
$row['owner'] = 1;
}
See also: What's the & for, anyway?
'products' =>
array
0 =>
array
'pid' => string '3' (length=1)
'name' => string '#122232' (length=7)
'information' => string 'DSDSD' (length=5)
'image_tumb' => string '1tumbc4ad490017dc211002cfef359204ca7e154467_VisualVoicemail_Android_original.jpg' (length=80)
'tag' => string 'Sepatu' (length=6)
'price' => string '12000000.00' (length=11)
1 =>
array
'pid' => string '4' (length=1)
'name' => string 'Jam Bagus' (length=9)
'information' => string 'DSDSD' (length=5)
'image_tumb' => string '1tumbc4ad490017dc211002cfef359204ca7e_48447427_diaspora_dandy_logo.jpg' (length=70)
'tag' => string 'Jam' (length=3)
'price' => string '12000.00' (length=8)
2 =>
array
'pid' => string '6' (length=1)
'name' => string '#122232' (length=7)
'information' => string 'awdwad' (length=6)
'image_tumb' => string '1tumbc4ad490017dc211002cfef359204ca7e10408_710.jpg' (length=50)
'tag' => string 'Sepatu' (length=6)
'price' => string '140000.00' (length=9)
unlimited.
how do i display them like
name
name
name
name
<hr/>
name
name
name
name
<hr/>
or insert a hr after 4 times fetching ( currently useing PDO fecthall and foreach ) ?
// displays name loops like "namenamenamenamenamenametounlimiteddata"
foreach ($db['products'] as $product) {
echo "<div id='product'>";
echo $db[product][name];
echo "</div>";
}
but still no clue of doing the above. my friends say to use % sign in php? is that the best way ? if it is please give me an example how do it the right way, if there is a better way please describe it.
Thanks for looking in.
Adam Ramadhan
$i = 1;
foreach ($db['products'] as $product) {
echo "<div id='product'>";
echo $db[product][name];
echo "</div>";
if ($i % 4 == 0)
echo "<hr>";
$i++;
}
You can do:
$i = 0;
foreach ($db['products'] as $product) {
if($i && $i%4 == 0) {
echo "<hr />";
}
// your existing echo here
$i ++;
}
See it
Your friend is right you need to use the % operator called as modulus operator which gives the reminder after division.
$i = 0;
foreach ($db['products'] as $product) {
if($i%4 == 0)
echo "<hr>";
echo "<div id='product'>";
echo $db[product][name];
echo "</div>";
$i++;
}
Use a counter variable $i and increment it each time and check if
$i%4 == 0
then echo hr
From your example, I think you meant the 5th one.
Try something along the line of:
for($i = 0; $i < 100; $i++) {
if($i && $i%5 == 0) {
// the fifth
}
}
ops it's suppose to be 1
$a=1;
foreach ($db['products'] as $product) {
echo "<div id='product'>";
echo $db[product][name];
echo "</div>";
if($a%4==0){
echo "<hr/>";
}
$a++;
}