Insert Multiple Rows into Database in Codeigniter - php

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);
}
}

Related

Dynamic value set in table layout

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;

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,
);
}

Mysql : Table multiple rows Insert into one sql row

Currently I'm creating a software, where i can create multiple row in a table.
But the why I wrote PHP code, it create multiple rows into SQL but i need to create one row against one invoice number.
Html (auto generate using ajax):
<tr>
<td><input class="form-control" type="text" name="product_name[]" value="'.$proData['name'].'"></td>
<td><input class="form-control qnt'.$proData['id'].'" min="0" step="any" type="number" name="qnt[]" placeholder="Quantity"></td>
<td><input class="form-control unit_price'.$proData['id'].'" type="number" min="0" step="any" name="unit_price[]" placeholder="Unit Price"></td>
<td><input class="form-control pack_size'.$proData['id'].'" type="number" min="0" step="any" name="pack_size[]" placeholder="Pack Size"></td>
<td><input class="form-control unit_pack'.$proData['id'].'" type="number" min="0" step="any" name="unit_pack[]" placeholder="Unit Pack (jar/Drum)"></td>
<td><input class="form-control total_kg'.$proData['id'].'" type="number" name="total_kg[]" placeholder="Total Kg/s" value="" readonly></td>
<td><input class="form-control total_price'.$proData['id'].'" type="number" name="total_price[]" placeholder="Total Price ৳" value="" readonly></td>
<td><button type="button" class="rowDelete btn btn-danger"><i class="icon-trash"></i></button></td>
</tr>
php Code :
public function add(){
// Form Inputs
$invoice = $this->input->post('invoice');
$date = $this->input->post('date');
$created_by = $this->input->post('created_by');
$products = $this->input->post('products');
$qnt = $this->input->post('qnt[]');
$unit_price = $this->input->post('unit_price[]');
$pack_size = $this->input->post('pack_size[]');
$unit_pack = $this->input->post('unit_pack[]');
$total_kg = $this->input->post('total_kg[]');
$total_price = $this->input->post('total_price[]');
$payed = $this->input->post('payed');
$price_less = $this->input->post('price_less');
$price_discount = $this->input->post('price_discount');
$price_due = $this->input->post('price_due');
$grand_total = $this->input->post('grand_total');
$payMethod = $this->input->post('payMethod');
$bank_name = $this->input->post('bank_name');
$cheque_no = $this->input->post('cheque_no');
$bank_acc_no = $this->input->post('bank_acc_no');
for($i = 0; $i < count($qnt); $i++){
$purchaseData = array(
'invoice_no' => $invoice,
'date' => $date,
'product_id' => $products,
'create_date' => $created_by,
'qnt' => $qnt[$i],
'unit_price' => $unit_price,
'pack_size' => $pack_size,
'unit_pack' => $unit_pack,
'total_kg' => $total_kg,
'total_price' => $total_price,
'payed' => $payed,
'price_less' => $price_less,
'price_discount' => $price_discount,
'price_due' => $price_due,
'grand_total' => $payMethod,
'payMethod' => $grand_total,
'bank_name' => $bank_name,
'cheque_no' => $cheque_no,
'bank_acc_no' => $bank_acc_no
);
$PurchaseQuery = $this->db->insert('purchase', $purchaseData);
if($PurchaseQuery){
$purchaseAdded = "Product Purchase Add.";
$this->session->set_flashdata('purchaseAdded', $purchaseAdded);
redirect('Purchase');
}
}
}
Please Help. I'm Using CI.
the requirement i quit different . what you can do is ? instead of looping through items . i mean
count($qnt)
just json_encode() the multiple items
or change your pattern and normalize you tables
to put order and items of orders in 2 different table
Please do this.
public function add(){
// Form Inputs
$invoice = $this->input->post('invoice');
$date = $this->input->post('date');
$created_by = $this->input->post('created_by');
$products = $this->input->post('products');
$qnt = $this->input->post('qnt[]');
$unit_price = $this->input->post('unit_price[]');
$pack_size = $this->input->post('pack_size[]');
$unit_pack = $this->input->post('unit_pack[]');
$total_kg = $this->input->post('total_kg[]');
$total_price = $this->input->post('total_price[]');
$payed = $this->input->post('payed');
$price_less = $this->input->post('price_less');
$price_discount = $this->input->post('price_discount');
$price_due = $this->input->post('price_due');
$grand_total = $this->input->post('grand_total');
$payMethod = $this->input->post('payMethod');
$bank_name = $this->input->post('bank_name');
$cheque_no = $this->input->post('cheque_no');
$bank_acc_no = $this->input->post('bank_acc_no');
$purchaseData = array(
'invoice_no' => $invoice,
'date' => $date,
'product_id' => $products,
'create_date' => $created_by,
'qnt' => $qnt[$i],
'unit_price' => $unit_price,
'pack_size' => $pack_size,
'unit_pack' => $unit_pack,
'total_kg' => $total_kg,
'total_price' => $total_price,
'payed' => $payed,
'price_less' => $price_less,
'price_discount' => $price_discount,
'price_due' => $price_due,
'grand_total' => $payMethod,
'payMethod' => $grand_total,
'bank_name' => $bank_name,
'cheque_no' => $cheque_no,
'bank_acc_no' => $bank_acc_no
);
$PurchaseQuery = $this->db->insert('purchase', $purchaseData);
if($PurchaseQuery){
$purchaseAdded = "Product Purchase Add.";
$this->session->set_flashdata('purchaseAdded', $purchaseAdded);
redirect('Purchase');
}
}
and let me know the result.

