Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array question 1

Status
Not open for further replies.

Aeros

Programmer
Oct 7, 2002
166
US
Im still kind of new to php and Ive looked into solving this on my own...so I must post the question.

Im trying to post the results of a multiple select list to a new page. I realize that I will be passing an array and on the second page (ive tried tons of configurations) the result I get is just 'Array' instead of the actual value(s).
Thank in advance



list.htm
-----------------------------
<form action="list2.php" method="post">
<select multiple name="intColorID[]" size="5">
<option value="1">Red</option>
<option value="2">Green</option>
<option value="3">Blue</option>
<option value="4">Purple</option>
</select>
<input type="submit" name="submit" value="submit">
</form>

list2.php
-----------------------------
<?
$colors = $HTTP_POST_VARS['intColorID'];
echo $colors;
?>
 
hmmm..I never came across that function. That seems to work perfectly.

Thanks!
 
Now I have a second part to this question. What I am basically trying to do is to take the results from the array, loop through them and do an insert oneach of them. Right now it is only inserting the last record in the array:

$colors = $HTTP_POST_VARS['intColorID'];
$colorCount = count($colors);

while($colorCount > 0)
{
$insertSQL2 = sprintf("INSERT INTO ProductColor_JUNC (intProdID, intColorID) VALUES (last_insert_id(),%s)",
GetSQLValueString($colors[$colorCount], "int"));
$colorCount--;
}

Thanks!
 
Most php tutorials would have covered this, you might google for one.

I'm not really sure why your code isn't working. Here's the easiest way to do it in my opinion:
Code:
foreach ( $colors as $color ) {
    // now $color will iterate through each element of your array
}
 
Yea thats a way to do it...but its still not correcting what is wrong in the insert query which is where the problem lies.

Thanks though
 
Try printing your insert statements to see what's wrong with them.
 
Can you paste the export of the table structure?
So we can look at fieldtypes, etc?
 
intProdID int(11) nonull
intColorID int(11) nonull
 
I have to go now, but I'll check back on this thread later.. in approx. 3 hrs.

 
The answer to the failed SQL is fairly evident:
I see you populating the SQL query, but never executing it. Your query is probably executed outside the foreach() loop and thus only the last color is added.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top