how to create table using multiple dimensional array in php - php

Actually This are my real data from API.
$main2 = array(
'sup1' => array(
'01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TATA'),
'04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 10, "source_type" => 'Rel'),
),
'sup2' => array(
'01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TCS'),
'04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 55, "source_type" => 'Jio'),
),
);
We need same out put using this array.
we create table all date in header like first key date as display in header.
-then data display supplier sup1 in first row
sup2 in second row row
like below out put
|Supplier Name |01-Jun-2019-TO-03-Jun-2019 |04-Jun-2019-TO-08-Jun-2019|<br/>
|sup1|54|TATA|10|Rel|
<br/>
|sup2|54|TCS|55|JIO|

Here is your code modified:
<?php
$main2 = array(
'01-Jun-2019-TO-03-Jun-2019' => array(
'sup1' => array("connection_count" => 54, "source_type" => 'TATA'),
'sup2' => array("connection_count" => 10, "source_type" => 'Rel'),
),
'04-Jun-2019-TO-08-Jun-2019' => array(
'sup1' => array("connection_count" => 54, "source_type" => 'TCS'),
'sup2' => array("connection_count" => 55, "source_type" => 'Jio'),
),
);
$suplist = array('sup1', 'sup2');
echo "<table border=1><tr>";
echo "<td>Supplier Name</td>";
foreach ($main2 as $key => $value) {
echo "<td colspan=2>" . $key . "</td>";
}
echo "</tr>";
foreach ($suplist as $supplier) {
echo "<tr><td>$supplier</td>";
foreach ($main2 as $value) {
echo "<td>" . $value[$supplier]['connection_count'] . "</td>";
echo "<td>" . $value[$supplier]['source_type'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
And here is sample output (reformatted for clarity):
<table border=1>
<tr>
<td>Supplier Name</td>
<td colspan=2>01-Jun-2019-TO-03-Jun-2019</td>
<td colspan=2>04-Jun-2019-TO-08-Jun-2019</td>
</tr>
<tr>
<td>sup1</td>
<td>54</td>
<td>TATA</td>
<td>54</td>
<td>TCS</td>
</tr>
<tr>
<td>sup2</td>
<td>10</td>
<td>Rel</td>
<td>55</td>
<td>Jio</td>
</tr>
</table>
Here is your code which works with updated input data format:
$main2 = array(
'sup1' => array(
'01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TATA'),
'04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 10, "source_type" => 'Rel'),
),
'sup2' => array(
'01-Jun-2019-TO-03-Jun-2019' => array("connection_count" => 54, "source_type" => 'TCS'),
'04-Jun-2019-TO-08-Jun-2019' => array("connection_count" => 55, "source_type" => 'Jio'),
),
);
$suppliers = array_keys($main2);
$dateRanges = [];
foreach ($main2 as $supplierData) {
$dateRanges = array_merge($dateRanges, array_keys($supplierData));
}
$dateRanges = array_values(array_unique($dateRanges));
sort($dateRanges);
echo "<table border=1><tr>";
echo "<td>Supplier Name</td>";
foreach ($dateRanges as $dateRange) {
echo "<td colspan=2>" . $dateRange . "</td>";
}
echo "</tr>";
foreach ($suppliers as $supplier) {
echo "<tr><td>$supplier</td>";
foreach ($dateRanges as $dateRange) {
if (isset($main2[$supplier][$dateRange])) {
echo "<td>" . $main2[$supplier][$dateRange]['connection_count'] . "</td>";
echo "<td>" . $main2[$supplier][$dateRange]['source_type'] . "</td>";
} else {
echo "<td></td><td></td>";
}
}
echo "</tr>";
}
echo "</table>";
And here is sample output (formatted for clarity):
<table border=1>
<tr>
<td>Supplier Name</td>
<td colspan=2>01-Jun-2019-TO-03-Jun-2019</td>
<td colspan=2>04-Jun-2019-TO-08-Jun-2019</td>
</tr>
<tr>
<td>sup1</td>
<td>54</td>
<td>TATA</td>
<td>10</td>
<td>Rel</td>
</tr>
<tr>
<td>sup2</td>
<td>54</td>
<td>TCS</td>
<td>55</td>
<td>Jio</td>
</tr>
</table>

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.

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>

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>

Creating a table with PHP

I am trying to create a table using PHP.
For some reason I can't display the key. Any idea why?
Here's the table.
Here's my code that I tried:
<table>
<?php
$users = array(
array('first_name' => 'RS', 'last_name' => 'AD'),
array('first_name' => 'SQ', 'last_name' => 'FS'),
array('first_name' => 'SA', 'last_name' => 'Guillen'),
array('first_name' => 'AS', 'last_name' => 'Gs')
);
$array_number = count($users)-1;
foreach($users as $key => $user) {
for($i=0; $i<=$array_number; $i++){
echo $users[$i]['first_name'].' '. $users[$i]['last_name'].'<br>';
}
}
?>
</table>
Any idea?
Try this
<table>
<tr>
<th>User #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Full Name</th>
<th>Full Name in UpperCase</th>
</tr>
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
foreach ($users as $key => $user) {
echo "<tr>";
echo "<td>" , $key + 1 , "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "<td>" . $user['first_name'] . " " . $user['last_name'] . "</td>";
echo "<td>" . strtoupper($user['first_name']) . " " . strtoupper($user['last_name']) . "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
</table>
Brief Explanation:
As you have shown within your image you need to define predefined headings which can be done using <th></th> and rest of those values you were getting from an $users array which can be placed within loop.
Note: Here you need to use single loop not nested loops as shown in example
The problem is that you loop over the array twice, once with the foreach and inside that with the for. But you need only one loop. Choose one.
Also, make it a habit to always use htmlspecialchars when echoing the contents of a variable. In this case it won't matter, but sometimes, you won't know where the variable has been!
Also, see Amol's answer.
You should use <tr> and <td>.
<table border="1">
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
$array_number = count($users)-1;
for($i=0; $i<=$array_number; $i++){
?>
<tr> <td><?php echo $users[$i]['first_name']; ?></td><td><?php echo $users[$i]['last_name']; ?></td> </tr>
<?php }
?>
</table>
<table> <?php
$users = array( array('first_name' => 'Michael', 'last_name' => 'Choi'), array('first_name' => 'John', 'last_name' => 'Supsupin'), array('first_name' => 'Mark', 'last_name' => 'Guillen'), array('first_name' => 'KB', 'last_name' => 'Tonel') );
$i = 1;
foreach($users as $key => $user) {
echo "<tr>";
echo "<td>" . $i . "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "</tr>"; $i++;
}
?>
</table>
Hope that will help
<table>
<tr>
<th>User #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Full Name</th>
<th>Full Name in UpperCase</th>
</tr>
<?php
$users = array(
array('first_name' => 'Michael', 'last_name' => 'Choi'),
array('first_name' => 'John', 'last_name' => 'Supsupin'),
array('first_name' => 'Mark', 'last_name' => 'Guillen'),
array('first_name' => 'KB', 'last_name' => 'Tonel')
);
foreach ($users as $key => $user) {
echo "<tr>";
echo "<td>" , $key + 1 , "</td>";
echo "<td>" . $user['first_name'] . "</td>";
echo "<td>" . $user['last_name'] . "</td>";
echo "<td>" . $user['first_name'] . " " . $user['last_name'] . "</td>";
echo "<td>" . strtoupper($user['first_name']) . " " . strtoupper($user['last_name']) . "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
`

PHP Sum Children in Array

Hey I have an array where I would like to sum the values of the first-level children at various levels of the array.
<?php
$group = Array('Electronics' => Array(
'6 - Cameras & Supplies' => Array(
'Cameras' => Array(
'Camcorders' => Array (
'Action Camcorders' => Array (
'total_ty_yest_sales' => 70.83,
'total_wo_dotcom_ty_yest_sales' => 401.59,
'east_ty_yest_sales' => 65.20
),
'Standard Camcorders' => Array (
'total_ty_yest_sales' => 96.09,
'total_wo_dotcom_ty_yest_sales' => 96.09,
'east_ty_yest_sales' => 68.21
),
'Surveillance' => Array(
'total_ty_yest_sales' => 84.00,
'total_wo_dotcom_ty_yest_sales' => 84.00,
'east_ty_yest_sales' => 26.00
)
),
'subCameras' => Array (
'Big Zoom' => Array(
'total_ty_yest_sales' => 31.66,
'total_wo_dotcom_ty_yest_sales' => 13.68,
'east_ty_yest_sales' => 1.47
),
'Pegged Cameras' => Array(
'total_ty_yest_sales' => 13.50,
'total_wo_dotcom_ty_yest_sales' => 5.50,
'east_ty_yest_sales' => 5.00
),
'Point-N-Shoot' => Array(
'total_ty_yest_sales' => 46.61,
'total_wo_dotcom_ty_yest_sales' => 10.35,
'east_ty_yest_sales' => 4.06
),
'Rugged Cameras' => Array(
'total_ty_yest_sales' => 87.04,
'total_wo_dotcom_ty_yest_sales' => 87.04,
'east_ty_yest_sales' => 65.20
),
'SLR' => Array(
'total_ty_yest_sales' => 50.19,
'total_wo_dotcom_ty_yest_sales' => 9.40,
'east_ty_yest_sales' => 1.37
)
)
) )
));
$totalSum = 0;
echo "<table>\n
<thead></thead>\n
<tbody>\n";
foreach($group as $gmm => $acctg_dept_nbrs) {
echo "<tr class=\"header\">\n
<td>" . $gmm . "</td>\n";
foreach ($acctg_dept_nbrs as $acctg_dept_nbr => $dept_catg_grp_descs) {
echo "<tr class=\"header\">\n
<td style=\"padding-left: 1em;\">" . $acctg_dept_nbr . "</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
$total_acctg_dept_nbr = 0;
foreach($dept_catg_grp_descs as $dept_catg_grp_desc => $dept_category_descs) {
echo "<tr class=\"header\">\n
<td style=\"padding-left: 2em;\">" . $dept_catg_grp_desc . "</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
$total_dept_catg_grp_desc = 0;
foreach($dept_category_descs as $dept_category_desc => $dept_subcatg_descs) {
echo "<tr class=\"header\">\n
<td style=\"padding-left: 3em;\">" . $dept_category_desc . "</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
echo "<td style=\"width:100px;\">SUM</td>\n";
foreach($dept_subcatg_descs as $dept_subcatg_desc => $values) {
echo "<tr>\n
<td style=\"padding-left: 4em;\">" . $dept_subcatg_desc . "</td>\n";
$sum = $values['total_ty_yest_sales'];
echo "<td style=\"width:100px;\">".$sum."</td>\n";
$sum = $values['total_wo_dotcom_ty_yest_sales'];
echo "<td style=\"width:100px;\">".$sum."</td>\n";
$sum = $values['east_ty_yest_sales'];
echo "<td style=\"width:100px;\">".$sum."</td>\n";
}
}
}
}
}
?>
I want to replace the "SUM"s in the output with the sum of the values of next level of indention.
This is not the most efficient thing to do, but you can create a function that calculates the sum of an array recursively:
function array_sum_recursive($array)
{
$sum = 0;
array_walk_recursive($array, function($item) use (&$sum) {
$sum += $item;
});
return $sum;
}

Categories