I have the following codes in my model.
This is my original code.
I have tested that the value of my $category_id = 1
function getSubCategory($category_id){
$this->db->select('id, subcategory');
$this->db->from('subcategory');
$this->db->where('category_id', $category_id);
$this->db->order_by("subcategory", "asc");
$query = $this->db->get();
return $query->result();
}
however I am encountering a problem where $this-db->where is not working.
function getSubCategory($category_id){
$cat = 1;
$this->db->select('id, subcategory');
$this->db->from('subcategory');
$this->db->where('category_id', $cat);
$this->db->order_by("subcategory", "asc");
$query = $this->db->get();
return $query->result();
}
I tried this line of code just to make sure that I am not doing any syntax error. And with these line of codes I am getting what I wanted. I wonder what makes my original code not work.
It would be a great help if someone can help. Thanks.
It turns out that the problem I was having is from the AJAX function that I was calling to the call the method in my model.
The answer is seen here
AJAX not displaying result
Related
this code is written by Codeigniter and it is in the models folder
I need to add
$this->db->limit($limit,$offset);
to following code. Where do I put it? The model code is here
$this->db->where('comments.product_id', $product_id);
$this->db->select('comments.comment,comments.ratings,comments.comment_users_id,comment_users.name');
$this->db->from('comments');
$this->db->join('comment_users', 'comment_users.id = comments.comment_users_id');
$this->db->join('product', 'product.product_id=comments.product_id');
$this->db->order_by('comments.id', 'DESC');
return $this->db->get()->result();
Order is doesn't matter just do like this
$this->db->where('comments.product_id', $product_id);
$this->db->select('comments.comment,comments.ratings,comments.comment_users_id,comment_users.name');
$this->db->from('comments');
$this->db->join('comment_users', 'comment_users.id = comments.comment_users_id');
$this->db->join('product', 'product.product_id=comments.product_id');
$this->db->order_by('comments.id', 'DESC');
$this->db->limit($limit,$offset); //here added
return $this->db->get()->result();
I am making a travel agency website in codeigniter. I am getting destination of africa from my database and displaying on a page.
here is my code of controller:
public function africa() {
$data['africa']= $this->Travel->africa_des();
$this->load->view('africa', $data);
}
my Model:
function africa_des() {
$this->db->distinct();
$this->db->select();
$this->db->from('travels_detail');
$this->db->like('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->result_array();
}
I want to get total number of flights of each destination that i have in my database. For instance I have 25 flights of Abuja so I want to get that number with Abuja destination.
Anyone have any idea?
Try this one
public function africa_desc(){
$this->db->distinct();
$this->db->select('*');
$this->db->from('travels_detail');
$this->db->where('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->result_array();
}
When you are using $this->db->distinct() you must use $this->db->group_by('column_name') see this answer https://stackoverflow.com/a/19509869/6098616. In your case I will assume that destination is your column name.
public function africa_desc(){
$this->db->select('*');
$this->db->distinct();
$this->db->group_by('destination');
$this->db->from('travels_detail');
$this->db->where('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->result_array();
}
Hope this will help.
Try this to count flights
public function africa_desc(){
$this->db->select('*');
$this->db->distinct();
$this->db->group_by('destination');
$this->db->from('travels_detail');
$this->db->where('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->num_rows();
}
so I'm having this issue not being able to get just a string from a table. This is my method in the controller:
public function get_tree($id){
$data['tree'] = $this->Product_model->get_tree($id);
return $data;
}
And this is the function in the model:
function get_tree($id){
$this->db->select('ascending_path');
$this->db->from('category');
$this->db->where('id', $id);
$result = $this->db->get();
return $result;
}
I'm showing this with Ajax into the view but nothin seems to show up. There is not even an error, 200 status code and the request is shown in the access log. Any hints?
PS: if I try to json_encode it and then pass the dataType:json in the ajax call all it comes back is:
{"tree":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null}}
Everything seems to be null. I don't even know what is that object, I mean, the query is supposed to bring just one string.
Change return $result;
to return $result->result();
Read more about Generating Query Results here
use this
function get_tree($id){
$query = $this->db->query("SELECT ascending_path FROM category WHERE id ='$id'");
$result = $query->result_array();
return $result;
}
I am trying to do this sql query on codeigniter
SELECT* FROM person WHERE type = 'staff' AND description LIKE 'university%'
I am not sure if it is correct...
$this->db->get_where('person' , array('type'=>'staff'))
->like('description','university%')
->result_array();
does anyone have an idea about my case? thanks in advance ...
Using Active Record you can do so
$query = $this->db->select('*')
->from('person')
->where('type','staff')
->like('description','university','after')
->get();
$result = $query->result_array();
Make sure you pass after as a third parameter in like() function so active record will add the wild card i.e % after university so it will look like LIKE 'university%'
The docs has this explained very well, but their API is very simple. To produce the query you need the relevant code which looks like this:
$this->db->select('*')
->from('person');
->where('type', 'staff')
->like('description','university');
$query = $this->db->get();
$result = $query->result_array();
I never used chaining(even though i know it's possible), but breaking down your question should be easy ;
$this->db->from('person');
$this->db->where('type','staff');
$this->db->where('description', 'university%');
$query = $this->db->get();
$result = $query->result_array();
return $result;
So I'm trying to utilise CodeIgniter's URI segment functionality. I have the URI segments being used as ids to retrieve information from the database.
It's working fine, but I have noticed that if I put in an id that doesn't exist in the database, it spits out a database error.
How do I prevent this from happening?
Model Code:
$query = $this->db->select('*')
->from('questions')
->where('questions.id', $question_id)
->join('users', 'users.id = questions.user_id')
->get();
return $query->row();
Call to model in controller:
$question_id = $this->uri->segment(3);
$data['question'] = $this->forum_model->get_question($question_id);
PHP error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
You need to check if the query returned any results before calling $query->row
The way to do it is:
$query = $this->db->select('*')
->from('questions')
->where('questions.id', $question_id)
->join('users', 'users.id = questions.user_id')
->get();
if($query->num_rows() > 0)
return $query->row();
else
return false;
$query->num_rows() will give you the number of rows returned.
to debug just do this...
//if this gives u no result, then thats the problem because it couldnt find the
//$this->uri->segment(3);
$question_id = $this->uri->segment(3);
echo $question_id;
//if this prints out the expected result.
//then you should know that the problem is still somewhere down the code.
$data['question'] = $this->forum_model->get_question($question_id);
var_dump($data['question']);
//this is for your model.
echo $question_id
$query = $this->db->select('*')
->from('questions')
->where('questions.id', $question_id)
->join('users', 'users.id = questions.user_id')
->get();
return $query->row();
NOTE: if all this echos outputs valid values, then check somewhere else for the problem