Jump to content

Errors Passing an array of ID's and Values


otonix

Recommended Posts

Okay heres the problem.

I have a form which displays items that sit within a packet. (In this case its called Wireless Package 1). Each item has its own specific value, which can be any string, hench why there is an 'input type= text' in the value column in the table.

Each of these values is stored in a table, with the primary key 'piid', (show as a hidden field within 3rd column).

 

The problem is when im posting the array 'values[]' I cant distinguish between what value needs to be referenced againist a piid, how do i obtain these individual ID's and Values in a loop so they can turned into a MySql Strings????? :confused:

 

I tried using an explode function but got no where, any help is very much apprieciated!

 

This is the code for the form displayed.

 

<table border="1" cellpadding="5" cellspacing="5">

<tr>

                            <th>Item ID</th>

<th>Item</th>

<th>Value</th>

                            <th>Remove</th>

</tr>

<form action="edit_packet.php" method="post">

<?php foreach($packages as $packet):?>

 

<tr>

                                        <td>

<?php echo $packet['piid'] ;?>

</td>

<td>

<?php echo $packet['desc'] ;?>

</td>

                                               

<td>

                                                <input type="hidden" name="values[]" value="<?php echo $packet['piid'];?>,," />

                                                <input type="text" name="values[]" value="<?php echo $packet['value']; ?>" />

                                                <input type="hidden" name="values[]" value="///" />

 

                                                                                                </td>           

                                                                               

                                        <td>

<form action="delete.php" method="post">

<input type="hidden" name="pid" value="<?php echo $packet['piid'];?>"/>

<input type="submit" value="Delete Packet">

 

</td>

</tr>

<?php endforeach; ?>

                       

</table>

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

                        </form>

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

okay basically Ive managed to be able to post it as a sting that would look like this.

 

21,,hello///20,,goodbye///

 

How can i run a loop to

- get the id (int after ///)

-string (after the ,,)

 

and then turn these into variables that as the loop cycles, inserts the string where ID matches string?

 

 

Link to comment
Share on other sites

Rather than screw with the data to try to include the ID, change the way you define the input fields.

 

<input type="text" name="values[<?php echo $packet['piid']; ?>]" value="<?php echo $packet['value']; ?>" />

 

Here we put the piid value as the index to the array of values.  When you process the POST values from the form we can do something like this:

 

foreach ($_POST[values] as $piid => $newValue) {
  // $piid is the database index we need to update
  // $newValue is the value entered by the user 
}

 

By the way, in your original code, you are creating a FORM tag after the input fields inside your loop. This is going to create all kinds of problems. The FORM tag has to come before any of the INPUT fields.  Its not clear if you want a single FORM for the entire page, or separate FORMs for each package. But you need to review the way you are creating them and do it appropriately.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.