Codeigniter: fetch and sum field values - php

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!

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>

Return table with foreach not echo

I am writing a word press plugin using short code.
I have a array like this:
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10),
2 => array(same above),
3 => array(same above)
.......................
);
How can I use foreach loop or any ways to return (not echo) a table like this:
<table>
<tr>
<th>Name</th>
<th>Birth</th>
<th>Number</th>
</tr>
<tr>
<td>Show $data[0]['name']</td>
<td>Show $data[0]['birth']</td>
<td>Show $data[0]['number']</td>
</tr>
<tr>
Show all keys and all values of $data same above into each <tr> tags
</tr>
</table>
Thanks a lot!
You can do something like this
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10),
2 => array(same above),
3 => array(same above)
);
$str="<table><tr>";
$str.="<th>Name</th>";
$str.="<th>Birth</th>";
$str.="<th>Number</th></tr>";
foreach ($data as $key => $value) {
# code...
$str.="<tr>";
$str.="<td>Show $data['name']</td>";
$str.="<td>Show $data['birth']</td>";
$str.="<td>Show $data['number']</td></tr>";
}
return $str;
For me the best way is to put your table inside var and create it row by row, like this :
`
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10)
);
$myTable = "
<table>
<tr>
<th>Name</th>
<th>Birth</th>
<th>Number</th>
</tr>";
foreach($data as $key => $value)
{
$myTable .= "
<td>Show ".$value['name']."</td>
<td>Show ".$value['birth']."</td>
<td>Show ".$value['number']."</td>
</tr>";
}
$myTable.="</table>";
echo $myTable; //You can also return it
`

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>

How to print out the value in view file?

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.

Categories