Associative Multidimensional Array

I want to be able to use a variable (which is a value that is imputed in a textbox) in an
Associative Multidimensional Array and print out the rest of the array values associated with it.
here is what i have so far that doesnt work
<html>
<body>
<form method="post" action="newcal.php">
<table>
<tr>
<td> Item #: </td>
<td> <input type=text name= txtitem > <br> </td>
<td>
</td>
<td> <fieldset style = "width:60px">
<input type=submit value = "Get Data" name= getdata>
<input type=submit value = "Add to Cart" name= addto>
<input type=submit value = "Get Total" name= gettotal>
</fieldset>
</td>
</tr>
</table>
</form>
<?php
$item = isset($_POST['txtitem']);
$stuff = array(
array("id" => 1,"name" => "Apples","price" => 50 ),
array("id" => 2,"name" => "Pineapples","price" => 125 ),
array("id" => 3,"name" => "Mango","price" => 35 ),
array("id" => 4,"name" => "Banana","price" => 25 ),
array("id" => 5,"name" => "Naseberry","price" => 38 ));
if(isset($_POST['getdata']))
{
foreach ($stuff as $row)
{
if ($row['id'] == $item)
{
$name = $row['name'];
$price = $row['price'];
}
}
echo $name;
echo $price;
}
?>
</body>
</html>
I think your error is with the following line:
$item = isset($_POST['txtitem']);
$item is being set to a boolean value. You probably want something like this:
$item = (isset($_POST['txtitem']) ? intval($_POST['txtitem']) : null);

Generate a custom HTML form using array values with PHP

