how to make tabular data from nested array - php

Hi iam working on attendance report, data in array we have :
Array
(
[0] => Array
(
[ActatekLog] => Array
(
[id] => 1
[timeentry] => 2011-02-16 00:09:02
[eventID] => OUT
[terminalSN] => 00111DA08C8B
[jpegPhoto] =>
)
)
[1] => Array
(
[ActatekLog] => Array
(
[id] => 1
[timeentry] => 2011-02-16 00:09:12
[eventID] => IN
[terminalSN] => 00111DA08C8B
[jpegPhoto] =>
)
)
[2] => Array
(
[ActatekLog] => Array
(
[id] => 1
[timeentry] => 2011-02-16 00:11:31
[eventID] => OUT
[terminalSN] => 00111DA08C8B
[jpegPhoto] =>
)
)
[3] => Array
(
[ActatekLog] => Array
(
[id] => 1
[timeentry] => 2011-02-16 00:11:40
[eventID] => IN
[terminalSN] => 00111DA08C8B
[jpegPhoto] =>
)
)
[4] => Array
(
[ActatekLog] => Array
(
[id] => 1
[timeentry] => 2011-02-16 00:13:17
[eventID] => OUT
[terminalSN] => 00111DA08C8B
[jpegPhoto] =>
)
)
[5] => Array
(
[ActatekLog] => Array
(
[id] => 1
[timeentry] => 2011-02-16 00:13:21
[eventID] => IN
[terminalSN] => 00111DA08C8B
[jpegPhoto] =>
)
)
)
report format is :
Date In Time Out Time Work Time
2011-02-16
i need help in display in time and out time in one row for same day.
Please help

I would start by formatting the output array a little bit first.
$arr = array();
foreach ( $result as $row ) {
$arr[ $row[ 'terminalSN' ] ][ $row[ 'eventID' ] ] = $row;
}
Then output should be a little easier:
<table>
<tr>
<th>Date</th>
<th>Time In</th>
<th>Time Out</th>
<th>Work Time</th>
</tr>
<?php foreach ( $arr as $key => $value ) : ?>
<tr>
<td><?php echo date( 'Y-m-d', strtotime( $value[ 'IN' ][ 'timeentry' ] ) ); ?></td>
<td><?php echo date( 'H:i:s', strtotime( $value[ 'IN' ][ 'timeentry' ] ) ); ?></td>
<td><?php echo date( 'H:i:s', strtotime( $value[ 'OUT' ][ 'timeentry' ] ) ); ?></td>
<td><?php echo "Calculate based on out minus in. Pretty simple at this point." ?></td>
</tr>
<?php endforeach; ?>
</table>

I'm using your data model never changes and that your data is already sorted the way you'd like it to be sorted. So, for an array like this:
$data = array
(
0 => array
(
'ActateLog' => array
(
'id' => 1,
'timeentry' => '2011-02-16 00:09:02',
'eventID' => 'OUT',
'terminalSN' => '00111DA08C8B',
'jpegPhoto' => false
)
),
1 => array
(
'ActateLog' => array
(
'id' => 1,
'timeentry' => '2011-02-16 00:09:12',
'eventID' => 'IN',
'terminalSN' => '00111DA08C8B',
'jpegPhoto' => false
)
)
);
I made a function, that returns an array with n/2 rows and has both IN and OUT in the same row.
function processMyArray($data)
{
$output = array();
$lim = count($data);
$i = $j = 0;
for($i=0; $i<$lim; $i+=2, $j++)
{
$output[$j] = array
(
'date' => substr($data[$i]['ActateLog']['timeentry'], 0, 10),
'IN' => substr($data[$i]['ActateLog']['timeentry'], 11),
'OUT' => substr($data[$i+1]['ActateLog']['timeentry'], 11),
'work_time' => strtotime($data[$i+1]['ActateLog']['timeentry']) - strtotime($data[$i]['ActateLog']['timeentry'])
);
}
return $output;
}

Here you go that'll do it though you dont have two values in your array that you state you want in your table.
<table>
<tr>
<th>Date</th>
<th>Time In</th>
<th>Time Out</th>
<th>Work Time</th>
</tr>
<?php foreach ($thisArr as $arr) : ?>
<tr>
<td><?php echo date('Y-m-d'); ?></td>
<td><?php echo $arr['ActatekLog']['timeentry']; ?></td>
<td><?php echo "no time out in the array??"; ?></td>
<td><?php echo "you need to calculate this I assume"; ?></td>
</tr>
<?php endforeach; ?>
</table>

