Dynamic value set in table layout - php

I have an html in variable. In this variable I have a SN_NO,CUST_REF_NO,NAME in td. I need to replace this variable with dynamic values from array. The array value like this
$result = array(
array(
'sn_no' => '1',
'cust_ref_no' => 'A123',
'name' => "AAA",
'id'=>'111'
),
array(
'sn_no' => '2',
'cust_ref_no' => 'B123',
'name' => "BBB",
'id'=>'222'
),
array(
'sn_no' => '3',
'cust_ref_no' => 'C123',
'name' => "CCC",
'id'=>'333'
),
);
$tr_html ='<tr>
<td class="text-left" id="abc" width="3%">SN_NO</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">CUST_REF_NO</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">NAME</td>
</tr> ';
I need a final result should be like this
$tr_html ='<tr>
<td class="text-left" id="abc" width="3%">1</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">A123</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">AAA</td>
</tr>
<tr>
<td class="text-left" id="abc" width="3%">2</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">B123</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">BBB</td>
</tr>
<tr>
<td class="text-left" id="abc" width="3%">3</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">C123</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">CCC</td>
</tr>
';
I tried the below code
foreach ($result as $key => $val){
$test = str_replace('SN_NO', $val['sn_no'], $tr_html);
$test .= str_replace('CUST_REF_NO', $val['cust_ref_no'], $tr_html);
$test .= str_replace('NAME', $val['name'], $tr_html);
}
echo $test;
Please help me on this. Thanks in advance

<?php
$result = array(
array(
'sn_no' => '1',
'cust_ref_no' => 'A123',
'name' => "AAA",
'id'=>'111'
),
array(
'sn_no' => '2',
'cust_ref_no' => 'B123',
'name' => "BBB",
'id'=>'222'
),
array(
'sn_no' => '3',
'cust_ref_no' => 'C123',
'name' => "CCC",
'id'=>'333'
),
);
$tr_html ='<tr>
<td class="text-left" id="abc" width="3%">SN_NO</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">CUST_REF_NO</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">NAME</td>
</tr> ';
$finalResult = '';
foreach ($result as $row) {
$tmp = $tr_html;
foreach ($row as $key => $value) {
$tmp = str_replace(strtoupper($key), $value, $tmp);
}
$finalResult .= $tmp;
}
echo $finalResult;

You can use arrays in str_replace() to replace multiple values at once.
$test = '';
foreach ($result as $key => $val){
$test .= str_replace(
array('SN_NO', 'CUST_REF_NO', 'NAME'),
array($val['sn_no'], $val['cust_ref_no'], $val['name']),
$tr_html);
}
echo $test;

Related

Array in Codeigniter

