I'm new to PHP codeigniter. I need a big help from you people. This is one of my project matter. The thing is i'm going to access a table in the database in order to calculate the distance. Table name is gps_info. In there, there are two attributes which can be used to calculate the distance. Those are speed and timestamp. This my active record
public function get_distance() {
$this->db->select('speed','timestamp');
$this->db->from('gps_info');
$this->db->orderby('gps_info.id','desc');
$query = $this->db->get();
return $query->result();
}
This is not correct.. Can you help me please how to write this service correctly :)
The working code for fetching the data what you want:
public function get_distance()
{
$res = $this->db->select('speed, timestamp')
->from('gps_info')
->orderby('id', 'desc')
->get()
->result_array();
return $res;
}
Your select portion is wrong, it should be:
$this->db->select('speed, timestamp');
Related
I want to get data from two tables, the second table is for rating so i want to get rating of products simultaneosly. Below code is not working for me if i change
$this->db->select('dg_products.',', AVG(dg_rating.rating) As
averageRating');
to
$this->db->select('*');
then it is working.
Please help to sort out my issue.
public function get_rating()
{
$this->db->select('dg_products.*','*, AVG(`dg_rating.rating`) As averageRating');
$this->db->from('dg_products');
$this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
$this->db->where('dg_products.is_featured_prod','1');
$this->db->group_by("dg_products.id");
$query = $this->db->get();
$result = $query->result();
return $result;
}
Try it like this :
$this->db->select('dg_products.*, AVG(`dg_rating.rating`) As averageRating');
you just have an unneeded quotes in there.
In my Model, I wrote this function with MySQL raw query
function get_question_result($lr_id)
{
$raw_query = 'SELECT question_record.qr_id, LEFT(question.question, 50) as question,
question.correct_answer
FROM question_record
INNER JOIN question ON question.q_id = question_record.q_id
WHERE question_record.lr_id = '.$lr_id.' ';
$query = $this->db->query($raw_query);
$questionresult = $query->result_array();
return $questionresult;
}
It worked fine. It gave me the array I want. I continued my project.
Then suddenly I was curious to try it in CI Active Record Class.
function get_question_result($lr_id)
{
$this->db->select('question_record.qr_id, LEFT(question.question, 50) as question, question.correct_answer');
$this->db->from('question_record');
$this->db->join('question', 'question.q_id = question_record.q_id', 'inner');
$this->db->where('question_record.lr_id', $lr_id);
$result = $this->db->get()->result_array();
return $result;
}
It didn't work. It gave me this error
PHP Fatal error: Call to a member function result_array() on a non-object
Just out of curiosity, where did I do wrong?
Was it me writing it wrong or the result data structure with Active Record is just different?
'cause when I tried it again in Active Record without selecting this field
LEFT(question.question, 50) as question
It worked but it didn't give the field I want. Do you guys know why?
In your $this->db->select() call you need pass FALSE as second parameter so that active record will not try to add backticks ` for your columns in select statement
function get_question_result($lr_id)
{
$this->db->select('question_record.qr_id, LEFT(question.question, 50) as question, question.correct_answer',FALSE);
$this->db->from('question_record');
$this->db->join('question', 'question.q_id = question_record.q_id', 'inner');
$this->db->where('question_record.lr_id', $lr_id);
$result = $this->db->get()->result_array();
return $result;
}
According to docs
$this->db->select() accepts an optional second parameter. If you set
it to FALSE, CodeIgniter will not try to protect your field or table
names with backticks. This is useful if you need a compound select
statement.
$this->db->select();
I've to build a research system for an announcements website. We can search through categories or via the announcement city.
When someone research something I want to set a limit of results per category (example : 3) using Eloquent.
I know this isn't easy at all, even in raw SQL, and it's been hours i'm trying to find a way without success. I tried to add a closure in the query but it's very hard for me to understand how to use that properly ...
I understood i needed to make a subquery to do the trick, but it's also tricky to know how to organize all this.
The workbench (followed by the current query which isn't working) :
Here's the controller with the query :
$announcements = Announcement::select('announcements.*')
->withCitySlug($location)
->withCategory($category_id)
->perCategoryLimit(3)
->get();
The model Announcement (the interesting part) :
/**
* HasManyThrough
*/
public function categories()
{
return $this->belongsToMany('Category', 'announcement_categories');
}
public function scopeWithCitySlug($query, $city_slug) {
if (empty($city_slug)) return $query;
return $query->where('city_slug', 'LIKE', '%'.$city_slug.'%');
}
public function scopeWithCategory($query, $category_id) {
if ($category_id === FALSE) return $query;
return $query->join('announcement_categories', 'announcement_categories.announcement_id', '=', 'announcements.id')
->join('categories', 'categories.id', '=', 'announcement_categories.category_id') ->where('categories.id', '=', $category_id);
}
public function scopePerCategoryLimit($query, $limit) {
return $query;
/*return $query->with(array('categories' => function($q)
{
$q->limit(3)->get();
}));*/
}
Any Eloquent/SQL expert around here ? A little help in the way to solve this would be perfect ;)
PS : If i didn't give enough code to understand the problem, just let me know !
Well, you can select all categories and use an foreach with a limit to get all results or use partition by from sql.
My problem is pretty simple. I'm using codeigniter active record, and all the model class has to do is select the most recent item from a table that belongs to the user.
function get_progress($users_id)
{
$query = $this->db->get('progress');
$this->db->where('user_key', $users_id);
$this->db->order_by("id", "desc");
$this->db->limit(1);
return $query->row_array();
}
Seems simple, but for some reason, it's grabbing the lowest id that matches the user_key.
I've tried changing the where statement to
$this->db->where('id', '2');
And it works, but of course, that's just for troubleshooting. I need variables.
I've rewritten few ways, including using get_where(), and changing desc to asc. No matter what, it's grabbing the low id. How can I select the highest id where user_key is the matching number.
You can try the code below
function get_progress($users_id){
return $this->db->from('progress')
->where('user_key', $users_id)
->order_by("id", "DESC")
->get()
->row();
}
You will get the last recent row as STD Class object
function get_progress($users_id)
{
$this->db->select('*')
->from('progress')
->where('user_key',$users_id)
->order_by('id','desc')
->limit(1);
$q=$this->db->get();
return $q->result_array();
}
I have a function which I use all over the place to pull users friends out of a database. However I recently had to delete a few users for causing problems on the forums, this has given me a few "Trying to get property of non-object" problems and I have traced it down to this function.
function isFriend($user_id, $friend_id){
$this->db->from('friends');
$this->db->where('user_id', $user_id);
$this->db->where('friend_id', $friend_id);
$this->db->where('removed', 0);
$query = $this->db->get();
$result = $query->result();
return count($result);
}
Does anyone know how I can adjust this function to ignore deleted users?
If all you're doing is counting the results you can try this instead:
$this->db->count_all_results();
Like this:
function isFriend($user_id, $friend_id)
{
$num = $this->db
->from('friends')
->where('user_id', $user_id)
->where('friend_id', $friend_id)
->where('removed', 0)
->from('TABLE_NAME')
->count_all_results();
return $num;
}
It should return 0 if there are no results, whereas $query in your current function (I believe) may not return an object you can call result() on (hence your error).
http://codeigniter.com/user_guide/database/active_record.html
$this->db->count_all_results();
Permits you to determine the number of rows in a particular Active
Record query. Queries will accept Active Record restrictors such as
where(), or_where(), like(), or_like(), etc. Example:
echo $this->db->count_all_results('my_table'); // Produces an integer, like 25
$this->db->like('title', 'match');
$this->db->from('my_table'); echo
$this->db->count_all_results(); // Produces an integer, like 17
There may be "better" ways, but without knowing the guts of your application it's hard to say.
why did you actualy remove them from database when you have the flag "removed" ?
Anyway, i think you should also remove the connections of those removed users, i don't see anything wrong with the function.
Trying to alter the function to work, it's just a hack and it's not good practice.
I think the function itself looks fine but it seem likely your errors are caused when the query returns 0 results.
if the removed col in the friends table or the users table?
Maybe you should perform a join to the users table.