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

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>

Related

Display array of array in view page PHP

I am getting the result of an array of array from controller.
Here one is the main array and under "bom_dt" is the array of array.
I know that how to display the main array, but not sure on the array under main array. I am stuck here. Please give me some suggestion on this,
Array
(
[0] => Array
(
[tih_sku] => 10001
[tih_style] => RHH/110/02
[tih_color] => BLACK
[tih_leather] => PERU
[bom_dt] => Array
(
[0] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 5, 22.5 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 2
[mi_sku] => RHH/110/02 BLACK
)
[1] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 8.75
[mi_sku] => RHH/110/02 BLACK
)
[2] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-MD1-NA-NA-NA-NA-NA-NA-NA-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHH/110/02 BLACK
)
[3] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-CCF2-NA-NA-NA-NA-NA-NA-NA-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHH/110/02 BLACK
)
[4] => Array
(
[mm_spec_fld] => *-*-GROSSGRAIN-GRANITE-*-*-*-*-*-*-*-*-*-----*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 0.9
[mi_sku] => RHH/110/02 BLACK
)
)
)
[1] => Array
(
[tih_sku] => 10009
[tih_style] => RHH/110/04
[tih_color] => BLACK
[tih_leather] => PERU
[bom_dt] => Array
(
[0] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 5, 44.5 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHH/110/04 BLACK
)
[1] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 7.75
[mi_sku] => RHH/110/04 BLACK
)
[2] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-FRAME-RECTANGULAR-16MM-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 2
[mi_sku] => RHH/110/04 BLACK
)
[3] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-R.S.FITTINGS-NA-NA-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 2
[mi_sku] => RHH/110/04 BLACK
)
[4] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-MD1-NA-NA-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHH/110/04 BLACK
)
)
)
[2] => Array
(
[tih_sku] => 10230
[tih_style] => RHN/208/02
[tih_color] => BLACK
[tih_leather] => OSCAR
[bom_dt] => Array
(
[0] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 5, 68 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHN/208/02 BLACK
)
[1] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 5-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 17
[mi_sku] => RHN/208/02 BLACK
)
[2] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 8
[mi_sku] => RHN/208/02 BLACK
)
[3] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-BRANDED ADJUSTER-NA-25MM-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHN/208/02 BLACK
)
)
)
Main array's result will be the common for the corresponding sub-array.
Can you suggest how to display it on view page using foreach loop.
You would want to implement a nested foreach as Raymond Nijland alluded.
<?php foreach ($items as $item): ?>
<tr>
<th>SKU</th>
<td><?php echo escape($item['tih_sku']); ?></td>
...
<th>BOM DT</th>
<td>
<?php if (count($item['bom_dt]) > 0): ?>
<?php foreach ($item['bom_dt'] as $bom_dt): ?>
//display the bom_dt details
<?php endforeach; ?>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
There are many ways to accomplish the above example, but please check the documentation for the php framework you are using. If you aren't using a php framework, you should absolutely implement something to escape the data you are writing to the page to protect yourself from attack.
try it like following
<?php
$var = array(
array(
'tih_sku' => '10001',
'tih_style' => 'RHH/110/02',
'tih_color' => 'BLACK',
'tih_leather' => 'PERU',
'bom_dt' => array(
array(
'mm_spec_fld' => 'NO 5, 44.5 CM - METAL - BLACK',
'tbid_qnty' => '1',
'mi_sku' => 'RHH/110/04 BLACK',
),
array(
'mm_spec_fld' => 'NO 3 - NYLON',
'tbid_qnty' => '7.75',
'mi_sku' => 'RHH/110/04 BLACK',
)
),
),
array(
'tih_sku' => '10009',
'tih_style' => 'RHH/110/04',
'tih_color' => 'BLACK',
'tih_leather' => 'PERU',
'bom_dt' => array(
array(
'mm_spec_fld' => 'NO 5, 68 CM - METAL - BLACK',
'tbid_qnty' => '1',
'mi_sku' => 'RHN/208/02 BLACK',
),
array(
'mm_spec_fld' => 'NO 5',
'tbid_qnty' => '17',
'mi_sku' => 'RHN/208/02 BLACK',
)
),
)
);
?>
<?php if(array_filter($var)){ ?>
<style>
table{width:50%;}
td{border:1px solid #999;padding:5px;}
</style>
<table>
<tr><td>Specification</td><td>Quantity</td><td>Code</td></tr>
<?php
$returnArray = array_map(function($v){ return $v['bom_dt'];},$var);
array_walk($returnArray, function($v2){array_walk($v2, function($v3){
?>
<tr><td><?=$v3['mm_spec_fld'];?></td><td><?=$v3['tbid_qnty'];?></td><td><?=$v3['mi_sku'];?></td></tr>
<?php
});});
?>
</table>
<?php } ?>
demo: https://paiza.io/projects/bhrSiwQi5zxtSMAz7ev0Wg?language=php

Print associative array to table

I have an associative array like this, i want to generate a table with these data, like breakfast, snacks, lunch, supper, dinner in row the following is the code I've tried, but I am stuck where to break the table row because when the array contains more than one item.i wants to Print associative array to table
*--------------------------------------------------*
| **Breakfast Snacks Lunch Supper Dinner** |
| test test test testfrom
|testfrom
*--------------------------------------------------*
array output is as follow
Array
(
[meal_plan_id] => 17
[calorie_limit] => 1
[total_calorie] => 0
[date] => 2017-12-29
[meal_plan] => Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 107
[label] => test
[quantity] => 10
[unit] => g
[status] => bf
)
[1] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 108
[label] => test
[quantity] => 121
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[status] => su
)
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[status] => sn
)
[1] => Array
(
[id] => 116
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 113
[label] => test500
[quantity] => 20
[unit] => oz
[status] => dn
)
)
)
)
)
below is the code I've tried
// $daily_meals = show_daily_meals() // contains the array
if(!empty($daily_meals['meal_plan'])){
echo '<tr>';
foreach($daily_meals['meal_plan'] as $meal_plan){
foreach ($meal_plan['data'] as $data){
if(!empty($data['id']))
echo '<td class="'.$meal_type.'" data-meals-id="'.$data['id'].'"><span class="left">'.$data['label'].'</span> <span class="right">'.$data['quantity'].' <a class="delete_row"><i class="fa fa-trash" aria-hidden="true"></i></a></span><div class="row_loader"></div></td>';
else echo '<td></td>';
}
$i++;
if($i%5 == 0) echo '</tr>';
}
}
Here is your solution
Input
<?php
$array = array(
array(
'meal_type' => 'bf',
'label' => 'Breakfast',
'calorie_limit' => 30,
'total_calorie' => 0,
'data' => array(
array(
'id' => 107,
'label' => 'test',
'quantity' => 10,
'unit' => 'g',
'status' => 'bf'
),
array(
'id' => 109,
'label' => 'testfrom',
'quantity' => 12,
'unit' => 'g'
)
)
),
array(
'meal_type' => 'sn',
'label' => 'Snacks',
'calorie_limit' => 10,
'total_calorie' => 0,
'data' => array(
array(
'id' => 108,
'label' => 'test',
'quantity' => 121,
'unit' => 'g'
)
)
),
array(
'meal_type' => 'lu',
'label' => 'Lunch',
'calorie_limit' => 20,
'total_calorie' => 0,
'data' => array(array('status' => 'su'))
),
array(
'meal_type' => 'su',
'label' => 'Supper',
'calorie_limit' => 30,
'total_calorie' => 0,
'data' => array(
array('status' => 'sn'),
array(
'id' => 116,
'label' => 'test',
'quantity' => 200,
'unit' => 'oz'
),
)
),
array(
'meal_type' => 'dn',
'label' => 'Dinner',
'calorie_limit' => 20,
'total_calorie' => 0,
'data' => array(
array(
'id' => 113,
'label' => 'test500',
'quantity' => 20,
'unit' => 'oz',
'status' => 'dn'
)
)
)
);
Solution
1st foreach for parent array. Then second for its child..
foreach($array as $r){
$$r['label'] = '<table width="100%">';
foreach($r['data'] as $s){
if(isset($s['label']))$$r['label'] .= '<tr><td align="center">'.$s['label'].'</td></tr>'; // The if condition check weather the label is exist or not if yes then add that in particular label's table
}
$$r['label'] .= '</table>';
}
echo '
<table width="100%" border="1">
<thead>
<tr>
<th>Breakfast</th>
<th>Snacks</th>
<th>Lunch</th>
<th>Supper</th>
<th>Dinner</th>
</tr>
</thead>
Here all labels canverted into variable by $$r['label']. And all have there own table Now we add all these tables into the master table to get the output.
<tbody>
<tr>
<td>'.$Breakfast.'</td>
<td>'.$Snacks.'</td>
<td>'.$Lunch.'</td>
<td>'.$Supper.'</td>
<td>'.$Dinner.'</td>
</tr>
</tbody>
</table>';
//?// echo "<pre>";print_r($array);
?>
Output
Try storing all your column names in an array and create a table with it:
foreach($array as $row) {
$column[$i]['label'] = $row['label'];
$column[$i]['data'] = $this->getdata($row['data']);
$i++;
}
function getdata($array) {
$data = '';
foreach($array as $row) {
$data. = $row['label'].',';
}
return $data;
}
Create a table with the column names and try inserting data using a loop with respect to insert into

