0

I have multiple adresses in a database, for each row 1 departure 1 arrival.

I would like to add these in a "form/select"

I can display all of these with :

echo $arraydep[0].$arrayarr[0];

but

$adresses = array($arraydep[0], $arrayarr[0]);

or

$adresses = array_merge($arraydep[0], $arrayarr[0]);

or

$adresses[] = array($arraydep[0], $arrayarr[0]);

does not display all in my form, only the departure and the arrival for only 1 row

It would really nice if you could help me :)

Here is my complete code :

<?php
include "./cfg/db.php";

$sql = 'SELECT * FROM commandes WHERE phone="555-1234"';
$list = mysqli_query($base, $sql) or die("Erreur SQL !" . $sql . "<br />" . mysqli_error());
mysqli_query($base, $sql) or die("Erreur SQL !" . $sql . "<br />" . mysqli_error());

while ($data = mysqli_fetch_array($list))
    {
    $arraydep = array(
        $data[depadresse]
    );
    $arrayarr = array(
        $data[arradresse]
    );
    echo $arraydep[0] . $arrayarr[0]; // echo all adresses from database for this phone number (for testing)
    $adresses = array(
        $arraydep[0],
        $arrayarr[0]
    );
    }

?>
    <form action="test2.php" method="post">
    <select name="test2">
        <?php

foreach($adresses as $adresse)
    {
?>
        <option value="<?php
    echo $adresses . "<br />"; ?>">
        <?php
    echo $adresse . "<br />"; // only echo 1 departure and 1 arrival for 1 row in the drowdown menu
    }

?>
        </option>
    </select>
    <input type="submit" value="Valider" />
</form>

Regards.

2
  • reference your array elements using quotation marks, e.g. $data['depadresse'] Commented Jan 10, 2019 at 17:35
  • mysqli_error() requires a db connection here. Commented Jan 10, 2019 at 21:09

3 Answers 3

3

You are only inserting the first row in the array. Try this.

$i = 0;
    while ($data = mysqli_fetch_array($list))
        {
        $arraydep = array(
            $data[depadresse]
        );
        $arrayarr = array(
            $data[arradresse]
        );
        echo $arraydep[$i] . $arrayarr[$i]; // echo all adresses from database for this phone number (for testing)
        $adresses = array(
            $arraydep[$i],
            $arrayarr[$i]
        );
        $i++
        }
Sign up to request clarification or add additional context in comments.

Comments

0

define your array at outside loop otherwise that does not work on next statement.

look usages at below of my code

$arraydep = array();
$arrayarr = array();
$adresses = array();
while ($data = mysqli_fetch_array($list))
{
    $arraydep[] =  $data['depadresse'];
    $arrayarr[] =  $data['arradresse'];
    $adresses[] = array( $arraydep[0], $arrayarr[0]);
}

var_dump($adresses);

echo $adresses[0][0] . ' ' . $adresses[0][1];

echo $adresses[1];

foreach ($oneOfDep as $arraydep){
    echo $oneOfDep . ' ';
}

Comments

0

You need to append each set of arrive/depart to the array each time around the loop.

$adresses = array();
while ($data = mysqli_fetch_array($list,MYSQLI_ASSOC))
{
    $adresses[] = array(
        'dep' => $data['depadresse'],
        'arr' => $data['arradresse']
    );
}

If you use MYSQLI_ASSOC to retrieve results, you'll have the results keyed by the field names. I don't know what your fields are called, but they need to go inside quotes when accessing $data.

It's also good practice to initialise arrays before using them.

You should also avoid SELECT * usage and instead always list the fields you are interested in using. This tells other developers your intentions.

You should also consider using mysqli_fetch_all($list, MYSQLI_ASSOC) and then you won't need to use the above loop.

To output results just use the below. Although I'm not sure exactly how you want to output the result:

foreach($adresses as $adresse) 
{
    echo "<option value='{$adresse['dep']}'>{$adresse['dep']}</option>";
    echo "<option value='{$adresse['arr']}'>{$adresse['arr']}</option>";

}

Comments

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.