0

I get this example:

<?php 
$shop = array( array( Title => "rose", 
                  Price => 1.25,
                  Number => 15 
                ),
           array( Title => "daisy", 
                  Price => 0.75,
                  Number => 25,
                ),
           array( Title => "orchid", 
                  Price => 1.15,
                  Number => 7 
                )
         );
?>

But how can I make one of these with the following structure:

$result2 = mysqli_query($con, "SELECT r.id AS id, CONCAT(g.fname,' ',g.lname) AS name,
                                    r.arrival AS arrival, r.departure AS departure 
                                    FROM reservations r JOIN guests g ON r.guest = g.id
                                    WHERE r.unit = ".$unit_id."");

while ($row2 = mysqli_fetch_assoc($result2))
    {
        $reservations = array(  array (id => $row['id'],
                                      name => $row['name'],
                                      arrival => $row['arrival'],
                                      departure => $row['departure'],
                                    ));

    }

2 Answers 2

3

Are you looking for this? This will give $reservations with a structure like the one you show.

$result2 = mysqli_query($con, "SELECT r.id AS id, CONCAT(g.fname,' ',g.lname) AS name,
                                    r.arrival AS arrival, r.departure AS departure 
                                    FROM reservations r JOIN guests g ON r.guest = g.id
                                    WHERE r.unit = ".$unit_id."");

$reservations = array();  // makes sure the array exists in case result set is empty
while ($row = mysqli_fetch_assoc($result2))
    {
        $reservations[] =   array (id => $row['id'],
                                      name => $row['name'],
                                      arrival => $row['arrival'],
                                      departure => $row['departure'],
                                    );

    }

Access as follows:

foreach ($reservations as $row){
    echo $row["name"]." arrival ". $row["arrival"]; 
    echo "<br />"; 
}

You can also access:

for ($row = 0; $row<count($reservations); $row++){
    echo $reservations[$row]["name"]." arrival ". $reservations[$row]["arrival"]; 
    echo "<br />"; 
}
Sign up to request clarification or add additional context in comments.

4 Comments

The query will return x number of rows. So I want to account for that. To me, that just looks like the array will be re created each iteration and will only contain the last row from the query when finished.
It will create $reservations with x number of elements if that is how many rows are returned from the query.
okay. How does one access a row? For instance, in my first example one could access the data like so: for ($row = 0; $row < 3; $row++) { echo $shop[$row]["Title"]." costs ".$shop[$row]["Price"]." and you get ".$shop[$row]["Number"]; echo "<br />"; }
Okay, I'll give it a try later and see if all works well. Looks as if it will. Thanks a lot.
1

What about this:

$result2 = mysqli_query($con, "SELECT r.id AS id, CONCAT(g.fname,' ',g.lname) AS name,
                               r.arrival AS arrival, r.departure AS departure 
                               FROM reservations r JOIN guests g ON r.guest = g.id
                               WHERE r.unit = ".$unit_id."");
while($row = mysqli_fetch_assoc($result2) $reservations[] =   $row;

this instruction:

$reservations[] = $row;

means that $reservations is an array and we will append this array with $row on the next key ($reservations[ next key ] = $row)

3 Comments

How would you access the data?
for instance: $reservations[0]['name']
Thanks. answer goes to first-responder but I might end up using yours out of simplicity anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.