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"];
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 (and that extra seventh array entry) out of the array so
that it's clean going into the db?
So, I insert that array into the db like so:
// db connectivity stuff
INSERT INTO items (color) VALUES ( '$color')";
// there are other variables, but I'm trying to keep this as clean as
possible.
Then, I have this on the page that pulls the data from the db:
...
$num_results = mysql_num_rows($results);
for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($results);
$color[] = $row["color"];
echo "<form method='post' action='someotherpage.php'>";
echo "<select name='color'>";
$colorcount = count($color);
echo "<!-- colorcount: $colorcount -->";
for($x = 0; $x < $colorcount - 1; $x++)
{
echo "<option
value='$color[$x]'>$color[$x]</option>\n";
}
echo "</select></form>";
}
However, I'm putting that bad array in the database, and I still get
extra option entries, because I have no idea how many extras to remove.
So, I feel like I should be doing something BEFORE I do the db insert.
Other than that, the db insert and select work just fine (say, if I set
the values of that array manually in the php code).
Any suggestions would be very helpful!
Thanks,
Steven
--
Steven Jarvis
Web Developer
Arkansas Democrat-Gazette
Northwest Edition
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php