Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Why not simplify this and not do these queries in a loop, but rather use a join with first query to return all results in a single query? That said your problem lies in the fact you a overwriting each entry in loop with same index value of $secondstring. –  Mike Brant Mar 31 at 14:37
    
I haven't used SQL in a long time, I'll have a look into that when I get a chance though, thanks! –  ClassicStudent Mar 31 at 15:19

1 Answer 1

up vote 0 down vote accepted

Just build an array:

    while($secondrow = mysqli_fetch_assoc($secondresult)) {
            $data[$secondstring][] = $secondrow;
                                ^^---- add this
    }

Each new "secondrow" will get pushed onto that subarray.

share|improve this answer
    
I knew it was going to be something this simple! Played around with it for a few hours but would have taken alot longer on my own to get that. Thank You! –  ClassicStudent Mar 31 at 15:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.