CodeIgniter session shows different output in different functions - codeigniter

In first function of my controller, I am fetching random records from mysql table using CI active record
$query = $this->db->query("SELECT DISTINCT * FROM questions WHERE `level` = '1' ORDER BY RAND() limit 0,5");
$result = $query->result_array();
and saving result in session as
// saving questions id in session
for($i = 0; $i < count($result); $i++)
{
$session['questionsId'][] = $result[$i]['qId'];
}
$this->session->set_userdata($session);
and if print session variable it shows output like:
$qIds_o = $this->session->userdata('questionsId');
var_debug($qIds_o);
Array
(
[0] => 5
[1] => 9
[2] => 3
[3] => 6
[4] => 11
)
but if I retrieve same session in another function of same controller it shows different result
$qIds = $this->session->userdata('questionsId');
var_debug($qIds);
Array
(
[0] => 2
[1] => 8
[2] => 6
[3] => 3
[4] => 5
)
and if I remove ORDER BY RAND() from mysql query like:
$this->db->query("SELECT DISTINCT * FROM questions WHERE `level` = '1' limit 0,5");
it shows same session array in both functions. Very strange.
Please guide what is going wrong....
Here is my controller script:
public function set_value(){
$query = $this->db->query("SELECT DISTINCT * FROM questions WHERE `level` = '1' ORDER BY RAND() limit 0,5");
$result = $query->result_array();
// saving questions id in session
for($i = 0; $i < count($result); $i++)
{
$session['questionsId'][] = $result[$i]['qId'];
}
$this->session->set_userdata($session);
$qIds_o = $this->session->userdata('questionsId');
var_debug($qIds_o);
}
public function get_value(){
$qIds = $this->session->userdata('questionsId');
var_debug($qIds);
}
I called set_value() on page load while once the page loaded I call get_value() using AJAX post which simply hits my_controller/get_value/ and response back to browser.

Without looking at your controller (let's call it my_controller in detail, I think what might be happening is:
(1) You call the first function, my_controller/set_value where set_value sets the session variable and you echo the result.
(2) You then call the second function, show_value that simply echos out the session variable.
What you might be doing in set_value is:
1) echo out the current session variable
2) call the query and re-set the session variable
If this is the case, then when you go to show_value (2nd function) you are looking at the recently re-set value instead of the prior value that you echoed out in the first function.

I have two questions about this sentence:
but if I retrieve same session in another function of same controller it shows different result
Is this on a new page load?
If so, is the query run again?
I'm going to assume the answer to both of these questions is yes, since you said removing RAND() gives you the same results.
You are using a combination of RAND() and LIMIT in your query, meaning you want only five rows in a random order. That means that each time the query is run (and your session data is set), it is very likely that the results will be different.
I don't know exactly what you're doing with these IDs and what sort of data set you need, so this may not be 100% perfect for your solution, but if you only need to set this session data once, you should check if it exists before running the query.
if ($this->session->userdata('questionsId') === FALSE)
{
// Run your query and set your session data here.
// Note that in CI 3.0, Session::userdata()
// will return NULL if empty, not a boolean.
}

Related

Eloquent not updating first item in collection

I don't understand why this function won't update the first app and change its lock state to 0 and rest to 1. It should update all apps with pending status.
public function updateLockState( $stu_id )
{
$apps = Application::where('stu_id', $stu_id)->get();
$acceptedApps = $apps->whereIn('status', [ 'ACCEPTED_W_SCHOLARSHIP', 'ACCEPTED_WO_SCHOLARSHIP', 'ACCEPTED_BYSTUDENT', 'ACCEPTED_CONDITIONALLY', 'UNDER_REVIEW', 'REGISTERED' ]);
if ( $acceptedApps->count() == 0 )
{
// This has two apps showing in descending order by rank.
$pending_apps = $apps->where('status', 'PENDING')->sortByDesc('rank');
foreach ($pending_apps as $key => $value)
{
if ( $key == 0 )
{
$value->update(['locked' => 0]);
}
else
{
$value->update(['locked' => 1]);
}
}
}
dd();
}
I have even tried to use DB:table() function for updating apps with id in where clause but it gives same result. This function is executed on the first line of the index function i.e its the first function to run on the page that interacts with apps. It ends with a dd() or exit() which stops anything else to run.
EDIT:
When I manually update rank values in db, I run this script and it should update locks but it is not. It should make lock = 0 for first app and lock = 1 for all other app and since apps are ordered by ranks desc it, app with 18 rank should be on top hence its lock = 0 after update:
sortByDesc() sorts in descending order BUT it keeps the keys in the same order in the new collection that it returns.
So even if its desc order the first item in the collection $key may not always be 0.
Use:
$pending_apps = $apps->where('status', 'PENDING')->sortByDesc('rank')->values()->all();
Which will give you keys starting with 0.

