0

Can anyone point me in the right direction with the below

I have this HTML in a FROM (simplified for this example):

=============== Start HTML ===================

<select name="Postage[]" id="Unique-ID">
    <option value="1">Postage Option 1</option>
    <option value="2">Postage Option 2</option>
    <option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />

<select name="Postage[]" id="Unique-ID">
    <option value="1">Postage Option 1</option>
    <option value="2">Postage Option 2</option>
    <option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />

<select name="Postage[]" id="Unique-ID">
    <option value="1">Postage Option 1</option>
    <option value="2">Postage Option 2</option>
    <option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />

================== End HTML =====================

I am collecting this from the form and setting it to an array ($arrPostageOptions) with the below code:

=============== Start PHP ===================

if (isset($_POST['Postage'])) {
    if (is_array($_POST['Postage'])) {
        $n = count($_POST['Postage']);
        for ($i = 0; $i < $n; ++$i) {
            // check to only collect completed entries
            if( !empty($_POST['Postage'][$i]) && !empty($_POST['PostagePrice'][$i]) ){
                // assign to array
                $arrPostageOptions[$i] = array( "Postage" => $_POST['Postage'][$i], "PostagePrice" => $_POST['PostagePrice'][$i], );
            }
        }
    }
}  

=============== End PHP ===================

This results in the following array

============ array result ===============

Array(
    [0] => Array
        (
            [Postage] => 1
            [PostagePrice] => 12
        )
    [1] => Array
        (
            [Postage] => 2
            [PostagePrice] => 24
        )
    [2] => Array
        (
            [Postage] => 3
            [PostagePrice] => 48
        )
)

============ end array result ===============

The answer (I think) I want is for each array is :

1 & 12

2 & 24

3 & 48

(that the array numbers match the postage numbers is just coincidence, I only want ‘Postage’ and ‘PostagePrice’)

I am trying the following:

=========== start of what I am trying ==========

foreach ($arrPostageOptions as $id) {
    while ($id) {
        $Postage = $id["Postage"];
        $PostagePrice = $id["PostagePrice"];
        // echo $Postage.' '.$PostagePrice.'<br>';
        unset($id);
        if (!$listing_obj->addPostageOptions($ListingID, $PostageOptionID, $PostagePrice, $db)) {
           $err_text .= 'An error occurred adding Postage Option'.$PostageOptionID;
        }
    }   
}

====== end of what I am trying ==============

Unfortunately this is not working as I expect (but is the closest I have gotten) . I have also tried multiple foreach loops, but again I don’t get what im after (unsettling the $id is stopping the loop from continuing for ever, but is also giving me PHP notices of Undefined variable $id)

I’m sure I know how to do this, but seem to have forgotten and have possible over complicated it as these arrays are still confusing the s**t out of me.

Any advice would be greatly welcomed!

3
  • I'm still unsure of what you want the outcome to be Commented Jul 20, 2013 at 18:23
  • I don't get the sense of while ($id) and unset($id) Commented Jul 20, 2013 at 18:24
  • I don't think it should be name="Postage[]"... maybe name="Postage". Commented Jul 20, 2013 at 18:29

4 Answers 4

1

Why not simply:

foreach ($arrPostageOptions as $postageOption) {
    $postage = $postageOption["Postage"];
    $postagePrice = $postageOption["PostagePrice"];
    echo $postage.' '.$postagePrice.'<br>';
    ...
}

Sorry for renaming, but $id was too confusing for me.

Sign up to request clarification or add additional context in comments.

4 Comments

if that doesn't work for you try once var_dump($postageOption) inside the loop...
works perfect this time...... thank you, i think i was having a mind block moment, as im sure this was what i first tried and the reslt was "P"... hmmm.... thank you!
you're welcome :-) Btw it's not a good idea to have more than 1 element in a document with the same id.
yes, i will remove the elements with the same id, i just added it like that for the example
1

I'm not entirely sure I understood what you want to do but it would seem to me that you want to iterate the array like this, but I'm not sure.

foreach ($arrPostageOptions as $id=>$array) {
  foreach($array as $key=>$val){
    // do your stuff here using where you can also reference $id
  }
}

Comments

1

there are a couple of things I would like to say is that the isset function is a bit pointless here and it is best used along with a submit button, not for text fields and other inputs.

Also, you are declaring your array inside the loop so accessing it anywhere outside will give an undefined error.

I just used your form and updated the php script slightly to give the output you desire. Please have a look.

Your form

<form action="yourscript.php" method="POST">

    <select name="Postage[]" id="Unique-ID">
    <option value="1">Postage Option 1</option>
    <option value="2">Postage Option 2</option>
    <option value="3">Postage Option 3</option>
    </select>
    <input name="PostagePrice[]" id="Price-Unique-ID" value="" />

    <select name="Postage[]" id="Unique-ID">
        <option value="1">Postage Option 1</option>
        <option value="2">Postage Option 2</option>
        <option value="3">Postage Option 3</option>
    </select>
    <input name="PostagePrice[]" id="Price-Unique-ID" value="" />

    <select name="Postage[]" id="Unique-ID">
        <option value="1">Postage Option 1</option>
        <option value="2">Postage Option 2</option>
        <option value="3">Postage Option 3</option>
    </select>
    <input name="PostagePrice[]" id="Price-Unique-ID" value="" />

    <input type="submit" value="submit" name="submit"/>

    </form>

I added a submit button and a the method and action in the header just to make it a bit more clear.

And here is the script I used, pretty similar to yours

<?php

    $arrPostageOptions = array();

    if(isset($_POST['submit']))
    {
        $postage = $_POST['Postage'];
        $postagePrice = $_POST['PostagePrice'];

        $array_size = count($postage);

        for($i = 0; $i < $array_size; $i++)
        {
            if(!empty($postage[$i]) && !empty($postagePrice[$i]))
            {
                $arrPostageOptions[$i] = array("Postage"=>$postage[$i], 
                            "PostagePrice"=>$postagePrice[$i]);  
            }
        }
        print_r($arrPostageOptions);
    }

?>

I check if the submit button is set/clicked and enter the loop. I define the array variable outside so it can be used anywhere in the program.

If you see the print_r result, you will see the array in the format you desire.

I hope I answered a couple of your questions.

At the same time, its better to always put your POST data into variables before using them so you can check them and control them better than directly using $_POST['name']. This will be of more use when you work with databases and other related things.

Hope this helps.

Thanks, Shawn.

Comments

0

The while within the foreach is redundant. foreach will only continue as long as there is a new entry to become $id anyhow.

The loop should be fine if you remove the while. If you want to test whether the data is valid, test on the array keys themselves like by putting this in the loop

if(!$id["Postage"] || !$id["PostagePrice"]){
  continue;  
  }

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.