Want to store multiple id's (dynamically) in session array of Codeignitor 2.x - codeigniter-2

Need to store multiple id's dynamically in codeignitor session array. Array push seems to be not working. Am able to add only one id. Any help?
Code is below:
$internships_array['internships'][] = $internship_result['id'];
if (empty($this->session->userdata('internships')) || sizeof($this->session->userdata('internships')) < 1) {
$this->session->set_userdata($internships_array);
} else {
array_push($this->session->userdata('internships'), $internship_result['id']);
}

Instead of using array push in session get all the data in session to an array then update the array after that set session this will work
$internships_array['internships'][] = $internship_result['id'];
$insert_to_session=array();
if (!empty($this->session->userdata('internships')) || sizeof($this->session->userdata('internships')) >0) {
$insert_to_session [‘internships’]= $this->session->userdata('internships');
}
array_push($insert_to_session [‘internships’], $internship_result['id']);
$this->session->set_userdata($internships_array);

Related

Vuetify v-data-table change a row color for a few seconds

We've just moved over from bootstrap to Vuetify, but i'm struggling with something.
We have some updates sent (over signalR) that update a list of jobs, i'd like to be able to target a job that has been changed and change the row color for that particular job for a few seconds so the operator can see its changed.
Has anyone any pointers on how we can do this on a Vuetify v-data-table
Thanks
I ran into the same problem. This solution is a bit crude and a bit too late, but may help someone else.
In this example I change the colour of the row permanently until the page reloads. The problem with a temporary highlight is that if the table is sorted there is no way to put the row in the visible part of the table - v-data-table will put it where it belongs in the sort, even if it's out of the view.
Collect the list of IDs on initial load.
Store the list inside data of the component.
Use a dynamic :class attribute to highlight rows if the ID is not in the list (added or edited rows)
Solution in detail
1. Use TR in the items template to add a conditional class.
<template slot="items" slot-scope="props">
<tr :class="newRecordClass(props.item.email, 'success')">
<td class="text-xs-center" >{{ props.item.email }}</td>
:class="newRecordClass(props.item.email, 'success')" will call custom method newRecordClass with the email as an ID of the row.
2. Add an additional array to store IDs in your data to store
data: {
hydrated: false,
originalEmails: [], <--- ID = email in my case
3. Populate the list of IDs on initial data load
update(data) {
data.hydrated = true; // data loaded flag
let dataCombined = Object.assign(this.data, data); // copy response data into the instance
if (dataCombined.originalEmails.length == 0 ) {
// collect all emails on the first load
dataCombined.originalEmails = dataCombined.listDeviceUsers.items.map( item => item.email)
}
return dataCombined;
}
Now the instance data.originalEmails has the list of IDs loaded initially. Any new additions won't be there.
4. Add a method to check if the ID is in the list
newRecordClass(email, cssClass) {
// Returns a class name for rows that were added after the initial load of the table
if (email == "" || this.data.originalEmails.length==0) return "" // initial loading of the table - no data yet
if (this.data.originalEmails.indexOf(email) < 0 ) return cssClass
}
:class="newRecordClass(..." binds class attribute on TR to newRecordClass method and is being called every time the table is updated. A better way of doing the check would be via a computed property (https://v2.vuejs.org/v2/guide/computed.html#Computed-Properties). Vue would only call it when the underlying data changed - a method is called every time regardless.
Removing the highlight
You can modify newRecordClass method to update the list of IDs with new IDs after a delay to change the colour to normal.
#bakersoft - Did you find a solution? I suspect there is an easier way to skin this cat.

Vaadin-combo-box sorting

I'm trying to sort an array of objects in alphabetic order. However, somehow the vaadin-combo-box Polymer element only shows the first item in the array after a sort (even though there are more items in the array)
I'm getting my data from Firebase (called settingsData) so I have to use a splices observer to see if data is being added or remove:
Observer
observers: [
'sortData(settingsData.splices)'
],
sortData: function(newD, oldD) {
if(newD) {
var tmp = this.settingsData.sort(function(a,b) {return (a.name.toUpperCase() > b.name.toUpperCase()) ? 1 : ((b.name.toUpperCase() > a.name.toUpperCase()) ? -1 : 0);} );
//tmp is an array in the correct order
this.set("filteredItems", tmp);
}
}
HTML
<vaadin-combo-box id="picker" label="[[label]]" items="[[filteredItems]]" item-label-path="name" item-value-path="name" value="{{valueText}}" on-value-changed="valueChanged"></vaadin-combo-box>
Somehow the observer for array mutations is not fired after the first time. This means that the view for the vaadin-combo-box is not rendered correctly, which seems a little bit odd?
Clearing the array every time I'm trying to set the new data did the trick.
this.set("filteredItems", []);
this.set("filteredItems", tmp);
Either this.settingsData.sort really returns only 1 item or Firebase's Reactive data breaks it. Unable to reproduce it with inline array data.

Laravel 5 session array update

I am having trouble while updating session array value in laravel 5. Here is my function,
public function postCartItemAdd()
{
$id = Request::input('id');
Session::push('items', $id);
dd(Session::all());
}
Instead of pushing a new id into the array it just replaces the existing value leaving single item. Am I doing something wrong?
The problem is the session is saved as a flash data. So, you need to save the session whenever you push the data.
$request->session()->push('user.items', 'item1');
$request->session()->push('user.items', 'item2');
$request->session()->save();
or try this
$items = Session::pull('items');
$items[] = $id;
Session::push('items', $items);
umm i think you used it wrong,
see the DOC
it says
Session::push('user.teams', 'developers');
user is the array and we gonna put a value developers to that array with teams key
so then you need to use it in your case as,
Session::push('items.id', $id);
OR if you need to maintain items as an array with default keys like 0,1,2,3... to put the ids, then items should be an array
so there should be a something like,
Session::put('items', []);
then you can use Session::push('items', $id);
if you need to push ids in to same array as you tried.

how to use keys with couchbase views

i am trying to filter views results.
i am using this map:
function (doc,meta){
emit(arraydate([doc.date],doc.id);
}
reduce by _count
result with group level 2 is:
[2014,1]
undefined
1819
[2014,2]
undefined
35
now i want to filter only 2014,1
i am trying key=2014,1 , key="2014,1" , key="[2014,1]" and its always fail and i get 0 rows . i am using the ui to set the key search.
i want to find a way to use the key for filfering and the keys range filtering
thanks for helping.
Your function should be
function(doc,meta) {
if(meta.type == "json") {
if(doc.date) {
emit(dateToArray(doc.date));
}
}
}
This will produce keys like [2014,2,3,0,0,0], so to return only data for the first month of 2014 you'd use:
startKey = [2014,1]
endKey = [2014,2]
You don't need to emit the doc id as they id is automatically emitted and is accessible from the viewRow as id.

Checking if specific user is online through php session variable

I'm currently trying to display all online users on my webpage using the php session variables. To do this, whenever a user logs in or out, a column in a database gets set to "online" or "offline".. However this doesn't entirely work since the database doesn't get updated when the user closes their browser (and therefor destroys the session).
So is there another way of checking if a certain sessionid is set??
Currently I am setting the session like this:
session_start();
$_SESSION['username']="Example Username";
To check from the current users page if there is a session variable set we can simply use:
if(isset($_SESSION['username']))
{
//username is set
}
But if we need to check if a specific user is online, how do we get for instance an array of all the session variables that have been set? e.g.
//Get all $_SESSION['username'] vars
//loop through array and check for specific $_SESSION
for($i=0; ... )
{
if( $_SESSION['username'][$i] == "User 1" )
{
//do something
}
}
From all that I've read so far, there doesn't seem to be a way to get an array of all sessions on your page..
So is there a better way of doing it, e.g. how do facebook, twitter, etc handle this stuff?
Thanks for your help!
One solution is to store a timestamp in your database in addition to the online/offline flag that records the last time a user accessed any of your website resources. Then you might just consider them offline after 15 minutes or some other constant value.
The other solution is to play with the callbacks here http://php.net/manual/en/function.session-set-save-handler.php where I think you can handle the gc callback and set the offline flag in your database.
If you search on that page for class SessionDB or go here it looks like somebody has implemented some version of this already in a custom class!
you can use a simple update query
for example you have a table users and in that you have a column called status(online/offline)
on your login.php use
<?php
//your user verification code
if(variable that holds your sql query){
$user_status=('UPDATE user SET status= online WHERE email="'your user email selector'")
}
then on the logout do a similar script just change the online value to offline
You could try this:
foreach ($_SESSION as $sessionKey => $sessionValue)
{
if( $sessionKey == 'username' ) && ( $sessionValue == 'User 1' )
{
//do something
}
}

Resources