I had simple key in the totalwithouttax with 3000, totaltax with 10 and totalamtincltax with 2000 in my interface.
And I had echo the array, the totaltax and totalamtincltax echo the right value that I key in, But totalwithouttax echo 1000 only and data inserted in database is 1.000000.
Please help me. Thank You.
Controller:
$custTd = $post['id'];
$custMame = $post['name'];
$custEmail = $post['email'];
$custAddr = $post['address'];;
$custTel = $post['telno'];
$remarks = $post['remark'];
$paymentOption = $post['options'];
$promoCode = $post['promo_code'];
$arraySize = count($post['totalwithouttax']);
for($i=0; $i<$arraySize; $i++){
$totalWithoutTax = $post['totalwithouttax'][$i]; //$post['totalwithouttax'];
$totalTax = $post['totaltax'][$i]; //$post['totaltax'];
$totalAmtInclTax = $post['totalamtincltax'][$i];//$post['totalamtincltax'];
//MASSAGE DATA TO BE INSERTED TO ARRAY
$orderHeader = array(
'user_id' => $custTd,
'status' => 1,
'cust_name' => $custMame,
'cust_email' => $custEmail,
'cust_addr' => $custAddr,
'cust_tel' => $custTel,
'remarks' => $remarks,
'pay_option' => $paymentOption,
'total_without_tax' => $totalWithoutTax,
'total_tax' => $totalTax, 'total_amt_incl_tax' => $totalAmtInclTax,
'epp_first_payment' => $eppFirstPayment,
);
//INSERT ORDER HEADER
$orderHeaderId = $this->order->InsertOrderHeader($orderHeader);
view:
<tr class="item-details">
<td><span class="rowNumber">1</span></td>
<td class="">
<?php
$options = array(
'' => '~Choose An Item~'
);
foreach ($item as $rows){
$options[$rows->id] = $rows->item_name;
}
$select = array(
'id' => 'item_id',
'class' => 'form-control'
);
echo form_dropdown('item_id[]', $options,set_value('item_name'),$select);
?>
</td>
<td class=""><input type="number" class="item-qty" name="qty[]" min="0.00" /></td>
<td><input type="number" name="weight[]" class="weight" step="any" /></td>
<td><input type="number" name="transportation[]" class="transporation" step="any" /></td>
<td><input type="text" id="gp[]" name="gp[]" value="" /></td>
<td><input type="text" id="discount[]" name="discount[]" value=""/></td>
<td><input type="text" id="unit_price[]" name="unit_price[]" value="" /></td>
<td align="right">
<input type="text" id="totalwithouttax" name="totalwithouttax[]" value="">
</td>
<td align="right">
<input type="text" id="totaltax" name="totaltax[]" value="">
</td>
<td align="right">
<input type="text" id="totalamtincltax" name="totalamtincltax[]" value="">
</td>
</tr><br/>
You are wrong way calling post method
$custTd = $_POST['id'];
$custMame = $_POST['name'];
$custEmail = $_POST['email'];
$custAddr = $_POST['address'];;
$custTel = $_POST['telno'];
$remarks = $_POST['remark'];
$paymentOption = $_POST['options'];
$promoCode = $_POST['promo_code'];
$arraySize = count($post['totalwithouttax']);
for($i=0; $i<=$arraySize; $i++){
$totalWithoutTax = $_POST['totalwithouttax'][$i];
$totalTax = $_POST['totaltax'][$i]; //$post['totaltax'];
$totalAmtInclTax = $_POST['totalamtincltax'][$i];
//MASSAGE DATA TO BE INSERTED TO ARRAY
$orderHeader = array(
'user_id' => $custTd,
'status' => 1,
'cust_name' => $custMame,
'cust_email' => $custEmail,
'cust_addr' => $custAddr,
'cust_tel' => $custTel,
'remarks' => $remarks,
'pay_option' => $paymentOption,
'total_without_tax' => $totalWithoutTax,
'total_tax' => $totalTax,
'total_amt_incl_tax' => $totalAmtInclTax,
'epp_first_payment' => $eppFirstPayment,
);
}

Insert Multiple Rows into Database in Codeigniter

I got 3 constant rows with few columns like Item,Quantity,Weight...... in a row, in my Interface.
When I selected the data in the rows and submit form, only the 3rd row data will be inserted into the database.
I want each rows that contain data insert into the database. Any solution for this? Thank You.
Updated::
I had tried to use the PHP array and below is the updated code.But I had stucked at the foreach part, I want to insert the data into the database.
My View:
<tr class="item-details">
<td><span class="rowNumber">1</span></td>
<td class="">
<?php
$options = array(
'' => '~Choose An Item~'
);
foreach ($item as $rows){
$options[$rows->id] = $rows->item_name;
}
$select = array(
'name' => 'item_name[]',
'id' => 'item_name',
'class' => 'form-control'
);
echo form_dropdown('item', $options,set_value('item_name'),$select);
?>
</td>
<td class=""><input type="number" class="item-qty" name="qty[]"/></td>
<td><input type="number" name="weight[]" class="weight" /></td>
<td><input type="text" name="promo_code[]" value=""/></td>
<td><input type="text" name="gp[]" value=""/></td>
<td><input type="text" name="discount[]" value=""/></td>
<td><input type="text" name="unit_price[]" value=""/></td>
<td align="right" class="totalwithouttax">0.00</td>
<td align="right" class="totaltax">0.00</td>
<td align="right" class="totalamtincltax">0.00</td>
</tr><br/>
<tr class="item-details">
<td><span class="rowNumber">2</span></td>
<td class="">
<?php
$options = array(
'' => '~Choose An Item~'
);
foreach ($item as $rows){
$options[$rows->id] = $rows->item_name;
}
$select = array(
'name' => 'item_name[]',
'id' => 'item_name',
'class' => 'form-control'
);
echo form_dropdown('item', $options,set_value('item_name'),$select);
?>
</td>
<td class=""><input type="number" class="item-qty" name="qty[]"/></td>
<td><input type="number" name="weight[]" class="weight" /></td>
<td><input type="text" name="promo_code[]" value=""/></td>
<td><input type="text" name="gp[]" value=""/></td>
<td><input type="text" name="discount[]" value=""/></td>
<td><input type="text" name="unit_price[]" value=""/></td>
<td align="right" class="totalwithouttax">0.00</td>
<td align="right" class="totaltax">0.00</td>
<td align="right" class="totalamtincltax">0.00</td>
</tr><br/>
<tr class="item-details">
<td><span class="rowNumber">3</span></td>
<td class="">
<?php
$options = array(
'' => '~Choose An Item~'
);
foreach ($item as $rows){
$options[$rows->id] = $rows->item_name;
}
$select = array(
'name' => 'item_name[]',
'id' => 'item_name',
'class' => 'form-control'
);
echo form_dropdown('item', $options,set_value('item_name'),$select);
?>
</td>
<td class=""><input type="number" class="item-qty" name="qty[]"/></td>
<td><input type="number" name="weight[]" class="weight" /></td>
<td><input type="text" name="promo_code[]" value=""/></td>
<td><input type="text" name="gp[]" value=""/></td>
<td><input type="text" name="discount[]" value=""/></td>
<td><input type="text" name="unit_price[]" value=""/></td>
<td align="right" class="totalwithouttax">0.00</td>
<td align="right" class="totaltax">0.00</td>
<td align="right" class="totalamtincltax">0.00</td>
</tr><br/>
Controller:
//***************** ORDER ITEM ********************
$orderID = 1;
$productId = $this->input->post('product_id');
$itemName = $_POST['item_name'];//$this->input->post('item_name');
$qty = $this-> $_POST['qty'];//input->post('qty');
$weight = $this-> $_POST['weight'];//input->post('weight');
$unitPrice = $_POST['unit_price'];//$this->input->post('unit_price');
$transportationPrice = $this->input->post('transportation_price');
$itemType = $this->input->post('item_type');
$gp = $this-> $_POST['gp'];//input->post('gp');
$tax = $this->input->post('tax');
$amtInclTax = $this->input->post('amt_incl_tax');
$amtWithoutTax = $this->input->post('amt_without_tax');
$taxCode = $this->input->post('tax_code');
$orderItems = array (
'order_id' => $orderHeaderId ,
'product_id' => 7 ,
'item' => $itemName ,
'qty' => $qty ,
'weight' => $weight ,
'unit_price' => $unitPrice ,
'transportation_price' => 0 ,
'item_type' => 'GOLD' ,
'gp' => $gp,
'discount' => $discount,
'amt_without_tax' => 10,
'tax' => 20 ,
'amt_incl_tax' => 30,
'tax_code' => 40 ,
);
echo '<pre>'; print_r($orderItems); echo '</pre>';
foreach($itemName as $key => $iN){
//stuck at here
}
$orderHeaderId = $this->order->InsertItemData($orderHeaderId,$orderHeader,$orderItems);
Model:
public function InsertItemData($orderHeaderId,$orderHeader,$orderItems){
//take order_id from 'order' table //from InsertData(model) function
$orderHeaderId = $this->InsertData($orderHeader);
// Inserting Order Item
if($orderItems){
$this->db->insert_batch('order_items', $orderItems);
}
}
First make your all 3 portion of HTML form input name array like
<input type="number" class="item-qty" name="qty[]"/>
<input type="number" name="weight" class="weight[]" />
...
Then loop your data array as
$qty = $this->input->post('qty', TRUE);
$weight = $this->input->post('weight', TRUE);
$orderItems = array();
for ($i = 0; $i < count($qty); $i++) {
if ($qty[$i]) {
$orderItems[] = array (
'order_id' => $orderHeaderId ,
'product_id' => 7 ,
'item' => 'TEST' ,
'qty' => $qty[$i] ,
'weight' => $weight[$i]
...
);
}
}
And now you may use insert_batch method as
if ($orderItems) {
$this->db->insert_batch('table_name', $orderItems);
}
You could modify each of the codes like below.
Controller :
$orderID = 1;
$productId = $this->input->post('product_id');
$itemName = $_POST['item_name'];//$this->input->post('item_name');
$qty = $this-> $_POST['qty'];//input->post('qty');
$weight = $this-> $_POST['weight'];//input->post('weight');
$unitPrice = $_POST['unit_price'];//$this->input->post('unit_price');
$transportationPrice = $this->input->post('transportation_price');
$itemType = $this->input->post('item_type');
$gp = $this-> $_POST['gp'];//input->post('gp');
$tax = $this->input->post('tax');
$amtInclTax = $this->input->post('amt_incl_tax');
$amtWithoutTax = $this->input->post('amt_without_tax');
$taxCode = $this->input->post('tax_code');
$orderItems = array();
for ($i = 0; $i < count($qty); $i++) {
if ($qty[$i]) {
$orderItems[] = array (
// 'order_id' => $orderHeaderId ,
'product_id' => 7 ,
'item' => $itemName[$i] ,
'qty' => $qty[$i] ,
'weight' => $weight[$i] ,
'unit_price' => $unitPrice[$i] ,
'transportation_price' => 0 ,
'item_type' => 'GOLD' ,
'gp' => $gp[$i],
'discount' => $discount[$i],
'amt_without_tax' => 10,
'tax' => 20 ,
'amt_incl_tax' => 30,
'tax_code' => 40 ,
);
}
}
$this->order->InsertItemData($orderHeader,$orderItems);
Model :
public function InsertItemData($orderHeader,$orderItems) {
//take order_id from 'order' table //from InsertData(model) function
$orderHeaderId = $this->InsertData($orderHeader);
if($orderItems) {
// append order_id array into each order item
foreach ($orderItems as $key => $item) {
$orderItems[$key] = array('order_id' => $orderHeaderId) + $orderItems[$key];
}
// Inserting Order Item
$this->db->insert_batch('order_items', $orderItems);
}
}

Sorting all values of an array in a HTML table

I have an array that includes some other arrays. All values which are inside that array, should be placed in a HTML table. I get all the values but my HTML table looks horrible!
I have a code that looks like this:
<?php
$data = array(
'name' => array('Tom', 'Robert', 'Julia'),
'age' => array(32, 45, 21),
'city' => array('New York', 'Toronto', 'Los Angeles')
);
?>
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>City</td>
</tr>
<tr>
<?php
foreach($data as $values) {
foreach($values as $value) {
echo '<td>' . $value . '</td>';
}
}
?>
</tr>
</table>
The output of this code will be:
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>City</td>
</tr>
<tr>
<td>Tom</td>
<td>Robert</td>
<td>Julia</td>
<td>32</td>
<td>45</td>
<td>21</td>
<td>New York</td>
<td>Toronto</td>
<td>Los Angeles</td>
</tr>
</table>
This is exactly the output that I need:
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>City</td>
</tr>
<tr>
<td>Tom</td>
<td>32</td>
<td>New York</td>
</tr>
<tr>
<td>Robert</td>
<td>45</td>
<td>Toronto</td>
</tr>
<tr>
<td>Julia</td>
<td>21</td>
<td>Los Angeles</td>
</tr>
</table>
How can I do this?
Short solution with array_map function:
<?php
$data = array(
'name' => array('Tom', 'Robert', 'Julia'),
'age' => array(32, 45, 21),
'city' => array('New York', 'Toronto', 'Los Angeles')
);
$items = array_map(null, $data['name'], $data['age'], $data['city']);
?>
<table border='1'>
<tr><th>Name</th><th>Age</th><th>City</th></tr>
<?php
foreach ($items as $v) {
echo '<tr><td>' . implode('</td><td>', $v) . '</td></tr>';
}
?>
</table>
The output (push Run code snippet button):
<table border="1">
<tbody><tr><th>Name</th><th>Age</th><th>City</th></tr>
<tr><td>Tom</td><td>32</td><td>New York</td></tr><tr><td>Robert</td><td>45</td><td>Toronto</td></tr><tr><td>Julia</td><td>21</td><td>Los Angeles</td></tr></tbody></table>
Consider reformatting of the $data array to keep related values close together. It will make your foreach then easier.
$data = array(
array('name' => 'Tom', 'age' => 32, 'city' => 'New York'),
array('name' => 'Robert', 'age' => 45, 'city' => 'Toronto'),
//...
);
This way you get the one table data row within one iteration of foreach.

How to looping data from multidimension array in codeigniter

i found something problem with looping data in multidimension array. i got error. undefined $t in my view.
this my controller :
$dp[] = array(
'id' => $ft->idpr_order,
'nor_order_pro' => $ft->no_order_pro,
'product_name' => $ft->nama_produk,
'sku' => $ft->artikel,
'brand' => $ft->merk,
'discount' => $discft,
'size' => $ukr,
'color' => $wrn,
'qty' => $ft->qty,
'price_before' => $hg_before,
'price_fix' => $ft->harga_fix,
);
}
$exp = explode('|', $resex);
$data_cs = array(
'inv' => $invoice,
'tglOrdercs' => date('d F Y H:i:s'),
'tglExp' => $date_maju,
'nmlkp' => $nmlkpi,
'almtkp' => $address,
'note' => $note_ol,
'kota' => $city,
'prov' => $prov,
'layanan' => $exp[0],
'etd' => $exp[1],
'tarif' => $exp[2],
'noTelp' => $notelp271,
'methode' => $method,
'bnk_option' => $banking_select,
'cabang' => $banking_inf_cab,
'no_rek' => $banking_inf_no,
'an_bnk' => $banking_inf_an,
'subtotal' => $subt,
'kode_pembayaran' => $code_unik,
'total_belanja' => $tot_bel,
'berat_total' => $tot_ber,
'data_order' => $dp
);
$body = $this->load->view('em_info_notification_group/mail_order_for_admin',$data_cs,TRUE);
and this my view
<?php
foreach ($data_order as $data):
if (is_array($data)):
foreach ($data as $value):
if (is_array($value)):
foreach ($value as $t):
?>
<tr>
<td class="yut" align="left" style="border-bottom: 1px solid #ccc;">
<h4 style="margin-top:10px;margin-bottom: 10px;"><b><?php echo $t['product_name'];?></b></h4>
<ul class="list-unstyled" style="font-size: 12px; padding-left: 15px;margin-top: 0;">
<li class="gf">SKU : <?php echo $t['sku'];?></li>
<?php echo $t['color'];?>
<?php echo $t['size'];?>
</ul>
</td>
<td class="yut" style="font-size: 14px;border-bottom: 1px solid #ccc;" align="center">
<?php echo $t['qty'];?> pcs
</td>
<td class="yut" style="border-bottom: 1px solid #ccc;" align="right">
<span style="font-size: 12px;"><?php echo $t['price_before'];?><br><harga>Rp. <?php echo $t['price_fix'];?></harga><br><?php echo $t['discount'];?></span>
</td>
</tr>
<?php
endforeach;
endif;
endforeach;
endif;
endforeach;
?>
in my view i got error. undefined $t in my view. actually the data I want to parse it to the admin template html. I have done various ways but nothing works
Change your looping structure to:
foreach ($data_order as $data):
if (is_array($data)):
foreach ($data as $t):
Regards,

