i am new to Codeigniter, i have two tables named as Users and Amount, there are multiple amounts in Amount Table across one user have same user_id which is foreign key. i want to get data and sum up the amounts value if user_id is same.
My Modal is:
public function client_list(){
$this->db->select_sum('*', sum('amount'));
$this->db->from('users');
$this->db->join('amount', 'amount.user_id = users.ID');
$query=$this->db->get();
$result=$query->result();
return $result;
}
Controller is:
public function index(){
$data['records'] = $this->Admin_model->client_list();
$this->load->view('admin/Client_list', $data);
}
View part is:
<?php
foreach($records as $record):
?>
<tr class="odd gradeX">
<td>
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="checkboxes" value="1" />
<span></span>
</label>
</td>
<td> <?php echo $record->firstname; ?> </td>
<td> <?php echo $record->lastname; ?> </td>
<td> <?php echo $record->phone;?> </td>
<td> <?php echo $record->email;?> </td>
<td> <?php echo $record->amount;?> </td>
<td> <?php echo $record->dateofbirth;?> </td>
</tr>
<?php endforeach; ?>
Kind Help would be highly appreciated.
Related
Model
// Relationship in \App\FatherRegistrars and \App\MotherRegistrars and \App\GuardianMaleRegistrars \App\GuardianFemaleRegistrars
public function student_registrars()
{
return $this->belongsToMany('App\StudentRegistrars')->withTrashed();
}
Controller
public function index(Request $request)
{
$dataFathers = \App\FatherRegistrars::get();
$dataMothers = \App\MotherRegistrars::get();
$dataGM = \App\GuardianMaleRegistrars::get();
$dataGF = \App\GuardianFemaleRegistrars::get();
// manual pagination using code attached in AppServiceProvider.php
$data = $dataFathers->toBase()->merge($dataMothers)->paginate($items);
return view('parents-guardians-registrars.index', compact('data', 'dataFathers', 'dataMothers', 'dataGM', 'dataGF'))->withItems($items);
}
View
#foreach($data as $var)
<tr>
<td style="text-align:center;">
<input type="checkbox" id="select" class="sub_chk" data-id="{{$var->id}}" value="{{$var->id}}" name="selected_values[]"/>
</td>
<td>{{$var->id }}</td>
<td>{{$var->name}}</td>
<td>{{$var->email}}</td>
<td>
<?php $elements = array(); ?>
#foreach($var->student_registrars as $category)
<?php $elements[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements); ?>
</td>
<td>
// Second foreach should be here
</td>
<td>
Detail
</td>
#endforeach
// Second foreach
#foreach($dataGM as $var2)
<tr>
<td>
<?php $elements = array(); ?>
#foreach($var2->student_registrars as $category)
<?php $elements[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements); ?>
</td>
</tr>
#endforeach
</tr>
And the result for code above is:
I have a little problem related multiple foreach in one view. It's actually just a simple problem but I am stuck here. Any body can solve it?
Should I use partial view to do this?
This may not exactly work (not tested or verified), but you can give something like this a try:
#foreach($data as $var)
<tr>
<td style="text-align:center;">
<input type="checkbox" id="select" class="sub_chk" data-id="{{$var->id}}" value="{{$var->id}}" name="selected_values[]"/>
</td>
<td>{{$var->id }}</td>
<td>{{$var->name}}</td>
<td>{{$var->email}}</td>
<?php $elements = array(); ?>
#foreach($var->student_registrars as $i => $category)
<td>
<?php $elements[] = ' '.$category->name.' '; ?>
<?php echo implode(',<br>', $elements); ?>
</td>
<td>
<?php $elements2 = array(); ?>
#foreach($var2->student_registrars[$i] as $category)
<?php $elements2[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements2); ?>
</td>
#endforeach
<td>
Detail
</td>
</tr>
#endforeach
The problem you have is that there is no direct association $data and $dataGM. You should try and use Relationships so that the data needed for $dataGM can be accessed as a relationship from $data, e.g.
$children = $data[$i]->children;
$adopted = $data[$i]->adopted_children;
Then you can loop directly on these items instead of having to create multiple variables.
I am building a store front with CodeIgniter where list of items from the database is displayed with a checkbox in the View. The user is to select items and enter their quantity. When the user Click Send, the list of Items and their quantity that user set will be submitted to the Database.
Here is the View Code:
<table>
<thead>
<tr>
<th>SN</th>
<th>Product</th><th>Tick</th>
<th>quantity</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
foreach($it as $r):
$i++;
?>
<tr><td><?php echo $i; ?></td>
<td><?=$r['Item'];?></td>
<td>
<input type="checkbox" name="item[]" value="<?=$r['Item']?>" /></td>
</td>
<td>
<input type="number" name="qty[]" />
</td>
</tr> <?php endforeach?>
<tr><td colspan="3">
<input type="submit" name="btnub" value="Send Order" /></td></tr>
</tbody>
</table>
<!-- Controller Code (CodeIgniter) -->
<!-- CodeIgniter Controller Code to harvest the Order -->
if (isset($_POST['btnub'])) {
foreach($_POST['item'] as $key=>$item) {
for ($i = 1; $i < 2000; $i++) {
$h = "qty" . $i;
$qty = $this->input->post[$h];
}
$tt = array(
'item' => $item,
'Qty' => $qty,
);
$this->tranmodel->insertthing("cuzorder", $tt);
} }
my code runs well but im getting this error Message: Undefined property: stdClass::$Date and i dont know where this error came from, as far as ive seen my codes are correct. The problem is in my foreach code
here's my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class News_and_events extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin_model', 'am');
}
public function index(){
if($this->session->userdata('logged_in')){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
redirect('login', 'refresh');
}
}
}
my model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Admin_model extends CI_Model{
public function saveData($array){
$this->db->insert('news_and_updates', $array);
}
public function getAllData(){
return $this->db->select(
'news_and_updates.Event',
'news_and_updates.Description',
'news_and_updates.Date'
)
->from('news_and_updates')
->order_by("Date", "desc")
->get()->result_object();
}
}
?>
and my views
<script type="text/javascript">
$(document).ready(function(){
$("#add_another").click(function(){
alert('test');
});
});
</script>
<div class="container" >
<br />
<br />
<br />
<ul id="nav">
<li><h4>Home</h4></li>
<li><h4>News and Events</h4></li>
<li><h4>Activities</h4></li>
</ul>
<div class="starter-template">
<h1>News And Events</h1>
<?php if($this->session->flashdata('add_another')):?>
<div id="add_another" style="float:left;">
<input type="button" value="Add Another" class="btn btn-primary" />
</div>
<?php else: ?>
<form action="<?php echo base_url().'news-and-events/add'?>" method="post">
<?php echo validation_errors('<div class="error">', '</div>');?>
<table class="table-striped">
<tr>
<td>Date: </td>
<td><input type="text" id="datepicker" name="date" value="<?php echo set_value('date');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td >Event: </td>
<td ><input type="text" name="event" value="<?php echo set_value('event');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td width="20%">Description: </td>
<td><textarea cols="30" rows="5" name="description" ><?php echo set_value('description');?></textarea></td>
</tr>
<tr>
<td width="20%"> </td>
<td><input type="submit" value="Add" class="btn btn-success" /></td>
</tr>
</table>
</form>
<?php endif; ?>
<br />
<br />
<table class="table" >
<tr>
<th>Date</th>
<th width="51%">Event</th>
<th>Description</th>
<th>Options</th>
</tr>
<?php foreach($allData as $x => $allDatas): ?>
<tr>
<td><?php echo $allDatas->Date; ?></td>
<td><?php echo $allDatas->Event; ?></td>
<td></td>
<td></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div><!-- /.container -->
<script>
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#datepicker').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: "yy-mm-dd"
});
</script>
in my foreach code the error is this one $allDatas->Date; it says Message: Undefined property: stdClass::$Date
and that's the field name in my table Date
can someone help me figured this out??
any help is much appreciated! thanks! can't find the error
In your model or controller - after the getAllData method is executed:
echo the result of $this->db->last_query()
Manually execute the returned query in your database, then fix your query
As the comments started, The issue is that Date isn't generated from the query, which means something is wrong in that level, not the output itself.
Make sure the query is correct, dump it before you load the view directly from the controller to see what it gets, if it doesn't get a Date, your issue is elsewhere, probably in the query itself.
Sorry for asking this old questions, and I know that I've read before I ask here, it's can use database for adding more cart without limitation. I already try to use ci_sessions table to store session but still no luck, I only can adding 6 items maximum.
please help me, I looking for some example for this almost two days and result is nothing
EDITED
this is my view
<table id="box-table-a" summary="Employee Pay Sheet">
<thead>
<tr>
<th scope="col">Description</th>
<th scope="col">Price</th>
<th class="centered" scope="col">Options</th>
</tr>
</thead>
<tbody>
<?php foreach ($foto_produk->result() as $key => $value) {?>
<tr>
<td><?php echo $value->description;?></td>
<td><?php echo $value->price;?></td>
<td class="centered"><input type="checkbox" name="produk_foto[]" value="<?php echo $value->id;?>" /></td>
</tr>
<?php }?>
</tbody>
</table>
here's my controller code
if($this->input->post('produk_foto')){
$id_foto = $this->input->post('produk_foto');
foreach ($id_foto as $key => $value) {
$this->db->where('id', $value);
$query = $this->db->get('foto_product');
if($query->num_rows() > 0){
foreach($query->result() as $ids => $rows){
echo $rows->id.'<br />';
$data_produk = array(
'user_data'=> array(
'id' => $rows->id,
'price' => $rows->price,
'name' => $rows->description,
'qty' => $rows->aantal
)
);
$this->cart->insert($data_produk);
}
}
}
}
and this my view code
<?php if(!$this->cart->contents()):?>
<div class="alert-box warning">The regular products are empty.</div>
<div class="clearfix"></div>
<?php else:?>
<hr>
<h4>REGULAR PRODUCTS</h4>
<div class="order_detail" id="Display">
<table>
<thead>
<tr>
<th>DESCRIPTION</th>
<th>QUANTITY</th>
<th>PRICE PER ITEM(S)</th>
<th>TOTAL</th>
<th>REMOVE</th>
</tr>
</thead>
<tbody>
<?php foreach($this->cart->contents() as $rows):?>
<tr>
<td style="font-weight:bold;"><?php echo $rows['name'];?></td>
<td>
<?php echo form_open(current_url());?>
<input type="text" size="3" name="quantity" value="<?php echo $rows['qty'];?>" />
<input type="hidden" size="3" name="rowid" value="<?php echo $rows['rowid'];?>" />
<input type="submit" name="update" value="Update" />
<?php echo form_close();?>
</td>
<td><?php echo $rows['price'];?></td>
<td><?php echo $this->cart->format_number($rows['subtotal']);?></td>
<td>delete</td>
</tr>
<?php endforeach;?>
</tbody>
<tfoot>
<tr>
<td colspan="3">Total Products</td>
<td colspan="3">€ <?php echo $this->cart->format_number($this->cart->total());?></td>
</tr>
<tr>
<td colspan="3">Total Shipping</td>
<td colspan="3"></td>
</tr>
<tr>
<td colspan="3"></td>
<td colspan="3" style="padding:0;text-align:center;">
<p>TOTAL :</p>
<span class="tot">€ <?php echo $this->cart->format_number($this->cart->total());?></span>
</td>
</tr>
</tfoot>
</table>
</div>
<?php endif;?>
with this code I want to insert using checkbox with array, and I have more than 6 checkbox
thank you in advance
Ok it's solved by my self..
I don't know that in the name of product there are special characters..
and shame of me..
but thank you anyway
I have a problem in cakephp
controller
$due = $this->Issue->find('all' , array('conditions' => array ('Issue.user_id'=>6)));
$this->set('due',$due);
$id = $due['0']['Issue']['book_id'];
$b = $this->Bookmaster->find('first' , array ('conditions' => array ('Bookmaster.id' => $bookid)));
$book = $this->Book->find('all',array('conditions' => array ('Book.id' =>$id)));
$bookid = $book['0']['Book']['bookmaster_id'];
Ctp contain
<h1> ISSUED BOOK DETAILS </h1>
<table width="200" border="2" bordercolor="#009933">
<tr>
<td> BOOK ID </td>
<td> TITLE </td>
<td> AUTHOR </td>
<td> EDITION </td>
<td>ISSUED ON </td>
<td> DUE DATE </td>
<td> POTENTIAL FINE</td>
</tr>
<?php foreach ($due as $data ) { ?> <tr>
<td> <?php echo $data['Issue']['book_id']; ?> </td>
<td> <?php echo $b ['Bookmaster']['title'] ;?></td>
<td> <?php echo $b['Bookmaster']['author'];?></td>
<td> <?php echo $b ['Bookmaster']['edition'];?> </td>
<td> <?php echo $data['Issue']['issue_date_time']; ?> </td>
<td> <?php echo $data['Issue']['due_date']; ?> </td>
<td> <?php echo $out['Fine']['fineamount'];?> </td>
</tr> <?php }?>
</table>
My problem is how can I manipulate with this with for loop.
My loop is not iterating for bookmaster.. so how can I do that..
How can i increment the value of bookid
The problem is you are looping on $due, and the $b data is a key valued array (i.e. $b[0]['bd'], $b[1]['bd'], $b[2]['bd'], etc.) Therefore, the key (or the index) for the array you are searching for will not ever be aligned with the $due loop.
A better solution is to write a join so each record contains the information you require and the loop will be able to display the data you require. More requirements / information is required on your part to enable anyone here to offer a suggestion.