I have some user preferences I am keeping in an array in MySQL. This is how I get the current array:
$array = DB::queryFirstField("SELECT dashboard_array FROM compel_dashboard
WHERE user_id = %i", $user_id);
Which gives me:
"s:19:"dashboard-reccomend";"
Now if I want to add a new string ($dashboard_item contains a string like dashboard-progress) to this array I was thinking:
$array[] = serialize($dashboard_item);
$query = DB::update('compel_dashboard', array(
'dashboard_array' => $array
), "user_id=%s", $user_id);
When I make this call though it doesnt update. The array stays the same as my first call, nothing gets added. If I just update that dashboard_array field without trying to add to the array it replaces the value fine so I know its something I am doing with adding to the array.
This is what I did to get it to work. I had to unserialize the array before adding to it.
$dashboard = DB::queryFirstField("SELECT dashboard_array FROM compel_dashboard
WHERE user_id = %i", $user_id);
$array[] = unserialize($dashboard);
array_push($array, $dashboard_item);
$new_dash = serialize($array);
$query = DB::update('compel_dashboard', array(
'dashboard_array' => $new_dash
), "user_id=%s", $user_id);
Related
I am not sure how to write the following code.
$rowID = $_POST['rowID'];
if ($listing = $Listings->getData($rowID)) {
$jsonArray = array(
'listing_number' => $listing['listing_number'],
);
exit(json_encode($jsonArray));
}
When I do it like that, the response is Undefined Index: listing_number.
However, If I write it like this,
$rowID = $_POST['rowID'];
if ($listing = $Listings->getData($rowID)) {
$jsonArray = array(
'listing_number' => $listing[0],
);
exit(json_encode($jsonArray));
}
The response is
{"listing_number":{"id":"24","client_id":"1","address":"","address_2":"","city":"","state":"","zip":"","price":"","listing_number":"asdasdasdasd","remarks":"","link":"","status":"","bd":"","ba":"","lot_sz":"","sq_ft":"","yr":"","type":"","thumb":""}}
Which lets me know my SQL is and PHP is correct, I just don't know how to access $listing['listing_number] correctly.
Any help would be appreciated.
as GrumpCrouton said in the comment, your query is returning an array of results. So if you want to access a value in the first result, you first need to access this result using it's index : $listing[0]->listing_number.
$rowID = $_POST['rowID'];
if ($listing = $Listings->getData($rowID)) {
$jsonArray = array(
'listing_number' => $listing[0]->listing_number,
);
exit(json_encode($jsonArray));
}
P.S. You can convert object to array using a simple cast ( $result = (array) $result ), but it is not a must in your case. Casting your object to array will allow you to acces it's data using result['key'] rather than result->key.
I want to merge two of my columns (yanlis_cevaplar, cevap_icerik) into an array and this code here gives me only one column in array when I print it (yanlis_cevaplar).
How do I fix it?
$cevaplar = "SELECT yanlis_cevaplar FROM cevaplar";
$cevap_sonuc = $conn->query($cevaplar) or die(mysqli_error($conn));
$cevap1 = array(); //create empty array
while ($row = $cevap_sonuc->fetch_array()) { //loop to get all results
$cevap1[] = $row; //grab everything and store inside array
}
$cevaplar2 = "SELECT cevap_icerik FROM cevaplar";
$cevap_sonuc2 = $conn->query($cevaplar) or die(mysqli_error($conn));
$cevap2 = array(); //create empty array
while ($row = $cevap_sonuc2->fetch_array()) { //loop to get all results
$cevap2[] = $row; //grab everything and store inside array
}
$tumcevaplar = array_merge($cevap1, $cevap2);
print_r($tumcevaplar);
Instead of making multiple queries, you can just fetch all the columns you want in one single query:
$cevaplar = "SELECT yanlis_cevaplar, cevap_icerik FROM cevaplar";
$cevap_sonuc = $conn->query($cevaplar) or die(mysqli_error($conn));
// Now you can fetch all the rows straight away without any loop.
// The MYSQLI_ASSOC will return each row as an associative array
$result = $cevap_sonuc->fetch_all(MYSQLI_ASSOC);
print_r($result);
This will result in something like this:
Array
(
[0] => Array
(
[yanlis_cevaplar] => some value
[cevap_icerik] => some value
)
[1] => Array
(
[yanlis_cevaplar] => some value
[cevap_icerik] => some value
)
... and so on ..
)
If this isn't what you want, then you need to show us an example.
I also recommend that you go through some basic SQL tutorials. How SELECT works is SQL 101. Here's one of many guides: https://www.tutorialspoint.com/mysql/mysql-select-query.htm
I'm trying to populate the array for the variable $example_data. I have an array of Mass' and dates that I want to populate this array with. However I do not know how to create a new array entry for each $mass[$x] and $date[$x] that I want to store. I've tried putting a foreach loop inside the $example_data = array( but it didnt work.
This is what I want it to do, but doesn't work:
$example_data = array(
foreach($exer->results() as $ex){
$mass = $ex->Mass;
$date = $ex->Date;
array($date,$mass),
);
This is what I've tried but not sure how to complete it:
$userID = $user->data()->id;
$sql = "SELECT * FROM userdetails WHERE UserID = ".$userID."";
$details = DB::getinstance()->query($sql);
$x = 1;
foreach($details->results() as $detail){
/** getting data from each record from field Mass and storing it in $mass array **/
$mass[$x] = $detail->Mass;
$date[$x] = $detail->Date;
$x++;
}
$x = 1;
$example_data = array(
array($date[$x],$mass[$x]),
/** I want it to create a new array entry for each $mass[] **/
);
It depends on the type of database you're using...
For example if you're using MySQL, it's mysqli_fetch_assoc()
You're trying to fetch a result set into an associative array but the result set itself is an associative array
try...
$sql = "SELECT * FROM userdetails WHERE UserID = ".$userID."";
$details = DB::getinstance()->query($sql);
$example_data = array(
array("Mass"),
array("Date")
);
while($row = $details->fetch_assoc()) {
array_push($example_data['Mass'], $row['name_of_mass_column_in_db']);
array_push($example_data['Date'], $row['name_of_date_column_in_db']);
}
}
Basically, you can just fetch an associative array instead of populating an associative array with an associative array...
I have a database in MySQL and I'm using this query to select certain rows from it using PHP:
$q = "SELECT Number, Body
FROM boxes
WHERE Number BETWEEN '1' AND '4' ORDER BY Number ASC";
Then calling the query and initiating arrays:
$r = $mysqli->query($q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$array = array();
$content = array();
Then attempting to sort the results into an associative array where the 'number' is the key and the 'body' is the value.
while ($row = mysqli_fetch_assoc($r)) {
$array = array(
$content[$row['Number']] = $row['Body']
);
This works fine except it will not store the first value. This is the result of print_r($content);, missing the first row.
Array ( [2] => This is entry two [3] => This is entry three [4] => This is entry four )
I have tried running the SQL query within PHPMyAdmin and it returns all four rows as I would expect.
Does anyone have any ideas what I'm doing wrong?
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
You are getting first returned row by this line. You have to remove it and it will work properly.
mysqli_fetch_array and mysqli_fetch_assoc returning NEXT row on every call.
Hi in the code below I want to add an extra value for the associative array. For each queryresult in wich elements ["Nietaanwezig"] and ["Aanwezig_diner"] both are 0 I want to add the element ["Nietingevuld"] and set it's value to 1, otherwise i want to add the element ["Nietingevuld"] and set it's value to 0. Albeit I have tried a lot of options, I don't seem to ge a good solution.
// numerically indexed array of places
$namen = [];
$queryresult = [];
// TODO: search database for places matching $_GET["geo"]
$search = $_GET["zoekopdracht"];
if ($search = "diner")
{
$namen = query ("SELECT * FROM gasten WHERE Typegast = 1");
foreach ($namen as $naam)
{
$queryresult [] = [
"Voornaam" => $naam["Voornaam"],
"Achternaam" => $naam["Achternaam"],
"Nietaanwezig" => $naam["Nietaanwezig"],
"Aanwezig_diner" => $naam["Aanwezig_Diner"],
];
}
Don't do it all in a single stage then. Build the new child array, modify it as necessary, THEN insert it into the parent array:
$temp = array('Voornaam' => $naam['Voornaam'], etc....);
if (whatever you want to check) {
$temp['something'] = 'else';
}
$queryresult[] = $temp;