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,
Related
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.
I have a problem in inserting array in the database in Codeigniter, I tried following way but it gives an error "Message: Illegal string offset 'year'", "Message: Illegal string offset 'month'" and so on..., I really confused about how to solve this, please help.
this is the form:
<form action="<?php echo base_url();?>hr/Home/store_attendance" method="post"
id="student_attendance_entry">
<input type="hidden" name="year" value="<?php echo $year; ?>" />
<input type="hidden" name="month" value="<?php echo $month; ?>" />
<?php foreach($staffs as $staff): ?>
<table class="table table-striped table-bordered table-hover attendance_entry"
style="border:initial;border: 1px solid #ddd;" id="sample_editable_2">
<thead>
<tr>
<th style="width: 50px !important;">شماره</th>
<th align="center" style="width:auto%">آی دی</th>
<th align="center" style="width:15%">نام</th>
<th align="center" style="width:15%;"> ولد</th>
<th align="center" style="width:auto">حاضر</th>
<th align="center" style="width:auto">غیر حاضر</th>
<th align="center" style="width:auto">توضیحات</th>
</tr>
</thead>
<tbody>
<?php $a=1; foreach($staffs as $st):?>
<tr>
<td align="center" style="font-size: 11px"><?php echo $a;?></td>
<td align="center" style="font-size: 11px"><?php echo $st['s_id'];?></td>
<td align="center"><?php echo $st['dari_name'];?></td>
<td align="center"><?php echo $st['dari_fName'];?></td>
<td>
<input type="number" min="0" class="form-control" name="total_present_day[]"
value="<?php if($st['total_present']==null){echo '';}else{ echo $st['total_present'];}?>"
data="<?php echo $a;?>">
<input type="hidden" name="salary_type[]" id="salary_type" value="<?php echo $st['salary_type']?>">
</td>
<td>
<input type="number" min="0" class="form-control" name="total_absent_day[]"
value="<?php if($st['absent']==null){echo '';}else{ echo $st['absent'];}?>"
data="<?php echo $a;?>">
<input type="hidden" class="form-control" name="staff_id[]" value="<?php echo $st['s_id'];?>">
</td>
<td>
<textarea min="0" class="form-control" name="memo[]"
colspan='3' rowspan="1"
data="<?php echo $a;?>"><?php if($st['memo']==null){echo '';}else{ echo $st['memo'];}?></textarea>
</td>
</tr>
<?php $a++;endforeach;?>
</tbody>
</table>
<?php endforeach; ?>
<br>
<div class="form-actions right">
<a href="<?php echo base_url();?>student/school/attendance" class="btn default" data-dismiss="modal"><i
class="fa fa-close"></i> بستن</a>
<input type="submit" name="save" class="btn blue" value="ذخیره" />
</div>
this is the controller:
public function store_attendance()
{
$data=array(
'year' => $this->input->post('year'),
'month' => $this->input->post('month'),
'staff_id' => $this->input->post('staff_id'),
'total_present_day' => $this->input->post('total_present_day'),
'total_absent_day'=>$this->input->post('total_absent_day'),
'salary_type'=>$this->input->post('salary_type'),
'memo'=>$this->input->post('memo')
);
$this->dd($data);
// $this->dd($class);
$insert_att = $this->stuff_model->add_staff_attendance($data);
// var_dump($insert_att);
if($insert_att)
{
echo redirect(base_url().'hr/register_employee_attendance');
}
}
this is the model:
public function add_staff_attendance($data)
{
$this->db->trans_begin();
foreach ($data['total_present_day'] as $key => $value) {
{
$dataToSave = array(
'year' => $value['year'],
'month' => $value['month'],
'type_id'=>$value['salary_type'][$key],
'total_present' => $value['total_present_day'][$key],
'absent'=>$value['total_absent_day'][$key],
'memo'=>$value['memo'][$key],
'staf_id' => $value['staff_id'][$key]
);
$this->db->insert('staff_attendance', $dataToSave);
}
if ($this->db->trans_status() === false) {
$this->db->trans_rollback();
return false;
} else {
$this->db->trans_commit();
return true;
}
}
}
I called $this->dd($data) in controller and this is the output:
array (
'Total' => 7,
)
array (
'year' => '1400',
'month' => '2',
'staff_id' =>
array (
0 => '3',
),
'total_present_day' =>
array (
0 => '26',
),
'total_absent_day' =>
array (
0 => '0',
),
'salary_type' => '1',
'memo' =>
array (
0 => 'dds',
),
)
And this is the result of echo '<pre>'; print_r($data); echo '</pre>'; In model:
Array
(
[year] => 1400
[month] => 1
[staff_id] => Array
(
[0] => 3
[1] => 1
)
[total_present_day] => Array
(
[0] => 26
[1] => 20
)
[total_absent_day] => Array
(
[0] => 0
[1] => 6
)
[salary_type] => Array
(
[0] => 1
[1] => 1
)
[memo] => Array
(
[0] => asfd
[1] => saef
)
)
Array
(
[year] => 1400
[month] => 1
[staff_id] => Array
(
[0] => 3
[1] => 1
)
[total_present_day] => Array
(
[0] => 26
[1] => 20
)
[total_absent_day] => Array
(
[0] => 0
[1] => 6
)
[salary_type] => Array
(
[0] => 1
[1] => 1
)
[memo] => Array
(
[0] => asfd
[1] => saef
)
)
Your submitted data is structured in a logical, minimalistic, yet irregular fashion. By reconfiguring the name attributes of the form fields in your view, you can largely reduce the lines of processing code in the controller and the model.
Move the hidden fields inside your foeach() loop and declare dynamic indexes. Effectively the named fields would resemble this:
<?php foreach($staffs as $index => $staff) { ?>
<input type="hidden" name="attendance[<?php echo $index; ?>]['year']">
<input type="hidden" name="attendance[<?php echo $index; ?>]['month']">
<input type="number" name="attendance[<?php echo $index; ?>]['total_present']">
<input type="hidden" name="attendance[<?php echo $index; ?>]['type_id']">
<input type="number" name="attendance[<?php echo $index; ?>]['absent']">
<input type="hidden" name="attendance[<?php echo $index; ?>]['staf_id']">
<textarea name="attendance[<?php echo $index; ?>]['memo']"></textarea>
<?php } ?>
The controller needs a validation/sanitization process and some restucturing before blindly passing data to the model. I will not go into the validation/sanitization, but you should iterate the data, and deny it from being saved if ANY of the values are inappropriate.
Controller:
public function store_attendance(): void
{
// Definitely validate and sanitize the rows of data before passing to model.
// This shortcut is for demonstrating how to batch insert
if ($this->stuff_model->add_staff_attendance($this->input->post('attendance'))) {
redirect(base_url() . 'hr/home/register_employee_attendance');
} else {
// reload view and explain what went wrong
}
}
Model:
public function add_staff_attendance(array $data): bool
{
return $this->db->insert_batch('staff_attendance', $data);
}
None of this answer was tested.
To leave the hidden fields outside of the loop, you will need to assemble the rows' values into the correct structure before sending to the model for batch_insertion.
$presentDays = $this->input->post('total_present_day');
if ($presentDay) {
$rows = [];
foreach ($presentDays as $i => $presentDay) {
$rows[] = [
'year' => $this->input->post('year'),
'month' => $this->input->post('month'),
'staf_id' => $this->input->post('staff_id')[$i],
'total_present' => $this->input->post('total_present_day')[$i],
'absent' => $this->input->post('total_absent_day')[$i],
'type_id' => $this->input->post('salary_type')[$i],
'memo' => $this->input->post('memo')[$i]
];
}
if ($this->stuff_model->add_staff_attendance($rows)) {
redirect(base_url() . 'hr/home/register_employee_attendance');
} else {
// reload view and explain what went wrong
}
}
public function add_staff_attendance($data)
{
$this->db->trans_begin();
foreach ($data['total_present_day'] as $key => $value) {
{
$dataToSave = array(
'year' => $data['year'], // this seems to be same for all
'month' => $data['month'], // this seems to be same for all
'type_id' => $value['salary_type'][$key], // please change name='salary_type[]' in your form
'total_present' => $value['total_present_day'][$key], // seems to be an array
'absent' => $value['total_absent_day'][$key], // seems to be an array
'memo' => $value['memo'][$key], // seems to be an array
'staf_id' => $value['staff_id'][$key] // seems to be an array
);
$this->db->insert('staff_attendance', $dataToSave);
}
if ($this->db->trans_status() === false) {
$this->db->trans_rollback();
return false;
} else {
$this->db->trans_commit();
return true;
}
}
}
I solved my problem by changing some codes in the controller and model.
controller:
public function store_attendance()
{
$data = array();
$insert_att = 0;
$tst = $this->input->post('total_present_day');
for ($i=0; $i < count($tst); $i++) {
$data = array(
'year' => $this->input->post('year'),
'month' => $this->input->post('month'),
'staf_id' => $this->input->post('staff_id')[$i],
'total_present' => $this->input->post('total_present_day')[$i],
'absent'=>$this->input->post('total_absent_day')[$i],
'type_id'=>$this->input->post('salary_type')[$i],
'memo'=>$this->input->post('memo')[$i],
);
// var_dump($data);
$this->get_public_data->saveData('staff_attendance', $data);
}
echo redirect(base_url().'hr/home/rgisterAttendance');
}
Model:
public function add_staff_attendance($data)
{
$this->db->insert('staff_attendance', $data);
}
it seems that the error is in the controller, you must go through the array while it is saving.
public function store_attendance()
{
$data=array(
'year' => $this->input->post('year'),
'month' => $this->input->post('month'),
'staff_id' => $this->input->post('staff_id'),
'total_present_day' => $this->input->post('total_present_day'),
'total_absent_day'=>$this->input->post('total_absent_day'),
'salary_type'=>$this->input->post('salary_type'),
'memo'=>$this->input->post('memo')
);
foreach($data as $d):
$insert_att = $this->stuff_model->add_staff_attendance($d);
if($insert_att)
{
echo redirect(base_url().'hr/register_employee_attendance');
}
else
{
echo redirect(base_url().'hr/register_employee_attendance');
}
endforeach;
}
You should try something like it
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;
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>
i have an array like that, i want to print out it in view file, but it's empty, array is like that
array(
(int) 0 => array(
'ProductsUserNode' => array(
'product_user_node_id' => '155',
'user_node_id' => '53',
'product_id' => '1',
'is_active' => '1',
'expiry' => '0000-00-00',
'created' => '2013-01-10 10:27:22',
'modified' => '2013-01-10 10:27:22',
'created_view' => '10:27 AM, Jan 10,2013',
'modified_view' => '10:27 AM, Jan 10,2013'
),
'UserNode' => array(
'user_node_id' => '53',
'division_id' => '28',
'role_id' => '4',
'user_id' => '56',
'created' => '2013-01-10 10:27:20',
'created_view' => '10:27 AM, Jan 10,2013'
),
'Product' => array(
'product_id' => '1',
'name' => 'Manager',
)
),
i am using this code in view file
foreach ($products as $products)
{
?>
<tr>
<td> <?php $products['ProductsUserNode']['product_id']?> </td>
<td> <?php $products['Product']['name']?> </td>
</tr>
<?php }?>
i have also set the variable in controllerlike that,
$this->set('products',$products);
but it is not working, what's the problem? Thanks in advance
Use the echo keyword to print your variables.
foreach ($products as $product)
{
?>
<tr>
<td> <?php echo $product['ProductsUserNode']['product_id']?> </td>
<td> <?php echo $product['Product']['name']?> </td>
</tr>
<?php }?>
foreach ($products as $products) // wrong
You need to iterate over it properly (basic php!):
<?php foreach ($products as $product) { ?>
<tr>
<td> <?php echo h($product['ProductsUserNode']['product_id']); ?> </td>
<td> <?php echo h($product['Product']['name']); ?> </td>
</tr>
<?php } ?>
Also note the h() to secure the view.
PS: You should "bake" your code. This way you would learn from it how do to it properly.