On Thursday 06 June 2002 12:03, Steven Jarvis wrote:
> I'm trying to store an array in a mysql db. I'm creating the array
> variable through an html form and passing it to the page where I store
> it in the db. I want to create the array with a form, store it in the
> db, then display it in another form with a <select> statement. However,
> I'm getting extra array elements, including another array in the final
> element. I can't figure out how to deal with this extraneous data.
>
> Page 1:
> The insertion form looks like this:
> <form method="post" action="arraytest.php">
> COLORS:<br />
> <input type="text" name="color[]" size="30" /><br />
> <input type="text" name="color[]" size="30" /><br />
> <input type="text" name="color[]" size="30" /><br />
> <input type="text" name="color[]" size="30" /><br />
> <input type="text" name="color[]" size="30" /><br />
> <input type="text" name="color[]" size="30" /><br />
> <input type="submit" value="do it!" />
> </form>
>
> That's five input fields.
>
> Page 2:
> I want the user to be able to use none, some, or all of the fields, so I
> don't know how many elements will be in the array.
>
> $color[] = $_POST["color"];

That should be:

  $color = $_POST["color"];

Actually from your results below it seems like you've enabled register_globals 
and it's not necessary to explicit (re)assign $color.

> I'm testing the incoming array with:
>
> while ($element = each($color))
>       {
>               echo $element["key"];
>               echo ": ";
>               echo $element["value"];
>               echo "<br>\n";
>       }
>
> And I get results like this printed to the browser (I always get seven
> entries, including the last array entry):
>
> 0: green
> 1: black
> 2: blue
> 3:
> 4:
> 5:
> 6: Array
>
> First question: is there something I can do at this point to cut the
> empty entries 

foreach ($color as $key => $val) {
  if ($val) {
    $tmp[] = $val;
  }
}
$color = $tmp;    

That will remove empty values from $color.

> (and that extra seventh array entry) out of the array so
> that it's clean going into the db?

See above about assigning $color from $_POST.

-- 
Jason Wong -> Gremlins Associates -> www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *

/*
You're always thinking you're gonna be the one that makes 'em act different.
                -- Woody Allen, "Manhattan"
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to