Build table with rowspan using multidimensional array - php

I want students and events for week to be displayed in calendar format.I have this array as follows. Every student has 7 arrays starting from monday to sunday and inner array of each has events for day
$array = [
'Alex' => [
[
['event' => 'eventName1'],['event' => 'eventName2']
],
[
['event' => 'eventName3'],['event' => 'eventName4']
],
[
['event' => 'eventName5'],['event' => 'eventName6']
],
[
['event' => 'eventName7'],['event' => 'eventName8']
],
[],
[],
[]
],
'Allen' => [
[
['event' => 'eventName'],['event' => 'eventName']
],[
['event' => 'eventName'],['event' => 'eventName']
],
[],
[],
[],
[],
[]
],
];
I need the array to displayed as week calendar,
I don't know where it gone wrong with following code
<table>
<thead>
<tr>
<th>Participant</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thrusday</th>
<th>Friday</th>
<th>Satday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
<?php foreach ($array as $participant => $event): ?>
<?php foreach ($event as $i => $value):?>
<tr>
<?php if ($i === 0): ?>
<td rowspan="2"><?= $participant ?></td>
<?php endif ?>
<?php foreach ($value as $index => $eventD):?>
<td><?php echo $eventD['event']; ?></td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
<?php endforeach ?>
</tbody>
</table>
My problem is the loop and how to print the the events of the particular day.
Can anyone please in building the correct format. thanks for help

<?php
$array = [
'Alex' => [
[
['event' => 'eventName1'],['event' => 'eventName2']
],
[
['event' => 'eventName3'],['event' => 'eventName4']
],
[
['event' => 'eventName5'],['event' => 'eventName6']
],
[
['event' => 'eventName7'],['event' => 'eventName8']
],
[],
[],
[]
],
'Allen' => [
[
['event' => 'eventName'],['event' => 'eventName'],['event' => 'eventName'],['event' => 'eventName'],
],[
['event' => 'eventName'],['event' => 'eventName']
],
[],
[],
[],
[],
[]
],
];
$maxRows = [];
foreach ($array as $participant => $weekEvents) {
$max = 0;
foreach ($weekEvents as $dayEvents) {
if (count($dayEvents) > $max) {
$max = count($dayEvents);
}
}
$maxRows[$participant] = $max;
}
?>
<table border="1">
<thead>
<tr>
<th>Participant</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thrusday</th>
<th>Friday</th>
<th>Satday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
<?php foreach ($array as $participant => $weeklyEvents): ?>
<?php for ($i = 0; $i < $maxRows[$participant]; $i++): ?>
<tr>
<?php if ($i === 0): ?>
<td rowspan="<?php echo $maxRows[$participant]; ?>">
<?php echo $participant; ?>
</td>
<?php endif; ?>
<?php for ($j = 0; $j < 7; $j++): ?>
<td>
<?php $dayEvents = $weeklyEvents[$j]; ?>
<?php if (isset($dayEvents[$i])): ?>
<?php echo $dayEvents[$i]['event']; ?>
<?php else: ?>
<?php endif; ?>
</td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
<?php endforeach; ?>
</tbody>
</table>

Check this html
<table border="1">
<thead>
<tr>
<th>Participant</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thrusday</th>
<th>Friday</th>
<th>Satday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
<?php foreach ($array as $participant => $event): ?>
<tr>
<td><?= $participant ?></td>
<?php foreach ($event as $i => $value): ?>
<td>
<?php foreach ($value as $index => $eventD): ?>
<?php echo $eventD['event'] . '<br/>'; ?>
<?php endforeach ?>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>