foreach loop is not working after print array

I've tried to loop of following array.
Array
(
[mech_info] => Array
(
[make] => Amaka
[0] => Array
(
[year] => 2001
[model] => Array
(
[0] => Test one
[1] => test fix
[2] => Hamour
[3] => Imagica
)
)
[1] => Array
(
[year] => 2002
[model] => Array
(
[0] => Test Two
)
)
[2] => Array
(
[year] => 2014
[model] => Array
(
[0] => Test three
)
)
[3] => Array
(
[year] => 2015
[model] => Array
(
[0] => test four
)
)
)
)
Array
(
[mech_info] => Array
(
[make] => PRI
[0] => Array
(
[year] => 2005
[model] => Array
(
[0] => PRIMODE
[1] => Temp Pri
[2] => primode
[3] => yyy
)
)
)
)
I want to do it with foreach loop. I have tried by following code but it is not show anything except
`print_r($_POST['mech_show']);`.
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model_data as $key => $mec_value) {
echo "string";
echo $meta_value['make'];
}
echo "<pre>";
print_r($_POST['mech_show']);
exit();
also not able to go under foreach and data not print in loop.
given me error
Notice: Undefined index: mech_info
Warning: Invalid argument supplied for foreach() in
i also trie this way but
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $_POST['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
echo "<pre>";
print_r($all_make_model['mech_info']);
but it's showing Warning: Illegal string offset 'mech_info' in ..
I don't know if my code is wrong or I'm missing something anyone pls help me.
Thank You
Some change your foreach loop. it $meta_value['make'] should be $mec_value['make']
So,
$all_make_model= $_POST['mech_show'];
//$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
try
$all_array=array("mech_info"=>array("make"=>"Amaka",array("year"=>2001,"model"=>array("one","two","three")),array("year"=>2002,"model"=>array("one","two","three")),array("year"=>2003,"model"=>array("one","two","three")),array("year"=>2004,"model"=>array("one","two","three"))),array("mech_info"=>array("make"=>"PRI",array("year"=>2001,"model"=>array("one","two","three")))));
$all_make_model= $all_array;
//$all_make_model_data = $all_make_model['mech_info'];
//print_r($all_make_model['mech_info']);
foreach ($all_make_model['mech_info'] as $key => $mec_value) {
if(is_numeric($key)) continue;
echo $mec_value; // output Amaka
}
exit();
This Code just work.
To iterate on multiple mech_info, i added a leave in the array, because otherwise you are tring to create multiple object with the same index.
$p = Array('mech_show' => Array(
0 => Array(
'mech_info' => Array(
'make' => 'Amaka',
'0' => Array(
'year' => 2001,
'model' => Array(
0 => 'Test one',
1 => 'test fix',
2 => 'Hamour',
3 => 'Imagica'
)
),
'1' => Array(
'year' => 2002,
'model' => Array(
0 => 'Test Two'
)
),
'2' => Array(
'year' => 2014,
'model' => Array(
0 => 'Test three'
)
),
'3' => Array(
'year' => 2015,
'model' => Array
(
0 => 'test four'
)
)
)
),
1=>Array(
'mech_info' => Array(
'make' => 'PRI',
'0' => Array(
'year' => 2005,
'model' => Array(
0 => 'PRIMODE',
1 => 'Temp Pri',
2 => 'primode',
3 => 'yyy'
)
)
)
)
)
);
$all_make_model= $p['mech_show'];
foreach($all_make_model as $all_make_model_data){
foreach($all_make_model_data as $mech_info)
var_dump($mech_info['make']);
}
where you have to replace $p with $_POST

get values from database and sort them based on other fields

I'm using php and mysql -[codeigniter framework]- and this is my table structure:
Please visit this image to find out table structure
Every single item is separated to three or four parts that these parts made this item's price.
And these items belong to an invoice that will point using refid. refid is invoice_id in fact.
now I need to get a specific invoice items and show them in a table.
this is what I need:
Please visit this image to find out my need
I do not have any problem to get values from database and I will use codeigniter active records.
As you may know, codeigniter database result_array function will give us an associative array. Now I want to separate every item from other and display them in a html table and I have to use rowspan for every item dependence sub items.
Would you please help me to find out how should I use result array to simply show the result in a html table?
so lets assume that you are getting a result array like this,
$result = array(
array(
'id' => 26,
'price' => 100,
'parent' => 0
),
array(
'id' => 27,
'price' => 100,
'parent' => 26
),
array(
'id' => 28,
'price' => 150,
'parent' => 26
),
array(
'id' => 29,
'price' => 100,
'parent' => 26
),
array(
'id' => 30,
'price' => 100,
'parent' => 0
),
array(
'id' => 31,
'price' => 99,
'parent' => 30
),
array(
'id' => 32,
'price' => 75,
'parent' => 30
),
array(
'id' => 33,
'price' => 600,
'parent' => 0
),
array(
'id' => 34,
'price' => 100,
'parent' => 33
),
array(
'id' => 35,
'price' => 50,
'parent' => 33
),
array(
'id' => 36,
'price' => 100,
'parent' => 33
),
);
i cant insert all the data manually in the array from your image, so i have inserted only the needed once,
so now u can do something like this to group the data of same item,
$new_array = array();
$invoice_price = 0;
foreach ($result as $key => $value) {
$invoice_price += $value['price'];
if($value['parent'] == 0){
$new_array[$value['id']]['other_data'][] = $value;
$new_array[$value['id']]['final_price'] = $value['price'];
}else{
$new_array[$value['parent']]['other_data'][] = $value;
$new_array[$value['parent']]['final_price'] += $value['price'];
}
}
now if u print this $new_array, u will get the following output,
Array
(
[26] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 26
[price] => 100
[parent] => 0
)
[1] => Array
(
[id] => 27
[price] => 100
[parent] => 26
)
[2] => Array
(
[id] => 28
[price] => 150
[parent] => 26
)
[3] => Array
(
[id] => 29
[price] => 100
[parent] => 26
)
)
[final_price] => 450
)
[30] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 30
[price] => 100
[parent] => 0
)
[1] => Array
(
[id] => 31
[price] => 99
[parent] => 30
)
[2] => Array
(
[id] => 32
[price] => 75
[parent] => 30
)
)
[final_price] => 274
)
[33] => Array
(
[other_data] => Array
(
[0] => Array
(
[id] => 33
[price] => 600
[parent] => 0
)
[1] => Array
(
[id] => 34
[price] => 100
[parent] => 33
)
[2] => Array
(
[id] => 35
[price] => 50
[parent] => 33
)
[3] => Array
(
[id] => 36
[price] => 100
[parent] => 33
)
)
[final_price] => 850
)
)
hope u can see that the data is already groups by items.
now just send the data in your view file,
$this->load->view('yourviewfilename',array('data'=>$new_array,'invoice_price'=>$invoice_price));
and to print the table like your screenshot use the following code,
<table border="1">
<tr>
<th>ID</th>
<th>parent</th>
<th>price</th>
<th>final price</th>
</tr>
<?php $count = 0; ?>
<?php foreach ($data as $key => $value) { ?>
<?php $count++; ?>
<?php foreach ($value['other_data'] as $k => $v) { ?>
<tr>
<?php if ($k == 0) { ?>
<td rowspan="<?php echo count($value['other_data']); ?>"><?php echo $count ?></td>
<?php } ?>
<td> <?php echo $v['parent']; ?></td>
<td> <?php echo $v['price']; ?></td>
<?php if ($k == 0) { ?>
<td rowspan="<?php echo count($value['other_data']); ?>"><?php echo $value['final_price']; ?></td>
<?php } ?>
</tr>
<?php } ?>
<?php } ?>
</table>
u will get output like this,
hope u got the point and can print the other data you needed.

how to make tabular data from nested array

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>

Categories