how to get the result of cart in the model in codeigniter? - php

My cart have two roomname.I loop the cart to get all value of roomname and want data of the both two roomname in the view.
My Model
public function getextrabedinfo(){
foreach ($this->cart->contents() as $cart) {
$roomname = $cart['roomname'];
$query = $this->db->query("SELECT * FROM roomcalendar where roomname='$roomname' ");
return $query->result();
}}

According to your question if you want to send data from model to view then try your code in this way..
public function getextrabedinfo(){
$alldata = array();
foreach ($this->cart->contents() as $cart) {
$roomname = $cart['roomname'];
$query = $this->db->query("SELECT * FROM roomcalendar where roomname='$roomname' ");
$alldata[] = $query->result();
}
return $alldata ;
}
Try this .. Hope it will work.

Related

Codeigniter Limit Start

Hi i got a problem with a query with CodeIgniter 3.
I'm trying to get all users from database with limit and start excluding the ID of the user who is getting the list.
That's my model:
public function getUserList($id,$limit,$start){
$this->db->select('*');
$this->db->from('register');
$this->db->where('register.id' != $id);
$this->db->order_by("id");
$this->db->limit($limit,$start);
$result = $this->db->get();
return $result->result_array();
}
And that's the controller:
public function users(){
$this->load->model('User_model');
$id = $_GET['id'];
$limit = $_GET['limit'];
$start = $_GET['start'];
$data = $this->User_model->getUserList($id,$limit,$start);
echo "<pre>"; print_r($data); die;
$this->load->view('api', $data);
}
The problem is this function return me an empty array instead an array of 5 array.
Custom key/value method:
You can include an operator in the first parameter in order to control the comparison:
Change below line.
$this->db->where('register.id !=', $id);

fetching products of current user_id

I have function to fetch the products from ci_products table
the code is as follows
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$products = $this->db->get();
return $products;
}
now I want to fetch from where user_id is current user_id
I have tried few things but not working
what I have tried`
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$this->db->where('user_id',$this->session->userdata('user_id'));
$products = $this->db->get();
return $products;
}
$data = $this->db->get_where('product_ids',$product_ids);
$this->db->where_in('product_ids',$data);
$this->db->where('user_id',$this->session->userdata('user_id'));
$query = $this->db->get();
return $query->result_array();
Try this one :
In your first code:
function get_product_selections($product_ids)
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('product_id', $product_ids);
$products = $this->db->get();
return $products->result_array();
}
what do you mean when you put array() in the parameter? you want to retrieve an array() of products?
function get_product_selections()
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('user_id',$this->session->userdata('user_id')); // the user who is currently login
$products = $this->db->get();
return $products->result_array(); // return the results
}

Laravel - Can't use a manyToMany method after join

I've a strange behavior of Laravel when I'm trying to use a belongsToMany method, only after a join.
I've these classes: Meeting, Order, Organizer, User and Product
and these pivot tables: meeting_organizer, order_product, organizer_user
And this is order class
class Order extends \Eloquent {
public function meeting(){
return $this->belongsTo('Meeting');
}
public function state(){
return $this->belongsTo('OrderState', 'order_state_id');
}
public function products(){
return $this->belongsToMany('Product')->withPivot('qty');
}
public function sellers(){
$meeting = $this->meeting()->first();
if($meeting) return $meeting->sellers()->get();
else return array();
}
public function organizers(){
$meeting = $this->meeting()->first();
if($meeting) return $meeting->organizers()->get();
else return array();
}
When I use
$data['records'] = Order::all();
and then this loop:
foreach( $data['records'] as $r ){
if($r->meeting){
$r->meeting_date = $r->meeting->date;
$r->meeting_name = $r->meeting->name;
}
$r->status = $r->state->name;
$r->delivery_date = date_format(date_create($r->delivery_date), 'm-d');
$r->seller = '';
foreach( $r->sellers() as $s) $r->seller .= $s->name.', ';
$r->organizer = '';
foreach( $r->organizers() as $o) $r->organizer .= $o->name.', ';
$r->product = '';
foreach( $r->products as $p) $r->product .= $p->pivot->qty ." x ". $p->name.'<br />';
}
works well.
But if I use this join
$data['records'] = Order
::join('meeting_organizer', 'orders.meeting_id', '=', 'meeting_organizer.id')
->join('organizer_user', 'meeting_organizer.organizer_id', '=', 'organizer_user.organizer_id')
->where('organizer_user.user_id', '=', Auth::user()->id)->get();
the loop doesn't return any products.
So, I don't get any error, only an empty result.
If I try to print on screen inside my loop $r->id the result is wrong and the same number for all records, but all of other properties are correct.

codeigniter how to send query->num_rows from model to controler

Perhaps a simple question:
This is my striped code in my model:
public function trainees()
{
$this->db->select('*');
$this->db->from('relations');
$query = $this->db->get();
if ($query->num_rows() >0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
}
My controler:
public function adresslist() {
$this->load->model('Adresslist');
$data['trainees'] = $this->Adresslist->trainees();
$data['numrows'] = ??????
$this->load->view('adresslist', $data);
}
And in my view I load the table data with a foreach
works perfect.
But how do I send the value of num_rows to my controler?
The number of trainees is simply the length of the array returned, you don't need to know num_rows
$data['numrows'] = count( $data['trainees']);
will do the trick

choose a item in a database dropdown list in codeigniter

I have a dropdown list populated from a database of car models in codeigniter.
When user choose the value in dropdown (e.g. Lexus, Honda, etc.) the list of cars of chosen manufacturer should load. Hovewer, nothing works. Here's my current code.
//controller
public function carsPage() {
$this->load->model('model_users');
$data3['images'] = $this->model_users->getRow();
$data['tags'] = $this->model_users->get_dropdown_tags();
$newData = array_merge($data, $data3);
$this->load->view('cars', $newData);
}
//model
function get_dropdown_tags() {
//select distinct car_make from cars
$tags = $this->db->query("SELECT distinct car_make from cars");
$dropdowns = $tags->result();
foreach ($dropdowns as $dropdown)
{
$dropdownlist[$dropdown->car_make] = $dropdown->car_make;
}
$finaldropdown = $dropdownlist;
return $finaldropdown;
}
I have a theory about what you are trying to ask/do and this might work.
Controller:
public function carsPage() {
$this->load->model('model_users');
$data['images'] = $this->model_users->getRow();
$data['tags'] = $this->model_users->get_dropdown_tags();
$this->load->view('cars', $data);
}
Model:
public function get_dropdown_tags($car=NULL){
$this->db->select('car_make');
$this->db->from('cars');
$this->db->where('car_make', $car);
return $this->db->get();
}

Categories