Try like this if my understanding is correct
<?php
$array = [
'Alex' => [
[
['event' => 'eventName1'],['event' => 'eventName2']
],
[
['event' => 'eventName3'],['event' => 'eventName4']
],
[
['event' => 'eventName5'],['event' => 'eventName6']
],
[
['event' => 'eventName7'],['event' => 'eventName8']
],
[],
[],
[]
],
'Allen' => [
[
['event' => 'eventName'],['event' => 'eventName']
],[
['event' => 'eventName'],['event' => 'eventName']
],
[],
[],
[],
[],
[]
],
];
echo "<table border='1'>
<thead>
<tr>
<th>Participant</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thrusday</th>
<th>Friday</th>
<th>Satday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>";
foreach($array as $key => $data)
{
echo "<tr>
<td>$key</td>";
foreach($data as $data2) {
if(is_array($data2) && isset($data2[0])) {
echo "<td>";
array_walk($data2, function($val, &$dat){
echo $val['event'].', ';
});
echo "</td>";
} else {
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</tbody>
</table>";
?>
fiddle link http://sandbox.onlinephpfunctions.com/

Related

Please help about Codeigniter, about sales input

this models M_penjualan.php
function simpan_penjualan($nofak,$total,$jml_uang,$kembalian){
$idadmin=$this->session->userdata('idadmin');
$this->db->query("INSERT INTO tbl_jual (jual_nofak,jual_total,jual_jml_uang,jual_kembalian,jual_user_id,jual_keterangan) VALUES ('$nofak','$total','$jml_uang','$kembalian','$idadmin','eceran')");
foreach ($this->cart->contents() as $item) {
$data=array(
'd_jual_nofak' => $nofak,
'd_jual_barang_id' => $item['id'],
'd_jual_barang_nama' => $item['name'],
'd_jual_barang_satuan' => $item['satuan'],
'd_jual_barang_harpok' => $item['harpok'],
'd_jual_barang_harjul' => $item['amount'],
'd_jual_qty' => $item['qty'],
'd_jual_diskon' => $item['disc'],
'd_jual_total' => $item['subtotal']
);
$this->db->insert('tbl_detail_jual',$data);
$this->db->query("update tbl_barang set barang_stok=barang_stok-'$item[qty]' where barang_id='$item[id]'");
}
return true;
this class CI_Controller Penjualan.php
function add_to_cart(){
if($this->session->userdata('akses')=='1' || $this->session->userdata('akses')=='2'){
$kobar=$this->input->post('kode_brg');
$produk=$this->m_barang->get_barang($kobar);
$i=$produk->row_array();
$data = array(
'id' => $i['barang_id'],
'name' => $i['barang_nama'],
'satuan' => $i['barang_satuan'],
'harpok' => $i['barang_harpok'],
'price' => str_replace(",", "", $this->input->post('harjul'))-$this->input->post('diskon'),
'disc' => $this->input->post('diskon'),
'qty' => $this->input->post('qty'),
'amount' => str_replace(",", "", $this->input->post('harjul'))
);
if(!empty($this->cart->total_items())){
foreach ($this->cart->contents() as $items){
$id=$items['id'];
$qtylama=$items['qty'];
$rowid=$items['rowid'];
$kobar=$this->input->post('kode_brg');
$qty=$this->input->post('qty');
if($id==$kobar){
$up=array(
'rowid'=> $rowid,
'qty'=>$qtylama+$qty
);
$this->cart->update($up);
}else{
$this->cart->insert($data);
}
}
}else{
$this->cart->insert($data);
}
the latter view v_penjualan.php
<?php $i = 1;?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td><?=$items['id'];?></td>
<td><?=$items['name'];?></td>
<td><?=$items['satuan'];?></td>
<td><?php echo number_format($items['amount']);?></td>
<td><?php echo number_format($items['disc']);?></td>
<td><?php echo number_format($items['qty']);?></td>
<td><?php echo number_format($items['subtotal']);?></td>
<td class="text-center"><span class="far fa-trash-alt mr-2"></span> Hapus</td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
in the above code when inputting and leads to add_cart "$rowid=$items['rowid'];" which is displayed Only 6 Rows, I can't add to the 7th input and so on, please help, is there any missing code? thanks for the help.

How to get foreach in controller into table view in blade laravel

I have been trying to get data from google analytics in laravel. It will show the pathpage and the pageviews in a form of table. I have retrieved the data using spatie/laravel-analytics but when I display it on the view the page views is not tally to the google analytics. So I have been trying to sort the data using the following code but keep getting errors
Here is my controller:
public function google(MediaSite $mediaSite)
{
$ga7day = Analytics::performQuery(
Period::days(7),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$ga1month = Analytics::performQuery(
Period::days(30),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$ga3month = Analytics::performQuery(
Period::months(3),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$analyticsDataToday = Analytics::performQuery(
Period::days(7),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$mediaSite = new MediaSite;
$mediaSite = MediaSite::Where('media_owner_id','1');
foreach ($mediaSite as $site) {
$site->view7days = 0;
foreach ($ga7day as $ga) {
# code...
if($ga['1'] === '/media-site/'. $site->id);
$site->view7days = $ga['3'];
# code...
}
$site->view1month = 0;
foreach ($ga1month as $mga) {
# code...
if($mga['1'] === '/media-site/'. $site->id);
$site->view1month = $mga['3'];
# code...
}
$site->view3month = 0;
foreach ($ga3month as $nga) {
# code...
if($nga['1'] === '/media-site/' . $site->id);
$site->view3month = $nga['3'];
# code...
}
}
return
view('analytics.google',compact('ga7day','ga1month','ga3month',
'analyticsDataToday','site','mediaSite','ga','mga','nga'));
my view:
<body>
<form method="get" action="{{ action('AnalyticsController#google') }}">
{{ csrf_field() }}
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="eg: Media Ownner" required>
<span class="input-group-btn">
<button type="submit" class="btn btn-default">
Go!
</button>
</span>
</div>
</form>
<br>
<br>
<table class="table">
<tr>
<th> Media Site </th>
<th> 7 days</th>
<th> 1 month</th>
<th> 3 month</th>
<th> Overall</th>
</tr>
<tr>
#foreach ($ga as $index => $key)
<tr> <td> {{ $key['1'] }} </td>
<td> {{ $key['3'] }} </td>
<td> {{ $ga1month[$index]['3'] }}</td>
<td> {{ $ga3month[$index]['3'] }}</td>
<td> {{ $analyticsDataToday[$index]['3'] }}</td></tr>
#endforeach
</tr>
</table>
</body>
</html>
I keep getting these errors:
enter image description here
Replace your controller with the following
public function google(MediaSite $mediaSite) {
$ga7day = Analytics::performQuery( Period::days(7),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$ga1month = Analytics::performQuery( Period::days(30),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$ga3month = Analytics::performQuery( Period::months(3),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$analyticsDataToday = Analytics::performQuery( Period::days(7),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$mediaSite = new MediaSite;
$mediaSite = MediaSite::Where('media_owner_id','1');
foreach ($mediaSite as $site) {
$site->view7days = 0;
foreach ($ga7day as $ga) {
# code...
if($ga['1'] === '/media-site/'. $site->id);
$site->view7days = $ga['3'];
# code...
}
$site->view1month = 0;
foreach ($ga1month as $mga) {
# code...
if($mga['1'] === '/media-site/'. $site->id);
$site->view1month = $mga['3'];
# code...
}
$site->view3month = 0;
foreach ($ga3month as $nga) {
# code...
if($nga['1'] === '/media-site/' . $site->id);
$site->view3month = $nga['3'];
# code...
}
}
return view('analytics.google',compact('mediaSite'));
}
And in the view
#foreach ($meadiaSite as $site)
<tr>
<td> {{ $site['1'] }} </td>
<td> {{ $site->view7days }} </td>
<td> {{ $site->view1month }}</td>
<td> {{ $site->view3month }}</td>
<td> {{ /*same logic*/ }}</td>
</tr>
#endforeach

Show array data in HTML table multidimensional array in php

I have an array like this:-
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test',
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
Now I show this data in HTML table like this for user 'test' :-
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</thead>
<tbody>
<tr>
<td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['amount'];
echo "<br />";
}
}
?></td><td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['user'];
echo "<br />";
}
}
?></td>
</tr>
</tbody>
But now the problem is that when I use this code it shows the amount and user as a whole instead of two different rows. How can I fix this? Any help?
You just need to move your foreach loop outside of the <tr>...</tr> structure. This should work:
<?php foreach($str as $new_str){
if($new_str['user']=="test"){
echo "<tr><td>" . $new_str['amount'] . "</td><td>" . $new_str['user'] . "</td></tr>";
}
}
?>
Output (for your data)
<tr><td>0.9</td><td>test</td></tr>
<tr><td>1.4</td><td>test</td></tr>
Your tr was not repeating. output image I hope this will help.
<?php
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test' ,
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
?>
<table>
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</tr>
</thead>
<tbody>
<?php foreach($str as $new_str) {
if($new_str['user'] == "test"){
echo '<tr>';
echo '<td>'.$new_str['amount'].'</td>';
echo '<td>'.$new_str['user'].'</td>';
echo '</tr>';
}
} ?>
</tbody>
</table>

Codeigniter explode filename and return to view only gives one file

I have multiple files that have this name format field1_field2_field3.pdf. I separated each field and I want to return it to the view and paginate it. Can it be done? So far I have managed to explode the filename but only one file is returned to the view. Please help.
Controller :
$this->load->helper('directory');
$map = directory_map('./assets/data/');
$nric = $this->session->userdata('nric');
foreach($map as $row)
{
$separate = explode('_',$row);
}
$data['name'] = $separate[0];
$data['product'] = $separate[1];
$data['policyno'] = substr($separate[2],0,strlen($separate[2])-4);
$this->load->view('includes/user/header');
$this->load->view('user/statements',$data);
$this->load->view('includes/user/footer');
View
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $product; ?></td>
<td><?php echo $policyno; ?></td>
</tr>
In your Controller you want the data array to contain multiple value so do something like this
EDIT: I am assuming that your $map variable is something like this (do a print_r if you want to see)
$map = [
'field1_field2_field3.pdf',
'field11_field22_field33.pdf',
'field111_field222_field333.pdf'
]
So using the loop
foreach($map as $row)
{
$separate = explode('_',$row);
$data[] = [
'name' => $separate[0],
'product' => $separate[1],
'policyno' => substr($separate[2],0,strlen($separate[2])-4)
];
}
Now doing as mentioned above in the foreach loop will give you $data like
$data = [
0 => [
'name' => 'field1',
'product' => 'field2',
'policyno' => 'field3',
],
1 => [
'name' => 'field11',
'product' => 'field22',
'policyno' => 'field33',
],
2 => [
'name' => 'field111',
'product' => 'field222',
'policyno' => 'field333',
],
]
this $data you can pass to your view, as you were doing
$this->load->view('user/statements',$data);
then in your view you can have something like
<table>
<?php foreach ($data as $dataRow):?>
<tr>
<td><?php echo $dataRow['name']?></td>
<td><?php echo $dataRow['product']?></td>
<td><?php echo $dataRow['policyno']?></td>
</tr>
<?php endforeach?>
</table>
I think this should work :)
Now use some fancy JavaScript plug-in and create a paginated view

How to change multidimensional array into html table report php?

I have a multidimensional array like this:-
$data = array (
'SalaryHeadName' =>
array (
0 => 'Basic',
1 => 'PF',
),
'SalaryHeadType' =>
array (
0 => 'CR',
1 => 'DR',
),
'Amount' =>
array (
0 => 6000,
1 => 400,
),
)
how to change it into html table report like by using foreach loop or for loop thank in advance.
This is not general, its specific to your problem.
$data = array('SalaryHeadName'=>array(0=>'Basic',1=>'PF'),'SalaryHeadType'=>array(0=>'CR',1=>'DR'),'Amount'=>array(0=>6000,1=>400));
$array_keys = array_keys($data);
<table border="1">
<thead>
<?php foreach ($array_keys as $key) {?>
<th><?php echo $key ?></th>
<?php } ?>
</thead>
<tbody>
<?php for ($i=0; $i<count($data['SalaryHeadName']);$i++) {?>
<tr>
<td><?php echo $data['SalaryHeadName'][$i] ?></td>
<td><?php echo $data['SalaryHeadType'][$i] ?></td>
<td><?php echo $data['Amount'][$i] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
I hope this is what you want,
<?php
$data = array(
'SalaryHeadName' => array(
0 => 'Basic',
1 => 'PF'
) ,
'SalaryHeadType' => array(
0 => 'CR',
1 => 'DR'
) ,
'Amount' => array(
0 => 6000,
1 => 400
)
);
?>
<table border="1" cellpadding="10">
<?php
foreach($data as $key => $value)
{
echo "<tr><td>$key</td>";
foreach($value as $value1)
{
echo "<td>$value1</td>";
}
echo "</tr>";
}
?>
</table>

Categories