Related

How to create table with rowspan

I retrieved data from a sql query that I parsed into an array.
[D20180821] => Array
(
[mark01] => Array
(
[PARIS] => Array
(
[0] => Array
(
[event_id] => 1
[evnet] => OPEN_TICKET_D
[id] => 80
[time] => 2018-08-22 06:56:41
)
[1] => Array
(
[event_id] => 5
[evnet] => CLOSE_TICKET_D
[id] => 651
[time] => 2018-08-22 11:31:00
)
[2] => Array
(
[event_id] => 4
[evnet] => GO
[id] => 82
[time] => 2018-08-22 11:50:35
)
)
[LONDON] => Array
(
[0] => Array
(
[event_id] => 1
[evnet] => OPEN_TICKET_D
[id] => 79
[time] => 2018-08-22 06:56:38
)
[1] => Array
(
[event_id] => 5
[evnet] => CLOSE_TICKET_D
[id] => 652
[time] => 2018-08-22 11:29:00
)
[2] => Array
(
[event_id] => 4
[evnet] => GO
[id] => 81
[time] => 2018-08-22 11:50:35
)
[3] => Array
(
[event_id] => 6
[evnet] => CLOSE_TICKET_R
[id] => 647
[time] => 2018-08-24 10:40:00
)
)
)
[rows] => 2
)
I would like to create a table knowing that there are rowspan to do. To prepare this table I calculated the rowspan in [rows].
Here is an example of the painting I want to build
I can not get away with the lopps to build this table
thank you very much
Input Data
$tab=Array(
'D20180821' => Array
(
'mark01' => Array
(
'PARIS' => Array
(
'0' => Array
(
'event_id' => '1',
'evnet' => 'OPEN_TICKET_DEPLOY',
'id' => '80',
'time' => '2018-08-22 06:56:41'
),
'1' => Array
(
'event_id' => '5',
'evnet' => 'CLOSE_TICKET_DEPLOY',
'id' => '651',
'time' => '2018-08-22 11:31:00'
),
'2' => Array
(
'event_id' => '4',
'evnet' => 'POST_GO',
'id' => '82',
'time' => '2018-08-22 11:50:35'
)
),
'LONDON' => Array
(
'0' => Array
(
'event_id' => '1',
'evnet' => 'OPEN_TICKET_DEPLOY',
'id' => '79',
'time' => '2018-08-22 06:56:38'
),
'1'=> Array
(
'event_id' => '5',
'evnet' => 'CLOSE_TICKET_DEPLOY',
'id' => '652',
'time' => '2018-08-22 11:29:00'
),
'2' => Array
(
'event_id' => '4',
'evnet' => 'POST_GO',
'id' => '81',
'time' => '2018-08-22 11:50:35'
),
'3' => Array
(
'event_id' => '6',
'evnet' => 'CLOSE_TICKET_REMOVE',
'id' => '647',
'time' => '2018-08-24 10:40:00'
)
)
),
'rows' => '2'
)
);
The Code: (you can test it # phptester.net)
echo '<table border class="table table-striped">';
echo '<tr>';
echo '<th>Event</th>
<th>mark</th>
<th>Place</th>
<th>OPEN_TICKET_D</th>
<th>CLOSE_TICKET_D</th>
<th>GO</th>
<th>CLOSE_TICKET_R</th>';
echo '</tr>';
foreach ($tab as $k => $qal) {
$rowspan = $qal['rows'];
unset($qal['rows']);
$first = true;
foreach ($qal as $mark => $cities) {
foreach ($cities as $city => $details) {
echo '<tr>';
if ($first) {
echo "<td rowspan='$rowspan'>$k</td>";
$first = false;
}
echo "<td>$mark</td>";
echo "<td>$city</td>";
$temp = array_replace(
[
'OPEN_TICKET_DEPLOY' => '',
'CLOSE_TICKET_DEPLOY' => '',
'POST_GO' => '',
'CLOSE_TICKET_REMOVE' => ''
],
array_column($details, 'time', 'evnet')
);
foreach ($temp as $val) {
echo "<td>$val</td>";
}
echo '</tr>';
}
}
}
echo '</table>';
Output:
After storing the rows value, I remove it from the array so that it is not iterated in the next foreach loop.
The cell that contains the rowspan attribute must be echoed only once, that is why I am using a $first variable and checking for true|false.
As for the deepest subarray data, I am array_column() and array_replace() to ensure that all expected columns are represented, have a default value, and are in the correct order before they are looped. This part could have also been performed with a series of isset() conditions before echoing hardcoded keys -- I arbitrarily chose to use array functions instead.
Here is the alternate syntax:
echo '<table border class="table table-striped">';
echo '<tr>';
echo '<th>Event</th>
<th>mark</th>
<th>Place</th>
<th>OPEN_TICKET_D</th>
<th>CLOSE_TICKET_D</th>
<th>GO</th>
<th>CLOSE_TICKET_R</th>';
echo '</tr>';
foreach ($tab as $k => $qal) {
$rowspan = $qal['rows'];
unset($qal['rows']);
$first = true;
foreach ($qal as $mark => $cities) {
foreach ($cities as $city => $details) {
echo '<tr>';
if ($first) {
echo "<td rowspan='$rowspan'>$k</td>";
$first = false;
}
echo "<td>$mark</td>";
echo "<td>$city</td>";
echo '<td>' , (isset($details[0]['time']) ? $details[0]['time'] : '') , '</td>';
echo '<td>' , (isset($details[1]['time']) ? $details[1]['time'] : '') , '</td>';
echo '<td>' , (isset($details[2]['time']) ? $details[2]['time'] : '') , '</td>';
echo '<td>' , (isset($details[3]['time']) ? $details[3]['time'] : '') , '</td>';
echo '</tr>';
}
}
}
echo '</table>';

PHP Two Arrays Into One Table. First Array Vertical, Second Horizontal

i want result like this in view.
This is (part of) the array:
Array ( [0] => Array ( [car_id] => ferarri [total] => 15 ) [1] => Array ( [car_id] => lamborgini [total] => 10 ) [2] => Array ( [car_id] => ford [total] => 32 ) [3] => Array ( [car_id] => toyota [total] => 45 ) [4] => Array ( [car_id] => viar [total] => 1 ) ) 1
how do I make it look. horizontally displays the total. and vertical displays car brand.
You can use this
<?php
$car_tot = array(
'0' => array (
'car_id' => 'ferarri',
'total' => 15
),
'1' => array (
'car_id' => 'lamborgini',
'total' => 10
),
'2' => array (
'car_id' => 'ford',
'total' => 32
),
'3' => array (
'puskesmas_id' => 'toyota',
'total' => 45
),
'4' => array (
'car_id' => 'viar',
'total' => 1
)
);
echo '<pre>';
print_r( $car_tot );
echo '</pre>';
?>
<table>
<tr>
<th>Type</th>
<th>Total</th>
</tr>
<?php
foreach( $car_tot as $key=>$ctRow )
{
?>
<tr>
<td>
<?= !empty( $ctRow['car_id'] ) ? $ctRow['car_id'] : '';?>
</td>
<td>
<?= !empty( $ctRow['total'] ) ? $ctRow['total'] : '';?>
</td>
</tr>
<?php
}
?>
</table>

Displaying a nested array in an HTML table

This is a simple problem that I've been trying to solve for several hours. I have an array containing information of several students and marks they scored in tests. There are 8 subjects in total, with each subject having 5 parameters.
The array looks like below:
Array
(
[Ron] => Array
(
[subject1] => Array
(
[test1] => 47
[test2] => 86
[total] => 133
[percentage] => 88.67
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 963
[gross_percentage] => 80.25
[...]
[subject8] => Array
(
[test1] => 48
[test2] => 86
[total] => 134
[percentage] => 89.33
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 900
[gross_percentage] => 75.50
)
[John] => Array
(
[subject1] => Array
(
[test1] => 39
[test2] => 72
[total] => 111
[percentage] => 74
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 963
[gross_percentage] => 80.25
[...]
[subject8] => Array
(
[test1] => 39
[test2] => 75
[total] => 114
[percentage] => 76
[status] => Pass
)
[pass_count] => 8
[fail_count] => 0
[gross_total] => 846
[gross_percentage] => 70.5
)
)
I need to display the following in a nicely formatted HTML table (I'm planning to use Bootstrap), but I can't for the life of me figure out how to properly nest the table using PHP.
This is the HTML markup that I currently have: http://jsfiddle.net/9y910uvp/
This gives the following result:
Which is okay.
The Actual Problem
I'm using PHP to display the contents from the array. I'm confused how to display the values for the nested <tr> and <td> sections.
How do I loop through the array and display the contents correctly? (I'd post my attempts so far, but they're all stupid and not working so I'm not posting).
An example would be greatly appreciated because I've spent countless hours trying to get this working, but failed terribly.
Not quite the same output, but here's a recursive approach that should handle any depth of nested arrays...
<?php
$data = Array (
"Ron" => Array (
"subject1" => Array (
"tests" => Array (
"test1" => 47,
"test2" => 86,
"total" => 133,
"percentage" => 88.67,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 963,
"gross_percentage" => 80.25,
),
"subject8" => Array (
"tests" => Array (
"test1" => 48,
"test2" => 86,
"total" => 134,
"percentage" => 89.33,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 900,
"gross_percentage" => 75.50,
),
),
"John" => Array (
"subject1" => Array (
"tests" => Array (
"test1" => 39,
"test2" => 72,
"total" => 111,
"percentage" => 74,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 963,
"gross_percentage" => 80.25,
),
"subject8" => Array (
"tests" => Array (
"test1" => 39,
"test2" => 75,
"total" => 114,
"percentage" => 76,
"status" => "Pass",
),
"pass_count" => 8,
"fail_count" => 0,
"gross_total" => 846,
"gross_percentage" => 70.5,
),
),
);
print_r($data);
$table = table_cell($data);
echo $table;
function table_cell($data) {
$return = "<table border='1'>";
foreach ($data as $key => $value) {
$return .= "<tr><td>$key</td><td>";
if (is_array($value)) {
$return .= table_cell($value);
} else {
$return .= $value;
}
$return .= "</td><tr>";
}
$return .= "</tr></table>";
return($return);
}
and the table looks nothing like the requested but... it was an interesting excersize...
Try this out
<table border="1">
<thead>
<thead>
<tr>
<th>Name</th>
<th>Subject</th>
<th>Test1 Marks</th>
<th>Test2 Marks</th>
<th>Total Marks</th>
<th>Status</th>
<th>Percentage</th>
<th>Pass Count</th>
<th>Total Percentage</th>
</tr>
</thead>
<tbody>
</tr>
<?php
$numberOfSubjects = 3; //I used 3 subjects. You should change this to 8 for your data.
foreach ($data as $student => $info) {
echo "<tr><td rowspan=$numberOfSubjects />$student</td>";
//flag to let the inner loop the tr has been drawn for the first row
$firstRow = true;
foreach ($info as $key => $value) {
//we only want subject info
if (strpos($key, "subject") === 0) {
if (!$firstRow) {
echo "<tr>";
} //else we already drew it
//its a subject so
echo "<td>$key</td>";
echo "<td>{$value['test1']}</td>";
echo "<td>{$value['test2']}</td>";
echo "<td>{$value['total']}</td>";
echo "<td>{$value['status']}</td>";
echo "<td>{$value['percentage']}</td>";
//only draw total for the first row
if ($firstRow) {
echo "<td rowspan=$numberOfSubjects>{$info['pass_count']}</td>";
echo "<td rowspan=$numberOfSubjects>{$info['gross_percentage']}</td>";
}
//close the row
echo "</tr>";
$firstRow = false;
}
}
}
?>
</tbody>
</table>
Here is the output:
Its based on a sample dataset I constructed from your description, included below for completeness:
<?php
$data = array(
"Ron" => Array
(
"subject1" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25,
"subject2" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "subject3" => Array
(
"test1" => 48
, "test2" => 86
, "total" => 134
, "percentage" => 89.33
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 900
, "gross_percentage" => 75.50
),
"John" => Array
(
"subject1" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25,
"subject2" => Array
(
"test1" => 47
, "test2" => 86
, "total" => 133
, "percentage" => 88.67
, "status" => Pass
)
, "subject3" => Array
(
"test1" => 48
, "test2" => 86
, "total" => 134
, "percentage" => 89.33
, "status" => Pass
)
, "pass_count" => 8
, "fail_count" => 0
, "gross_total" => 963
, "gross_percentage" => 80.25
)
);
Judging from your array, this might be something like what you're looking for:
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Subject</th>
<th>Test1 Marks</th>
<th>Test2 Marks</th>
<th>Total Marks</th>
<th>Status</th>
<th>Percentage</th>
<th>Pass Count</th>
<th>Total Percentage</th>
</tr>
</thead>
<tbody>
<?php foreach($arr as $name => $subjects): ?>
<?php $i = 0; ?>
<?php foreach($subjects as $subjectName => $subject): ?>
<?php if (is_array($subject)): ?>
<tr>
<?php if ($i === 0): ?>
<td rowspan="8"><?php echo $name; ?></td>
<?php endif; ?>
<td><?php echo $subjectName; ?></td>
<td><?php echo $subject['test1']; ?></td>
<td><?php echo $subject['test2']; ?></td>
<td><?php echo $subject['total']; ?></td>
<td><?php echo $subject['status']; ?></td>
<td><?php echo $subject['percentage']; ?></td>
<?php if ($i === 0): ?>
<td rowspan="8"><?php echo $subjects['pass_count']; ?></td>
<td rowspan="8"><?php echo $subjects['gross_percentage']; ?></td>
<?php endif; ?>
</tr>
<?php endif; ?>
<?php $i++; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</tbody>
</table>

Handling multi-dimensional array in PHP

I have a $row array value like this.
Array
(
[123] => Array
(
[0] => Array
(
[author_name] => A1
[book_name] => A1B1
[rating] => 5
)
[1] => Array
(
[author_name] => A1
[book_name] => A1B2
[rating] => 5.5
)
[2] => Array
(
[author_name] => A1
[book_name] => A1B3
[rating] => 5.7
)
)
[456] => Array
(
[0] => Array
(
[author_name] => A2
[book_name] => A2B1
[rating] => 7
)
)
)
I need to display the above array values like this.
Author Code Author Name Book Name Rating
123 A1 A1B1 5
AlB2 5.5
A1B3 5.7
456 A2 A2B1 7
I'm new to array concepts, and now I'm able to fetch the array alone.
Any php code will be of great help.
I don't know what other ways (or options) to print something like that, but alternatively, you can also restructure it. Consider this example:
<?php
$original_values = array(
123 => array(
array('author_name' => 'A1', 'book_name' => 'A1B1', 'rating' => 5),
array('author_name' => 'A1', 'book_name' => 'A1B3', 'rating' => 5.5),
array('author_name' => 'A1', 'book_name' => 'A1B1', 'rating' => 5.7),
),
456 => array(
array('author_name' => 'A2', 'book_name' => 'A2B1', 'rating' => 7),
),
);
$formatted_values = array();
foreach($original_values as $id => $array1) {
foreach($array1 as $index => $value) {
$formatted_values[$id][$value['author_name']][] = array('name' => $value['book_name'], 'rating' => $value['rating']);
}
}
?>
<table cellpadding="10">
<thead><tr><th>Author Code</th><th>Book Name</th><th>Book Name</th><th>Rating</th></tr></thead>
<tbody>
<?php foreach($formatted_values as $author_code => $value): ?>
<tr valign="top">
<td><?php echo $author_code; ?></td>
<?php foreach($value as $author_name => $books): ?>
<td><?php echo $author_name; ?></td>
<td><?php foreach($books as $book_info): ?><?php echo $book_info['name']; ?><br/><?php endforeach; ?></td>
<td><?php foreach($books as $book_info): ?><?php echo $book_info['rating']; ?><br/><?php endforeach; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Sample Output

How to print multidimensional arrays in php

I have an array in below format
Array ( [0] => Array ( [product_id] => 33 [amount] => 1 ) [1] => Array ( [product_id] => 34 [amount] => 3 ) [2] => Array ( [product_id] => 10 [amount] => 1 ) )
I want to get output from that array as below format
Product ID Amount
33 1
34 3
10 1
Can anyone please help me regarding this problem. var_dump of the variable is.
array
0 =>
array
'product_id' => string '33' (length=2)
'amount' => string '1' (length=1)
1 =>
array
'product_id' => string '34' (length=2)
'amount' => string '3' (length=1)
2 =>
array
'product_id' => string '10' (length=2)
'amount' => string '1' (length=1)
I believe this is your array
$array = Array (
0 => Array ( "product_id" => 33 , "amount" => 1 ) ,
1 => Array ( "product_id" => 34 , "amount" => 3 ) ,
2 => Array ( "product_id" => 10 , "amount" => 1 ) );
Using foreach
echo "<pre>";
echo "Product ID\tAmount";
foreach ( $array as $var ) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}
Using array_map
echo "<pre>" ;
echo "Product ID\tAmount";
array_map(function ($var) {
echo "\n", $var['product_id'], "\t\t", $var['amount'];
}, $array);
Output
Product ID Amount
33 1
34 3
10 1
<table>
<tr>
<th>Product Id</th>
<th>Ammount</th>
</tr>
<?php
foreach ($yourArray as $subAray)
{
?>
<tr>
<td><?php echo $subAray['product_id']; ?></td>
<td><?php echo $subAray['amount']; ?></td>
</tr>
<?php
}
?>
</table>
Try this..
foreach($arr as $a)
{
echo $a['product_id'];
echo $a['amount'];
}
Format as per your requirment.

Categories