Reputation: 3
I have a MySQL database with a few 'heating profiles', each profile is in its own table and there is a seperate table to keep track of the names of each profile.
Because the my jQuery Mobile / Cordova HTML 5 page is loading dynamically based upon the number of profiles and their contents to display to the user I'm avoiding any hardcoding of names etc. Here is the PHP:
$con = mysqli_connect($server, $username, $password);
mysqli_select_db($con, $database);
$result = mysqli_query($con, "SELECT * FROM PROFILENAMES")
or die ("Query error: " . mysqli_error($con));
while($row = mysqli_fetch_assoc($result)) {
$string = $row['profileNames'];
$data[] = $string;
$string = str_replace(' ', '', $string);
$profileNames[] = $string;
}
foreach($profileNames as $secondstring){
$secondstring;
$secondresult = mysqli_query($con, "SELECT * FROM $secondstring ORDER BY startTime ASC")
or die ("Query error: " . mysqli_error($con));
while($secondrow = mysqli_fetch_assoc($secondresult)) {
$data[$secondstring] = $secondrow;
}
}
mysqli_close($con);
echo $_REQUEST['jsoncallback'] . '(' . json_encode($data) . ');';
In theory if there were more than one row in the profile "SELECT *" and a 'while' loop should append each to the array $data at key $secondstring. But no matter what I try only one row is passed back in the JSON, i've done echo's etc to make sure the data is being retrieved and it is, the $secondrow contains all rows, but after the first row is appended to the array it seems to stop.
The current output of this code is:
({"0":"Weekday","1":"Weekend","2":"Different","Weekday":{"setTemp":"26","startTime":"17:00:00","endTime":"19:00:00"},"Weekend":{"setTemp":"900","startTime":"12:00:00","endTime":"13:00:00"},"Different":{"setTemp":"666","startTime":"23:00:00","endTime":"02:00:00"}});
Is there any way I can force additional rows to append to that particular array key?
Using incrementing or unique key values for each row makes it much more difficult to parse this data in javascript on the otherside and insert it into a HTML element.
Any help would be appreciated! Sorry for the formatting, not sure what happened there.
Upvotes: 0
Views: 498
Reputation: 360792
Just build an array:
while($secondrow = mysqli_fetch_assoc($secondresult)) {
$data[$secondstring][] = $secondrow;
^^---- add this
}
Each new "secondrow" will get pushed onto that subarray.
Upvotes: 1