I'm trying to create pagination in codeigniter and I have it working, but I have a small issue. It seems to be loading all the entries in my database and not the selected ones I want.
public function original_count() {
$this->db->where('type', 'Original');
return $this->db->count_all("story_tbl");
}
I know that whats happening is that the last line is overrighting my previous statements. I can't seem to find a way around it though. I tried just a staight sql statement and then returning it, but I could not get that to work either.
this was my statement...
SELECT COUNT(*) FROM story_tbl where type = 'Original';
Some help would much appreciated! :)
CI has inbuilt count method
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:
https://www.codeigniter.com/userguide2/database/active_record.html
$total_count = $this->db->count_all_results('story_tbl', array('type' =>'Original'));
You could also use the built-in num_rows() function...
$query = $this->db->where('type', 'original')->get('story_tbl');
return $query->num_rows();
First Try this one.
$query = $this->db->where('tbl_field', 'value')
->get('your_table');
return $query->num_rows();
Besides this Codeigniter has it own function like the following.
$this->db->where('tbl_field', 'value')
->get('your_table');
return $this->db->count_all_results();
Or use this.
return $this->db->count_all('your_table');
Where wont work on count_all condition.. you can use below method to find out total number of rows..
public function count_all() {
$this->db->select ( 'COUNT(*) AS `numrows`' );
$this->db->where ( array (
'type' => 'Original'
) );
$query = $this->db->get ( 'story_tbl' );
return $query->row ()->numrows;
}
Related
How do I structure the below query in Codeigniter. I can't seem to figure it out.
This is the function in my model:
public function update_daily_inventory(){
foreach ($this->parseInventoryHtml() as $item)
{
$_item = $this->db->query('SELECT COUNT(*) as `count` FROM product WHERE product_code = "'.$item['sku'].'"');
}
return $_item;
}
I want to use an array key as the where variable. Apologize if this is simple I am new to coding. This keeps returning 0.
$this->db->where('product_code', $item['sku']);
$this->db->select('COUNT(*) as `count`');
$_item = $this->db->get('product')->row()->count;
There is no need to set alias for count.here is simple query to count number of rows found in result.
$this->db->where('product_code', $item['sku']);
$this->db->select('*');
$_item = $this->db->get('product')->num_rows();
You can use below code to structure your query.
$this->db->select('*');
$this->db->where('product_code', $item['sku']);
$_item = $this->db->count_all_results('product');
Hope this helps.
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 am trying to write a CSV in Codeignter for a result set fetched from db.
I am already using the solution mentioned here. Reports in Codeigniter
But the issue is that it writes the whole table. Now I want to fetch and write specific records. So here is my model code.
$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
->from('hangtag_request')
->where($where)
->get()
->result_array();
return $result;
It gives me this error.
You must submit a valid result object
If I change the model code to this
return $query = $this->db->get('hangtag_request');
It works perfectly. Is there any way I can make my model code to return the results in the form of DB object? Coz it seems thats the form we need.
Thanks.
If you need result object then you shouldn't use the result_array()
$this->db->get();
The above statement runs the selection query and returns the result.It does return in form that can be used for presentation.
result_array()
This function returns the query result as an array, or an empty array when no result is produced.
Its good to use return $query = $this->db->get('hangtag_request'); for obtaining result object.
I got it solved.
Just had to remove result_array() so the correct code becomes
$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
->from('hangtag_request')
->where($where)
->get();
return $result;
I am trying to do the following query in Axon but can't make it work. Using normal query it works -
SELECT user_name, email_id FROM ors_email_user WHERE email_sent=false LIMIT 5;
In Axon, I try to do the following -
$users = new Axon('ors_email_user');
$users->load(array('email_sent=:email_sent', array(':email_sent' => false)), '', 3);
while(!$users->dry()) {
echo 'here';
}
It never goes inside the while loop. What is wrong with the query? Is there a way where I can see what query is actually being formed.
I believe the load function only allows 3 parameters to be passed, where the third is the columns that should be ordered.
You should use the find() or select() function. From the website:
find( [criteria],[order],[limit],[offset] );
select( fields,[criteria],[grouping],[order],[limit],[offset] );
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.