Hi guys I tried to use the variable equation on Codeigniter as in the code I wrote below, Is my code writing correct? and here's my code so far I wanna using variable $amount in my controller
In Controller
$amount = $this->m_data->getTotalSales->$data['TOTAL_SALES'];
and this in Model
//GET TOTAL REVENUE
function getTotalSales(){
$this->db->select("(SELECT SUM(grand_total) FROM sales_order WHERE member = ".$this->session->userdata('ID').") - (SELECT SUM(amount) FROM payment WHERE member_id = ".$this->session->userdata('ID').") AS total_sales");
$query = $this->db->get();
if ($query->num_rows() >0){
foreach ($query->result() as $data) {
$hasilSemua[] = $data;
}
return $hasilSemua;
}
}
$amount = $this->m_data->getTotalSales();
function getTotalSales(){
$result1 = $this->db->select_sum("grand_total")
->from('sales_order')
->where("member = $this->session->userdata('ID')");
->get();
$result2 = $this->db->select_sum("amount")
->from('payment')
->where("member_id = $this->session->userdata('ID')")
->get();
return $result1->row()-$result2->row();
}
}
I believe this is what you are after. Note that I pass the UserID in as the second parameter to the WHERE function. This is in case there is any chance of the user having inputted it (always safter to do this anyway).
//GET TOTAL REVENUE
function getTotalSales(){
$db = $this->db;
$db->select('SUM(grand_total)');
$db->where('member',$this->session->userdata('ID'));
$q1 = $db->get_compiled_select('sales_order');
$db->select('SUM(amount)');
$db->where('member_id',$this->session->userdata('ID'));
$q2 = $db->get_compiled_select('payment');
$query = $db->query("SELECT ($q1) - ($q2) as total_sales");
$data = $query->row_array();
return $data['total_sales'];
}
You can then call $amount = $this->m_data->getTotalSales; to get your result.
Related
Original function:
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$query = $this->db->get('orders');
return $query->result_array();
}
}
I need to JOIN data from table order_shipping and order_products where order_id = order_id
I do:
/*
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
This above code working correct. But currently JOIN only after else. I need do the same way before else:
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
But this code before else not working correct for me and currently display wrong data. When I try fetch all orders, then second function display correct all orders. But when I try fetch example only order ID 2, then is used first function and display wrong data, example when I fetch order ID2, then always display only order with ID 1.
This function is for REST API fetch orders.
I just want to do this if(status==0) then only where condition is run and print only related to status==0 record.And when status==1 come then it bypass the where clause and print both data for status 0 and 1. I don't know how to build a query for this. Please help me in this.
my model code
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->where('student_info.admin_id',$admin_id)
->join('admin','admin.admin_id = student_info.admin_id')
->get();
return $query->result();
}
This code work properly and print separate data whose status 0 or 1.
Only admin_id = 1 contain Status = 1, other id's contain 0 status.
Replace your function with the following code...
<?php
function listing($status)
{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
if($status==0)
{
$this->db->where('student_info.admin_id',$admin_id);
}
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
?>
Try this:
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->join('admin','admin.admin_id = student_info.admin_id')
->where("student_info.admin_id = IF (admin.status = 0, $admin_id, '>-1'")
->get();
return $query->result();
}
It solves my problem..
function listing()
{
$admin_id = $this->session->userdata('admin_id');
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->or_where('admin.status < 1');
$query = $this->db->get();
return $query->result();
}
<?php
function listing($status)
{
if($status == 0){
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}else{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
}
?>
I am working on Basketball Referee project. I am trying to update table checking input value.
I am sending the value from my control.php like this:
$check_user = $this->input->post("headreferee");
$check = $this->sql_model->check1($check_user);
Here is my sql_model.php:
function check1($referee_name)
{
$sql = "SELECT * FROM duties WHERE username ='{$referee_name}' ";
$query = $this->db->query($sql);
if($query->num_rows()>0)
{
$this->db->set('count','count'+1);
$this->db->insert('duties');
}
else
{
return 0;
}
}
Actually it is increasing count and adding new row but without any referee_name. It has to find the correct referee_name and increase that row's count.
If you want to update so please modify your check1() function by below script.
function check1($referee_name)
{
$sql = "SELECT * FROM duties WHERE username ='{$referee_name}' ";
$query = $this->db->query($sql);
$result_referee = $query->row();
$countNew = $result_referee->count + 1;
if($query->num_rows()>0)
{
$countArray = array('count'=>$countNew);
$this->db->where('username',$referee_name);
$this->db->update('duties',$countArray);
}
else
{
return 0;
}
}
I hope that will be helpful for you as your solution.
I'm attempting to modify a date field from my database before it gets outputted to the view, but I'm not having much luck. This code doesn't seem to work, what am I doing wrong?
function get_journal_entry($id)
{
$sql = 'SELECT * FROM journal WHERE user_id = '.$this->tank_auth->get_user_id().' AND id = '.$id;
$query = $this->db->query($sql);
$query['created'] = date("c", strtotime($query['created']));
return $query->row_array();
}
$this->db->query returns a query object, not your results. You need to modify the row after calling $query->row_array.
$query = $this->db->query($sql);
$result = $query->row_array();
$result['created'] = date("c", strtotime($result['created']));
return $result;
Another version of the code, which may work:
function get_journal_entry($id)
{
$sql = 'SELECT * FROM journal WHERE user_id = ' . intval($this->tank_auth->get_user_id()) . ' AND id = ' . intval($id);
$row = $this->db->query($sql)->row_array();
$row['created'] = date("c", strtotime($row['created']));
return $row;
}
I have succeeded in querying one table to get information that is needed to query another table (If you can see a better way I would be grateful!)
My question is: How can I have multiple values come back from the first query and have my second query come back with multiple results.
As you can see I am inserting the returned result from query one into query two “msg_id = ?”(I use '$datas' to fill the ‘?’) but if my results from query one have multiple values then how will this work?
Also how can I make it get multiple results from query one? at the moment if there are multiple values in mysql it only grabs the first one it reads.
My MODEL code is as follows:
function check() {
$this->db->select('msgto_message');
$this->db->from('msgto');
$this->db->where('msgto_display', 'y');
$this->db->where('msgto_recipient', '1');
$w = $this->db->get();
if ($w->num_rows() > 0) {
$rowe = $w->row_array();
$datas = $rowe['msgto_message'];
}
$sql = "SELECT msg_content FROM msg WHERE msg_id = ?";
$data = $this->db->query($sql, $datas) or die(mysql_error());
if ($data->num_rows() > 0) {
foreach($data->result_array() as $row) {
$data = $row;
}
return $data;
}
}
My CONTROLLER code is as follows:
function index() {
$this->load->model('data_model');
$data['rows'] = $this->data_model->check();
$this->load->view('home', $data);
}
Thank you anyone that helps me, I greatly appreciate it!
You may find a database join helpful here. While I'm not entirely sure what you're trying to do here (especially in that foreach loop!), something like this might get you going in a more efficient direction:
function check() {
$this->db->select('msg.msg_content');
$this->db->from('msgto');
$this->db->join('msg', 'msgto.msgto_message = msg.msg_id');
$this->db->where('msgto.msgto_display', 'y');
$this->db->where('msgto.msgto_recipient', '1');
$data = $this->db->get();
if ($data->num_rows() > 0) {
return $data->result_array();
}
}
This asks the database to join together the msg and msgto tables based on msgto_message matching msg_id, and can use the WHERE critera on msgto while returning the results from msg.
Right not you are getting only the first row from query one
if ($w->num_rows() > 0) {
$rowe = $w->row_array();
$datas = $rowe['msgto_message'];
}
To get all the records you need to cycle through the result
if ($w->num_rows() > 0) {
foreach($rowe = $w->row_array() as $datas) {
$datas = $rowe['msgto_message'];
$sql = "SELECT msg_content FROM msg WHERE msg_id = ?";
$data = $this->db->query($sql, $datas) or die(mysql_error());
if ($data->num_rows() > 0) {
foreach($data->result_array() as $row) {
$data = $row;
}
return $data;
}
}
}