0

I have a JSON decoded array in php with foreach that includes a select option in a form every time I select an option from this drop down it returns the last value in the list. The option is just the selector not the value I'm trying to save. I'm trying to save the flatnumber,building,address1,address2,town,county variables into hidden inputs. How do I go about this? Any help really appreciated!!

    $api_url = 'http://rest.api?postcode=' . urlencode($_GET['postcode']);
    $api_json = file_get_contents($api_url);
    $api_array = json_decode($api_json, true);
    $address = $api_array['addresses'];
    foreach($address as $addresses){
    echo '<option>'.$addresses['address'].'</option>';
        $flatnumber = $addresses['flatNumber'];
        $address1 = $addresses['building'];
        $address2 = $addresses['address1'];
        $town = $addresses['town'];
        $county = $addresses['county'];
    }

Edited by cambraydesign

3
Contributors
5
Replies
47
Views
1 Year
Discussion Span
Last Post by cambraydesign
0

I really am lost with regard to your snippet. You're echoing an option on each iteration of the loop, which makes me think that this php code is jumbled up in the middle of some HTML markup. Also the $flatnumber (and all other vars following it) are being overwritten at each iteration, so be the end of the loop, they'll contain the data from the last item in the loop.

0

Here's a more full example, this should give you a better understanding what I am trying to achieve

$api_url = 'http://rest.api?postcode=' . urlencode($_GET['postcode']);
$api_json = file_get_contents($api_url);
$api_array = json_decode($api_json, true);
print_r($api_array);
$address = $api_array['addresses'];

?>
        <div class="row">
            <div id="area-chart-spline" style="width: 100%; height: 300px; display: none;">
            </div>
            <div class="col-md-6">
                <div class="panel panel-blue" style="background:#FFF;">
                    <div class="panel-heading">Address</div>
                    <div class="panel-body pan">
                        <form action='' method='post' border='0'>                            
                        <div class="form-body pal">
                            <div class="row">
                                <div class="col-md-2">Select Address</div>                                
                                <div class="col-md-10">
                                    <div class="form-group">
                                        <select>
                                            <?php foreach($address as $addresses){
                                                    echo '<option>'.$addresses['address'].'</option>';
                                                    $flatnumber = $addresses['flatNumber'];
                                                    $address1 = $addresses['building'];
                                                    $address2 = $addresses['address1'];
                                                    $town = $addresses['town'];
                                                    $county = $addresses['county'];
                                                    $postcode = $addresses['postCode'];
                                                } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <input type='hidden' name='address1' value='<?php echo $flatnumber.' '.$address1; ?>' />
                            <input type='hidden' name='address2' value='<?php echo $address2; ?>' />
                            <input type='hidden' name='town' value='<?php echo $town; ?>' />
                            <input type='hidden' name='county' value='<?php echo $county; ?>' />
                            <input type='hidden' name='postcode' value='<?php echo $postcode; ?>' />
0

Yes well this is my point. You've got the variables going through the foreach loop (being overwritten many times), ending up with the data for the last "record". Then after exiting the loop you're echoing out the data for the last record in the hidden inputs.

So confused as to what you're trying to achieve with this.

0

Hello, as diafol wrote (twice) the way you are doing it keeps only the last record data. @see foreach at php.net . There are many ways to do it client side without all these hidden fields (e.g. data attributes in the option) but there is many simpler (and more secure) ways server side. E.g. (I will use your own code)

$addresses = $api_array["addresses"];
$_SESSION["addresses"] = $addresses;
for($i = 0; $i < count($addresses); $i++)
{
    echo "<option value='".$i."'>".$addresses[$i]["address"]."</option>";
}

That way when you will receive the value of this select from post you will just get the array that has the same index from the session stored variable

0

Ok so your code brings up the full address but I need to get the values from the individual variables saved.

So $address[$i]["address"] is the full address, but I don't want to save this value I want this to select the relevant values from the json_decode

$address[$i]['flatNumber'];
$address[$i]['building'];
$address[$i]['address1'];
$address[$i]['town'];
$address[$i]['county'];
$address[$i]['postCode'];

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.