Updating multiple tables in codeigniter

I have a function in codeigniter that i want to use to update two tables. This is the code
if($loss_making_trade_amount > 5 && $loss_making_trade_amount < 20){
$user_data = array(
'trading_balance' => $trading_balance_float - 0.50
);
$data = array(
'trade_consequence' => '0.50',
'loss_in_amounts_cron_status' => 'seen'
);
$where = "id='$rid'";
$where_trading_balance = "email='$email'";
$this->db->where($where);
$this->db->set($data);
$this->db->update('mailbox_ke_01', $data);
//update users table at this level
$this->db->where($where_trading_balance);
$this->db->set($user_data);
$this->db->update('users', $user_data);
}
Will i be able to update the tables in the way i have done or will $this be pointing to the first table when updating the second table?.
Your code looks fine, once a query ran - you don't have to be worry about because the Query Builder resets itself after every query if it is finished - or dies in case of a DB Error.
You can take a closer look here
The documentation also clearly suggests that the query runs if the update function gets called.
$this->db->update() generates an update string and runs the query based on the data you supply.
For more information read the documentation here
The only thing i would change are your where clauses:
instead of
$where = "id='$rid'";
$this->db->where($where);
you should use the where function of the Querybuilder properly
$this->db->where("id", $rid);
The same applies for your $where_trading_balance

Vtiger select query

I'm copying a vtiger query in a similar way but there is one change that the query given first having only one output so there is kept 0 in 2nd argument,
but in my customized query there are multiple outputs so what should I kept instead of 0
both are given as below:
original query
$is_recurring_event_query = $adb->pquery('SELECT recurring_group_id from vtiger_activity where activityid=?',array($id));
$is_recurring_event = $adb->query_result($is_recurring_event_query,0,'recurring_group_id');
copying it to use at different way
$is_recurring_event_activity_query = $adb->pquery('SELECT activityid from vtiger_activity where recurring_group_id='.$is_recurring_event);
$is_recurring_event_activity = $adb->query_result ($is_recurring_event_activity_query,0,'activityid');
You have to put variable and have to use for loop for your query to execute and get multiple values.
Suppose your query is like this
$result = $adb->pquery ('SELECT * from vtiger_activity where id='.$recordId);
$noofrow = $adb->num_rows($result );
for($i=0; $i<$noofrow ; $i++) {
$Data['activityid']=$adb->query_result($result,$i,'activityid');
$Data['activityname']=$adb->query_result($result,$i,'activityname');
}
Here in $Data you will get an array of the values.

Batch API, inviting friends