Problem:
I am trying to generate a custom HTML form with help of values in an array using PHP.
The PHP code ($row['Key'] will contain W, B and R):
$numbers[$row['Key']] = array (
'C' =>
array (
'BO' => $row['BO'],
'BT' => $row['BT']),
'D' =>
array (
'MF' => $row['MF'],
'MT' => $row['MT'])
);
Array produced with PHP:
Array
(
[W] => Array
(
[C] => Array
(
[BO] => 36
[BT] => 63
)
[D] => Array
(
[MF] => 54
[MT] => 63
)
)
[B] => Array
(
[C] => Array
(
[BO] => 60
[BT] => 105
)
[D] => Array
(
[MF] => 90
[MT] => 105
)
)
[R] => Array
(
[C] => Array
(
[BO] => 12
[BT] => 21
)
[D] => Array
(
[MF] => 18
[MT] => 24
)
)
)
The outcome should look like the following. Notice the combination of W/B/R and BO/BT/MF/MT.
<table>
<tbody>
<tr>
<td>W</td>
<td><input type="text" name="WBO" id="WBO" value="36"></td>
<td><input type="text" name="WBT" id="WBT" value="63"></td>
<td><input type="text" name="WMF" id="WMF" value="54"></td>
<td><input type="text" name="WMT" id="WMT" value="63"></td>
</tr>
<tr>
<td>B</td>
<td><input type="text" name="BBO" id="BBO" value="60"></td>
<td><input type="text" name="BBT" id="BBT" value="105"></td>
<td><input type="text" name="BMF" id="BMF" value="90"></td>
<td><input type="text" name="BMT" id="BMT" value="105"></td>
</tr>
<tr>
<td>R</td>
<td><input type="text" name="RBO" id="RBO" value="12"></td>
<td><input type="text" name="RBT" id="RBT" value="21"></td>
<td><input type="text" name="RMF" id="RMF" value="18"></td>
<td><input type="text" name="RMT" id="RMT" value="24"></td>
</tr>
</tbody>
</table>
http://php.net/manual/en/control-structures.foreach.php
without using 3 foreach loops
$var = print_r($numbers, true);
$table = preg_replace_callback(
"{.*?[^ ][ ]{4}\[(\S+)\].*?[^ ][ ]{8}\)}s",
"func",
$var);
$table= preg_replace('{\n[^<].*}','',$table);
echo "<table>{$table}</table>";
function: (count space to find array level)
function func($matches)
{
$table='';
$k=$matches[1];
$t=$matches[0];
$tr = preg_replace('{.*?[^ ][ ]{20}\[(\S+)\].*?=>.*?(\S+)}s',PHP_EOL."<td><input type='text' id='{$k}$1' name='{$k}$1' value='$2'></td>",$t);
$table .= "<tr>".PHP_EOL;
$table .= "<td>{$k}<td>{$tr}".PHP_EOL;
$table .= "</tr>".PHP_EOL;
return $table;
}
echo
<table><tr>
<td>W<td>
<td><input type='text' id='WBO' name='WBO' value='36'></td>
<td><input type='text' id='WBT' name='WBT' value='63'></td>
<td><input type='text' id='WMF' name='WMF' value='54'></td>
<td><input type='text' id='WMT' name='WMT' value='63'></td>
</tr>
<tr>
<td>B<td>
<td><input type='text' id='BBO' name='BBO' value='60'></td>
<td><input type='text' id='BBT' name='BBT' value='105'></td>
<td><input type='text' id='BMF' name='BMF' value='90'></td>
<td><input type='text' id='BMT' name='BMT' value='105'></td>
</tr>
</table>
Should be fairly easy to step through this array with foreach().
[ghoti#pc ~]$ cat doit.php
#!/usr/local/bin/php
<?php
printf("<table>\n <tbody>\n");
$numbers=array(
'W' => array(
'C' => array( 'BO' => 36, 'BT' => 63),
'D' => array( 'MF' => 54, 'MT' => 63),
),
'B' => array(
'C' => array( 'BO' => 60, 'BT' => 105),
'D' => array( 'MF' => 90, 'MT' => 105),
),
);
$fmt1 = "\t<tr>\n"
. "\t\t<td>%s</td>\n"
. "%s"
. "\t</tr>\n";
$fmt2 = "\t\t<td><input type='text' name='%s%s' id='%s%s' value='%s'></td>\n";
foreach ($numbers as $index1 => $line1) {
foreach ($line1 as $index2 => $line2) {
foreach ($line2 as $index3 => $value) {
$output .= sprintf($fmt2, $index1, $index3, $index1, $index3, $value);
}
}
printf($fmt1, $index1, $output);
$output = "";
}
printf(" </tbody>\n</table>\n");
And the output:
[ghoti#pc ~]$ ./doit.php
<table>
<tbody>
<tr>
<td>W</td>
<td><input type='text' name='WBO' id='WBO' value='36'></td>
<td><input type='text' name='WBT' id='WBT' value='63'></td>
<td><input type='text' name='WMF' id='WMF' value='54'></td>
<td><input type='text' name='WMT' id='WMT' value='63'></td>
</tr>
<tr>
<td>B</td>
<td><input type='text' name='BBO' id='BBO' value='60'></td>
<td><input type='text' name='BBT' id='BBT' value='105'></td>
<td><input type='text' name='BMF' id='BMF' value='90'></td>
<td><input type='text' name='BMT' id='BMT' value='105'></td>
</tr>
</tbody>
</table>
[ghoti#pc ~]$
this?
echo "<table><tbody>";
foreach((array)$numbers as $key=>$val) {
echo "<tr><td>".$key."</td>";
foreach((array)$val as $key2=>$val2) {
foreach((array)$val2 as $key3=>$val3) {
echo '<td><input type="text" name="'.$key.$key3.'" id="'.$key.$key3.'" value="'.$val3.'"></td>';
}
}
echo "</tr>";
}
echo "</tbody></table>";
Check this code dude.
<table>
<tbody>
<?php
foreach($numbers as $key=>$sarray){
echo "<tr>";
echo "<td>$key</td>";
foreach($sarray as $key1=>$sarray1){
foreach($sarray1 as $fname=>$fvalue){
echo '<td><input type="text" name="'.$fname.'" id="'.$fname.'" value="'.$fvalue.'"></td>';
}
}
echo "</tr>";
}
?>
</tbody>
</table>

Categories