Forum Moderators: coopster

Message Too Old, No Replies

Simple array question

         

ajs83

2:30 am on Nov 3, 2005 (gmt 0)

10+ Year Member



I'm using the following code to grab the value of a dropdown box called "category"

if($_POST){
foreach ( $category as $value ) {
echo "$value<br>";
}

How do I also grab the option id associated with the row?


<option id="$id" value="$cid">$name</option>

ajs83

5:56 am on Nov 3, 2005 (gmt 0)

10+ Year Member



?

NomikOS

7:09 am on Nov 3, 2005 (gmt 0)

10+ Year Member



You mean this?


<select name="">
<?php
foreach ($category as $id => $value) :
if ($categorySelected == $value)
echo "<option value=\"$id\" selected>$value</option>";
else
echo "<option value=\"$id\">$value</option>";
endforeach;
?>
</select>

if so $category is gathered in this way (using your own sql queries of course):


<?php
$categorySelected = 'a selected category';
$sql->query("SELECT id,value FROM aTable");
while ($sql->nextRow())
{
$category[$sql->row['id']] = $sql->row['value'];
}

?>

please tell me if this answer your question.--

ajs83

7:23 am on Nov 3, 2005 (gmt 0)

10+ Year Member



I'm posting the form values to the script above, what I need it to do is also pick up and echo the option id as well.

Currently Using

<select size="1" name="category[]">
while (blah){
<option id="$id" value="$cid">$name</option>
}
</select>

Which posts to to

if($_POST){
foreach ( $category as $value ) {
echo "$value<br>";
}

It works fine for the option value, but I also need the option id.

NomikOS

8:15 am on Nov 3, 2005 (gmt 0)

10+ Year Member



Let me clear this:
you mean pass 2 values ($id and $value) throught a select tag?

if so the a very fast (and dirty) solution is something like this:


<select size="1" name="category">
while (blah) {
<option value="$cid=$id">$name</option>
}
</select>


<?php
if ($_POST)
{
$var = explode('=', $_POST['category']);
echo $var[0]; // value option
echo $var[1]; // id option
?>

what do you think?

ajs83

8:26 am on Nov 3, 2005 (gmt 0)

10+ Year Member



Yes, I do want to pass both values, but i need to do so in the form of an array since there are over 100 different versions of that select dropdown box.

All contain the same category[] name, it's just I don't know how to post two values from the array.

chrisjoha

8:40 am on Nov 3, 2005 (gmt 0)

10+ Year Member



That's not possible. Consider using something like this:


<select size="1" name="category[]">
while (blah){
<option id="$id" value="$cid::$id">$name</option>
}
</select>

Which posts to to


if($_POST){
foreach ($_POST['category'] as $value ) {
list($cid, $id) = explode('::', $value);
print 'CID: ' . $cid . ', ID: ' . $id . '<br />';
}

ajs83

8:48 am on Nov 3, 2005 (gmt 0)

10+ Year Member



Got it, Thanks to both of you!

NomikOS

7:50 am on Nov 4, 2005 (gmt 0)

10+ Year Member



well done
regards.-