I'm trying to invite all fans of a page to a facebook event using the batch API. It's the first time I'm using this API, and at this time I have nothing to test this part of code... can someone tell me how to test without using a real page, with real fans... or tell me if it looks correct ?
//Getting all the likers of the page
$result = $facebookObj->api(array(
'method' => 'fql.query',
'query' => 'select uid,name from user where uid in ( select uid from page_fan where uid in (select uid2 from friend where uid1 = me()) and page_id = '.$fbPage.')'
));
//If liker are more than 50 we use batch request
if($numLikers >50){
// split array into several part of 50 users accounts
$splitLikers = array_chunk($result, 50);
// count how many arrays are generated by the array_chunk
$countSplit = count($splitLikers);
//Start a loop through the numbers of groups of 50 users (or less if last group contains less than 50 users
for($a=0; $a<$countSplit; $a++){
//Second loop to go through the 50 likers in one group
for($b=0; $b<count($splitLikers[$a]); $b++){
// construct an array containing the whole group
$queries[$a] = array('method' => 'POST', 'relative_url' => $event_fbID . "/invited/" . $splitLikers[$a][$b]['uid']);
}
//Send POST batch request with the array above
$ret_val = $facebookObj->api('/?batch='.json_encode($queries[$a]), 'POST');
}
}else{
foreach ($result as $value) {
$ret_val = $facebookObj->api($event_fbID . "/invited/" . $value['uid'],'POST');
if($ret_val) {
// Success
$numInvited++;
}
}
}
Your code seems ok, but dont forget you may need to add the access token to the batch request
like:
$params['access_token']=[USER_ACCESS_TOKEN];
$ret_val = $facebookObj->api('/?batch='.json_encode($queries[$a]), 'POST', $params);
And for testing you may want to create a new page with a couple of friends added and test it.
Also remember you need to handle the response, the batch request will response with an array with the same length that the batch array, and each object may be a diferent code, true if it was send.
Also you can try your FQL code in the Graph Explorer (clic here)
Seems ok, but it shows a extrange result.

Codeigniter Pagination: Run the Query Twice?

I'm using codeigniter and the pagination class. This is such a basic question, but I need to make sure I'm not missing something. In order to get the config items necessary to paginate results getting them from a MySQL database it's basically necessary to run the query twice is that right?
In other words, you have to run the query to determine the total number of records before you can paginate. So I'm doing it like:
Do this query to get number of results
$this->db->where('something', $something);
$query = $this->db->get('the_table_name');
$num_rows = $query->num_rows();
Then I'll have to do it again to get the results with the limit and offset. Something like:
$this->db->where('something', $something);
$this->db->limit($limit, $offset);
$query = $this->db->get('the_table_name');
if($query->num_rows()){
foreach($query->result_array() as $row){
## get the results here
}
}
I just wonder if I'm actually doing this right in that the query always needs to be run twice? The queries I'm using are much more complex than what is shown above.
Unfortunately, in order to paginate you must know how many elements you are breaking up into pages.
You could always cache the result for the total number of elements if it is too computationally expensive.
Yeah, you have to run two queries, but $this->db->count_all('table_name'); is one & line much cleaner.
Pagination requires reading a record set twice:
Once to read the whole set so that it can count the total number records
Then to read a window of records to display
Here's an example I used for a project. The 'banner' table has a list of banners, which I want to show on a paginated screen:
Using a public class property to store the total records (public $total_records)
Using a private function to build the query (that is common for both activities). The parameter ($isCount) we pass to this function reduces the amount of data the query generate, because for the row count we only need one field but when we read the data window we need all required fields.
The get_list() function first calls the database to find the total and stores it in $total_records and then reads a data window to return to the caller.
Remember we cannot access $total_records without first calling the get_list() method !
class Banner_model extends CI_Model {
public $total_records; //holds total records for get_list()
public function get_list($count = 10, $start = 0) {
$this->build_query();
$query = $this->db->get();
$result = $query->result();
$this->total_records = count($result); //store the count
$this->build_query();
$this->db->limit($count, $start);
$query = $this->db->get();
$result = $query->result();
return $result;
}
private function build_query($isCount = FALSE) {
$this->db->select('*, b.id as banner_id, b.status as banner_status');
if ($isCount) {
$this->db->select('b.id');
}
$this->db->from('banner b');
$this->db->join('company c', 'c.id = b.company_id');
$this->db->order_by("b.id", "desc"); //latest ones first
}
And now from the controller we call:
$data['banner_list'] = $this->banner_model->get_list();
$config['total_rows'] = $this->banner_model->total_records;
Things get complicated when you start using JOINs, like in my example where you want to show banners from a particular company! You may read my blog post on this issue further:
http://www.azmeer.info/pagination-hitting-the-database-twise/

Resources