I'm have a live website that uses PHP and a mySQL database. I'm looking to use d3 to create a few visualizations. But I don't know how to alter the data as it is stored in the SQL database before directly encoding it in a JSON for d3. My PHP:
$activity_array = $array;
while ($activity = mysqli_fetch_assoc($get_all_activities)) {
// $user_name = find_user_by_id($activity['user_id']);
// echo $user_name['last_name'];
$activity_array[] = $activity;
}
echo json_encode($activity_array);
However, due to how my mysql database is set up, users are set up as numbers, so that what I get back in the JSON looks like:
[{"id":"93","date":"2020-05-01","user_id":"37","user_notes":"This user has blah blah."},...]
When I use this JSON to generate a graph in d3, I want it to show the actual user's name, not "37". In the PHP code above I commented out the query I have to get the user's name from their user_id, but then I have no idea how to get that into my JSON.
Thanks for the help!
You just need to modify the appropriate entry in the $activity array with the username retrieved by the second query:
$activity_array = $array;
while ($activity = mysqli_fetch_assoc($get_all_activities)) {
$user_name = find_user_by_id($activity['user_id']);
$activity['user_id'] = $user_name['last_name'];
$activity_array[] = $activity;
}
echo json_encode($activity_array);
It would however probably be easier to modify the $get_all_activities query to JOIN to the users table and fetch the username in that query directly.
The application I'm building requires some users to be linked to eachother.
There will be users and mentors. The database field "mentor_link" has a value. This value determines which users the mentor is linked to.
What I'm trying to do is search the database for the users with a certain "mentor_link" value and save their usernames as php variables to be used later.
Using json_encode() AND print_r simply displays something that looks like this:
[{"userName":"dave","0":"dave"},{"userName":"ely","0":"ely"},{"userName":"mentor1","0":"mentor1"}]
What I'm looking for is to target each username and save it as a variable. For example: $user1 = dave; $user2 = ely; etc etc.
All the other examples I've found only allow me to display it the same way as above.
My code is as follows:
$stmt = $user_home->runQuery("SELECT userName FROM tbl_users WHERE mentor_link=101");
$stmt->execute();
$result = $stmt->fetchAll();
echo json_encode($result);
The database is connected fine and I'm able to pull single values based on the users details but I can't figure out how to get info on other users and display it/save it as a variable.
Oh and I'm using PDO to connect to the database.
After this line:
$result = $stmt->fetchAll();
You can do anything with that data
print_r($result[0]);//user1
print_r($result[1]);//user2
or
$user1 = $result[0];
$user2 = $result[1];
then
echo $user1['userName'];//dave
echo $user2['userName'];//ely
I have been trying to get data from exploded values, but I am failing miserably and I am completely clueless despite all the researching I've been doing.
This is how the code looks like:
$array = explode(",", $hos['prop_owner']);
list($a) = $array;
$gu = $db->prepare("SELECT * FROM users WHERE user_id = :id");
$gu->execute(array(':id' => $a));
$dau = $gu->fetch();
echo $hos['prop_name']."<br><small>";
if(end($array)){
echo "<a href='/user/view/".$dau['user_id']."' style='color:#".$dau['user_colour']."'>".$dau['user_name']."</a></small><br>";
} else {
echo "<a href='/user/view/".$dau['user_id']."' style='color:#".$dau['user_colour']."'>".$dau['user_name']."</a>,";
}
Currently, the database field $hos['prop_owner'] contains the values "2,20" which are IDs of users (this field can potentially contain more IDs in the future). What I want to do is get all the user data from the exploded values, in this case 2 and 20, and then echo the information out in order as well.
Re-explanation:
I have a field in my database called prop_owner which is supposed to contain an unlimited number of user IDs, seperated by comma. Format: 1,2,3,4.
I want to take the value from this field, then somehow separate the user IDs and separately retrieve the usernames and echo them out.
Example result: Darren, Eva, Miles, Lisbeth
I hope I explained myself good enough to understand where I am trying to go with this.
Thanks in advance!
First of all the query will be like
SELECT * FROM users WHERE user_id in (2,20)
You need the data of both the users so the query will return all the data of all the ids that are being passed here..
You can directly pass here but you need to take care of security... or may be you can check how to pass values securely in such queries ...
I have a MySQL query that will be converted to JSON and used in Obj C for each user with a specific id. I believe that this is a MySQL puzzle, but there may be an answer in JSON. I don't ask many questions, so I'll try to make it concise.
Here is a screen shot of values for one user. If you notice, the field_id may vary because not all info is required, so the id field will vary from 3 to 8 values for a given user_id:
I have to make a query where the results GROUP BY column1 (user_id), but only WHERE column2 (field_id) has the following values field_id='18' and field='19', Then (THE BIG PROBLEM) I need to populate the results in one GROUP with both values of column3 (value) so I can get results in one JSON object.
I already know how to convert to JSON for use in iOS, but I can only get it to give me results as two objects.
My current query
$query = "SELECT * FROM table1 WHERE field_id='18' OR field_id='19' ORDER BY user_id ";
Current Result
[{"id":"5","user_id":"461","field_id":"18","value":"1_MX4zNjcxNzM5Mn4x...","access":"0"},
{"id":"6","user_id":"461","field_id":"19","value":"T1==cGFydG5lcl9pZD0...","access":"0"},
{"id":"11","user_id":"463","field_id":"18","value":"1_MX4zNjcxNzM...","access":"0"},
{"id":"12","user_id":"463","field_id":"19","value":"T1==cGFydG5lcl9...","access":"0"}]
I need the two JSON objects with matching user_id fields as one object with results that differentiate field='18' value from field='19' value. Something like:
[{"id":"5","user_id":"461","field_id":"18","value18":"1_MX4zNjcxNzM5Mn4x...","value19":"T1==cGFydG5lcl9pZD0...","access":"0"},
{"id":"11","user_id":"463","field_id":"18","value18":"1_MX4zNjcxNzM...","value19":"T1==cGFydG5lcl9...","access":"0"}]
OR
[{"id":"5","user_id":"461","field_id='18'":"1_MX4zNjcxNzM5Mn4x...","field='19'":"T1==cGFydG5lcl9pZD0...","access":"0"},
{"id":"11","user_id":"463","field_id='18'":"1_MX4zNjcxNzM...","field='19'":"T1==cGFydG5lcl9...","access":"0"}]
THANKS...
I'm not 100% sure if this will work. I cannot easily try the JSON output of it. But what if you run a query like this?
SELECT id, user_id,
GROUP_CONCAT(field_id,':',value) field_id,
access
FROM table1 WHERE field_id='18' OR field_id='19'
GROUP BY user_id
ORDER BY user_id
You probably want to to look at handling this in PHP when reading in the result set.
So use your current query of
SELECT * FROM table1 WHERE field_id='18' OR field_id='19' ORDER BY user_id
But build result data object like this:
$results = array();
$i = -1;
$current_user_id = '';
while ($row = [YOUR MySQL FETCH MECHANISM HERE]) {
// determine if this record represents a new user id in the result set
// if so, we need to set up the user object and start another entry in top level array
if($current_user_id != $row['user_id']) {
// you have moved to a new user in the result set
// increment your array counter and set current user id
$i++; // will set value to 0 on first iteration
$current_user_id = $row['user_id'];
// build new array entry
$results[$i] = new stdClass();
$results[$i]->user_id = $current_user_id;
$results[$i]->fields = array();
}
// build field object for insertion
$field = new stdClass();
$field->field_id = $row['field_id'];
$field->value = $row['value'];
$results[$i]->fields[] = $field;
}
On encoding $results this would give you a JSON structure like this:
[
{
"user_id":"461",
"fields": [
{
"field_id":"18",
"value":"foobar"
},
{
"field_id":"19",
"value":"abcxyz"
}
]
},
...
]
This data structure is going to be more readily usable by consuming app than some solution which requires exploding concatenated field id/value strings. Also note that I have not included id field anywhere in data structure, as it has no meaning in this context. If you truly needed that id, you could add it as another property in the field object since that is where there is a one-to-one relationship (not with user_id).
Based on your comment, if the field value is known and you need to access it via field_id index, you can slightly modify what I have shown above to build index-able field listing rather than a simply array of objects:
$results = array();
$i = -1;
$current_user_id = '';
while ($row = [YOUR MySQL FETCH MECHANISM HERE]) {
// determine if this record represents a new user id in the result set
// if so, we need to set up the user object and start another entry in top level array
if($current_user_id != $row['user_id']) {
// you have moved to a new user in the result set
// increment your array counter and set current user id
$i++; // will set value to 0 on first iteration
$current_user_id = $row['user_id'];
// build new array entry
$results[$i] = new stdClass();
$results[$i]->user_id = $current_user_id;
$results[$i]->fields = array();
}
// insert field value at field_id index position
$results[$i]->fields[$row['field_id']] = $row['value'];
}
This would give you a JSON representation like this.
[
{
"user_id":"461",
"fields": {
"18":"foobar",
"19":"abcxyz"
}
},
...
]
This would allow easy look-up by client based on field id. Of course you may want to consider similar for user_id. From the user object you could just access fields->18 (or similar based on on client language syntax).
Hey guys im quite new into programming and i would like some assistance...I store my sessions into a database table and each session row has a user_data array which contains the values email and logged_in.
I want to search that table for every session entry and print only the email from the user_data array (to server as a "who's online") but i get very confused with the multiple arrays.
$email = $this->session->userdata('email'); //That's how i read the single session now
I hope that this could will be useful for you.
i get the data from ci_session table and after that i ge the last activity you can get your
email the same way.
my model function where i get data from ci_session table
function get_alls()
{
$this->db->select('t1.*');
$this->db->from('ci_session AS t1');
$query = $this->db->get();
return $query;
}
inside my controller i called it.
$recs = $this->main_model->get_alls();
//echo unserialize($recs->row()->user_data); exit;
foreach($recs->result() AS $item)
{
if($item->last_activity+300<strtotime("now"))
{
$mhd = unserialize($item->user_data);
$this->ci_forms = array('loged'=> '0');
$this->crud_model->UpdateUid('users',$this->ci_forms,$mhd['userid']);
}
}