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.
Related
i have an array i and i want to show the array values if the name of same array repeat in the another array and have true value
my arrays like this
$array1 = [
array(
'name' => internal_evidence
'price' => 30
'course_id' => 3
),
array(
'name' => international_evidence
'price' => 450
'course_id' => 3
),
array(
'name' => internal_evidence
'price' => 10
'course_id' => 1
),
array(
'name' => technical_evidence
'price' => 134
'course_id' => 3
),
];
$array2 = [
array(
'id' => 3
'name' => graphic
'price' => 150
'attr' => array(
'internal_evidence' => 'true',
'international_evidence' => 'false',
'technical_evidence' => 'true'
)
),
array(
'id' => 5
'name' => 3dmax
'price' => 300
'attr' => array(
)
),
array(
'id' => 1
'name' => ICDL
'price' => 480
'attr' => array(
'internal_evidence' => 'true',
)
),
];
i want to showing this all attr selected with true value in like this
also I want to sum price of array2 member and array1
<h2>graphic</h2>
<p>internal_evidence</p>
<p>technical_evidence</p>
<small>course price: 150</small>
<small>314</small> <!-- Price with selected evidence -->
<h2>3dmax</h2>
<small>course price: 300</small>
<!-- its not have attr evidence -->
<h2>ICDL</h2>
<p>internal_evidence</p>
<small>course price: 480</small>
<small>490</small> <!-- Price with selected evidence -->
i try this but its don`t work properly
$priceOfAttr = 0;
foreach($array2 as $key => $cat):
echo "<h2>{$cat['name']}</h2>";
foreach($array1 as $pr):
if($pr['course_id'] == $cat['id']):
foreach($cat['attr'] as $m => $optionV):
if($m == $pr['name'] && $optionV == "true"){
echo $m .'<br>';
$priceOfAttr += $pr['price'];
// echo "<small>{$cat['price']}</small><br>";
// echo $cat['price'] + $pr['price']. "<br>";
}
endforeach;
echo $priceOfAttr + $cat['price'] . '<br>';
endif;
endforeach;
echo '<br>';
endforeach;
I'd use a combination array_reduce and array_map to transform your data into what you need, then simply loop over that to display your view:
<?php
// Index your $array1 by [id][name]
$array1ByIdAndName = array_reduce($array1, static function ($byIdAndName, $entry) {
$byIdAndName[$entry['course_id']][$entry['name']] = $entry;
return $byIdAndName;
});
// Transform $array2's `attr` entries into attribute list + compute total price
$array2 = array_map(static function ($entry) use ($array1ByIdAndName) {
$entry['total_price'] = $entry['price'];
$entry['attr'] = array_reduce(array_keys($entry['attr']), static function ($attrs, $attrName) use ($array1ByIdAndName, &$entry) {
if ($entry['attr'][$attrName] === 'true') {
$attrs[] = $attrName;
$entry['total_price'] += $array1ByIdAndName[$entry['id']][$attrName]['price'];
}
return $attrs;
}, []);
return $entry;
}, $array2);
// Display your view
?>
<?php foreach ($array2 as $entry): ?>
<h2><?= $entry['name'] ?></h2>
<?php foreach ($entry['attr'] as $attrName): ?>
<p><?= $attrName ?></p>
<?php endforeach ?>
<small>course price : <?= $entry['price'] ?></small>
<?php if ($entry['total_price'] > 0): ?>
<small><?= $entry['total_price'] ?></small>
<?php endif ?>
<?php endforeach ?>
Demo: https://3v4l.org/nS3Gl
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>
Please help with this...
I want to write a query to display sum of all marks of each student ( OR each record of a table - 1st CA + 2ND CA + EXAM SCORE = TOTAL).
I can display the marks successfully but can't fill in the 'Total'.
Please view the picture below.
View table
Please find my "mark table" and "subject table" structure below...
View table structure
Using the MVC architecture...
Here is my Model
function get_obtained_first_ca( $exam_id , $class_id , $subject_id , $student_id) {
$marks = $this->db->get_where('mark' , array(
'subject_id' => $subject_id,
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id))->result_array();
foreach ($marks as $row) {
echo $row['first_ca'];
}}
function get_obtained_second_ca( $exam_id , $class_id , $subject_id , $student_id) {
$marks2 = $this->db->get_where('mark' , array(
'subject_id' => $subject_id,
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id))->result_array();
foreach ($marks2 as $row) {
echo $row['second_ca'];
}}
function get_obtained_exam_score( $exam_id , $class_id , $subject_id , $student_id) {
$marks3 = $this->db->get_where('mark' , array(
'subject_id' => $subject_id,
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id))->result_array();
foreach ($marks3 as $row) {
echo $row['exam_score'];
}
}
Here is my autoload.php file content
$autoload['model'] = array('crud_model'); // The crud_model is my main model file.
Here is my controller
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');
Here is my view
<table>
<tbody>
<thead>
<tr>
<td style="text-align: center;">Subject</td>
<td style="text-align: center;">1st CA</td>
<td style="text-align: center;">2nd CA</td>
...
<td style="text-align: center;">Total</td>
</tr>
</thead>
<?php
$total_marks = 0;
$total_grade_point = 0;
$subjects = $this->db->get_where('subject' , array(
'class_id' => $class_id , 'year' => $running_year
))->result_array();
foreach ($subjects as $row3):
?>
<tr>
<td style="text-align: center;"><?php echo $row3['name'];?></td>
<td style="text-align: center;">
<?php
$obtained_mark_query = $this->db->get_where('mark' , array(
'subject_id' => $row3['subject_id'],
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id ,
'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
));
if($obtained_mark_query->num_rows() > 0){
$marks = $obtained_mark_query->result_array();
foreach ($marks as $row4) {
echo $row4['first_ca'];
$total_marks += $row4['first_ca'];
}
}
?>
</td>
<td style="text-align: center;"><?php
$obtained_mark_query = $this->db->get_where('mark' , array(
'subject_id' => $row3['subject_id'],
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id ,
'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
));
if($obtained_mark_query->num_rows() > 0){
$marks = $obtained_mark_query->result_array();
foreach ($marks as $row4) {
echo $row4['second_ca'];
$total_marks += $row4['second_ca'];
}
}
?></td>
</td>
<td style="text-align: center;">
<?php
echo $total_mark; // Here is where I need to fetch my Total
?>
</tr>
<?php endforeach;?>
Your effort to help me fill the total column is highly appreciated. Thanks in advance!
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
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>