using php I make a query to mysql. Inside my query, some values are in seconds (just some of them), I'd like to convert them into mm:ss. So I need to move into my "array" query and I do not understand how to move inside my query result, attribute after attribute.
Here is my db :
In my php code, I make the query and then for each row I put it inside an array.
$data=array();
$q=mysqli_query($mysqli, "SELECT * FROM TEST3612 WHERE user_id=$queryid");
while ($row=mysqli_fetch_object($q)) {
//$data[]=gmdate("H:i:s", $row%86400);
$data[]=$row;
}
I'd like then to move into my array, and when I have the attribute CS for example, convert the value.
Here is what I tried :
foreach($data as $value) {
if ($value['CS']) {
$value['CS'] = gmdate("H:i:s", $value%86400);
}
}
I got an error. I don't know how to move into my array, a select a element of a object. I thought I did it good but apparently no.
Thank you for you help.
mysqli_fetch_object returns objects, not arrays. Also you used $value instead of $value->CS inside of gmdate. So you should iterate like this:
foreach($data as $value) {
if ($value->CS) {
$value->CS = gmdate("H:i:s", $value->CS % 86400);
}
}
Alternativly you can write this directly in your while loop, so you don't need the foreach loop. I would say this is the better solutions because you don't write unnecessary code and performance gets improved a little bit:
while ($row = mysqli_fetch_object($q)) {
if ($row->CS) {
$row->CS = gmdate("H:i:s", $row->CS % 86400);
}
$data[] = $row;
}
Related
I trying to test display a nested Json structure but the result is in wrong format.
php
while($row=$statement->fetch()){
$value=str_replace('"','',$row['item_option_value']);
$option[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$option;
}
echo json_encode($all);
mysql databas structure
Here is the result when i run the script.
I want the json structure to be the right side of the screenshot. Anyone knows what's wrong?
You would need to empty the $option arrays then like this:
$option = []; //or $option = array(); in PHP < 5.4
This is needed in order not to keep storing the data from the previous iteration.
So:
while($row=$statement->fetch()){
$value=str_replace('"','',$row['item_option_value']);
$option = [];
$option[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$option;
}
echo json_encode($all);
The problem is because of this line here,
$option[$row['item_option_name']]=explode(',',$value);
In each iteration of while() loop, you're appending the previously calculated $options array to $all. Instead, create an temporary array to hold intermediate result and append that to $all in each iteration, like this:
while($row=$statement->fetch()){
$opArray = array();
$value=str_replace('"','',$row['item_option_value']);
$opArray[$row['item_option_name']]=explode(',',$value);
$all[$row['oid']]=$opArray;
}
echo json_encode($all);
I am trying to retrieve user data from a SQL table where one of the columns is blob_medium. I run the SQL query properly and get the data in the php script.
if(mysqli_num_rows($result)){
while($row=mysqli_fetch_assoc($result)){
$result_array['user_data'][]=$row;
}
}
Now, to json_encode this data, I need to encode the data of user_pic column to base 64. For that I am trying this. But, it seems I am doing something wrong. Any kind of help would be appreciated.
foreach($result_array as $key){
foreach($key as $key2){
//print_r(base64_encode($key2['user_pic']).'<br/>'.'<br/>');
$key2['user_pic'] = base64_encode($key['user_pic']);
//print_r(($key['user_pic']).'<br/>'.'<br/>');
}
}
When I uncomment the print_r statements my data is printed in base64 format but the data of the assoc array is not changing.
That's because the array's $key and $keys in the for loop are copies. If you want them to modify the original you can either do it by specifying them to be references, not copies:
foreach($result_array['user_data'] as &$key){
$key['user_pic'] = base64_encode($key['user_pic']);
}
Or by explicit index into the original:
foreach($result_array['user_data'] as $index => $key){
$result_array['user_data'][$index] ['user_pic'] = base64_encode($key['user_pic']);
}
That's because you're changing the $key2 array. A temporary value created by your foreachloop. I myself would recommend using a for loop in this specific situation, because I was told to never use a loop within a loop if I can prevent it, and it makes things a lot easier to read:
for($i=0; $i < count($result_array['user_data']); $i++){
$encodedUserPic = base64_encode($result_array['user_data'][$i]['user_pic']);
$result_array['user_data'][$i]['user_pic'] = $encodedUserPic;
}
I just started learning PHP and I am having some difficulties with some of the coding.
Hopefully, someone could help me a little.
I'm using this:
if(!empty($_POST['yyy'])) {
foreach($_POST['yyy'] as $a1) {
echo " $a1";}}
The echo will write several results of $a1 depending on how many were selected in the form.
What I want is to save those results to some values so I can add them in MySQL.
Something like this:
if(!empty($_POST['yyy']))
{
foreach($_POST['yyy'] as $a1)
{
echo " $a1"; where $a1 will create a $result1,$result2,$result3(for each isset)
}
}
Then if I use:
echo "$result2";
it will give me the second result.
Not clear whether you are asking about this kind of result or not. But you can use an array to store each values inside the foreach loop.
var data=[];// define an array to access outside of if statement later..
if(!empty($_POST['yyy'])) {
foreach($_POST['yyy'] as $a1){
data[]=$a1;
//or can use array_push() method
array_push(data,$a1);
}
}
/*this will give the second result(because array indexing starts from 0. So to get third result
use data[2])*/
echo data[1];
Furthermore by echoing quoted variable will not give the value of that variable but gives a string literal.
echo "$result2" //output---> $result
I have a class that does the MySQL stuff for me.
I have 1 to n rows in a MySQL Table and want to query specific results.
To query a table, I can now use
$db->select('tablename', '*');
$res = $db->Result()
to get the results as an associative array.
Now if I want to loop through, I have to check if there is one or more results and then either display that one result or loop through the results.
This bloats up my code and I would like to find a way to combine both results.
At the moment, I am doing this stuff like so:
if(is_array($res[0]){
//we have more than one result
foreach($res as $something)
{
//do stuff here
}
}
else
{
//do the same stuff as above here but now with other variables since $something is only filled in the foreach loop
}
Now as said, I would love to combine those two and have only one piece of code to display the results (or work further with them)
Change the input data structure into the format the loop expects, then iterate through it in the loop:
if(!is_array($res[0]){
$res[0] = [$res[0]];
}
foreach($res as $something)
{
//do stuff here
}
I would suggest you to switch to some wide deveoped classes like PDO (http://php.net/manual/en/book.pdo.php) or simply add a check in your Result method that returns an empty array in cases there are no results
function Result() {
// stuff to fetch and fill $arr;
return (is_array($arr[0])) $arr : array();
}
I have a sequence of mysql query result resources stored in a array.
E.G array([0] => resource [1] => resource ...ect);
This code retrieves the first resource in the array:
$third_count = "0";
while ($user_result = mysql_fetch_array($user[$third_count])) {
print_r($user_result);
}
$third_count = $third_count +1;
I'm just stuck trying to find an if statement that'll loop though the array.
Something like: while ($third_count =< $second_count) is what I need, but it doesn't seem to work.
Where $second count is the number of elements in the array.
Thanks for pointers!
What you want to do is use a foreach loop to loop through that array of result resources. Counts will not matter then.
foreach($resourcearr as $res) {
while ($user_result = mysql_fetch_array($res)) {
print_r($user_result);
}
}