best data structer for such a cross html table build

There are some groups which holds some data for example empty cars, reservated cars and cars which will return.. So if user want to see a cross table between car groups and dates,
I want to create a cross html table like this:
<table class="table table-striped table-bordered table-hover">
<tr>
<th colspan="2"></th>
<th>Date1</th>
<th>Date2</th>
<th>Date3</th>
</tr>
<!--Group1-->
<tr>
<td rowspan="4">Group1</td>
<td>Empty</td>
<td>5</td>
<td>12</td>
<td>3</td>
</tr>
<tr>
<td>On the Res</td>
<td>8</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Will Return</td>
<td>8</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Remains</td>
<td>-11</td>
<td>0</td>
<td>-9</td>
</tr>
<!--Group1 Ends -->
<!--Group1-->
<tr>
<td rowspan="4">Group2</td>
<td>Empty</td>
<td>5</td>
<td>12</td>
<td>3</td>
</tr>
<tr>
<td>On the Res</td>
<td>8</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Will Return</td>
<td>8</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Remains</td>
<td>-11</td>
<td>0</td>
<td>-9</td>
</tr>
<!--Group1 Ends -->
</table>
And here is the output of this html code:
What is the best php array structure for this kind cross table?
Thanks.
This is bound to get a variety of opinions. For what it's worth, this is one possible structure that would make sense and would be fairly straightforward to code against.
The "groups" could be the top-level keys, and the value for each group would be an array of arrays:
$table_data = [
'Group1' => [
[ 'Title' => 'Empty',
'Date1' => 5,
'Date2' => 12,
'Date3' => 3
],
[ 'Title' => 'On the Res',
'Date1' => 8,
'Date2' => 6,
'Date3' => 6
],
// etc...
],
'Group2' => [
[ 'Title' => 'Empty',
'Date1' => 5,
'Date2' => 12,
'Date3' => 3
],
[ 'Title' => 'On the Res',
'Date1' => 8,
'Date2' => 6,
'Date3' => 6
],
// etc...
],
// etc...
];
My thoughts on the PHP loop would be something like so:
foreach( $table_data AS $group => $rows ) {
$rowspan = count( $rows );
echo '<tr><td rowspan="' . $rowspan . '">' . $group . '</td>';
$tr = '';
foreach( $rows AS $row ) {
echo $tr . '<td>' . implode( '</td><td>', array_values( $row ) ) . '</td></tr>';
$tr = '<tr>';
}
}

Categories