Page 1 of 1
Using array for "selected" value?
Posted: Thu Apr 03, 2003 11:31 am
by shylock
I am trying to pull in a dynamic list from MySQL (which works fine) and then use a stored array to hilight certain values as "selected". (does not work as written) It does not give me an error either, so it is obviously not syntax, just "ignored".
Here is the full clip from my form, $prod_set is my array:
Code: Select all
<select name="prod_idї]" size="1" multiple id="prod_idї]">
<?php
do {
?>
<option value="<?php echo $row_rsProdsї'products_id']?>"
<?php
if (!(strcmp($row_rsProdsї'products_id'], $prod_set))) {
echo "SELECTED";
}
?>>
<?php echo $row_rsProdsї'products_name']?></option>
<?php
} while ($row_rsProds = mysql_fetch_assoc($rsProds));
$rows = mysql_num_rows($rsProds);
if($rows > 0) {
mysql_data_seek($rsProds, 0);
$row_rsProds = mysql_fetch_assoc($rsProds);
}
?>
</select>
I'm wondering if I'm not using strcmp the right way, or if it's even the smartest choice here.

Posted: Thu Apr 03, 2003 5:05 pm
by volka
the whole selection does not work or is there no element selected?
if you open the generated document in your browser's source view is there an <option> having the property SELECTED?
Posted: Fri Apr 04, 2003 6:22 pm
by shylock
Nothing is tagged "selected" using this method. I am trying a loop foreach now.
Posted: Sun Apr 06, 2003 6:59 pm
by shylock
Found some code that looks like it should work. but, doesn't.
Perhaps there is a syntax error? I'll snip the whole table cell.
Code: Select all
<td><select name="prod_idї]" size="1" multiple id="prod_idї]">
<?php
do {
?>
<?php
if (in_array($row_rsProdsї'products_id'], $prod_set)) {
echo "<option value="$row_rsProdsї'products_id']" selected>"$row_rsProdsї'products_name']"</option>";
} else { echo "<option value="$row_rsProdsї'products_id']">"$row_rsProdsї'products_name']"</option>";
}
?>
<?php
} while ($row_rsProds = mysql_fetch_assoc($rsProds));
$rows = mysql_num_rows($rsProds);
if($rows > 0) {
mysql_data_seek($rsProds, 0);
$row_rsProds = mysql_fetch_assoc($rsProds);
}
?>
</select></td>
Could be a great solution for others with the same needs.
Posted: Mon Apr 07, 2003 3:01 am
by Tubbietoeter
Correct me if i'm wrong, but if concentation don't you have to use dots?
echo "<option value="$row_rsProds['products_id']" selected>"$row_rsProds['products_name']"</option>";
would be
echo "<option value="$row_rsProds['products_id']" selected>".$row_rsProds['products_name']."</option>";
take a look at the sourcecode of the web page, do you see all elements in the option tags or is one missing?
for debugging do this:
Code: Select all
do {
if (in_array($row_rsProdsї'products_id'], $prod_set)) {
echo "equal: row_rsProdsї'products_id'] is -$row_rsProdsї'products_id']- and prod_set is -$prod_set-<br>";
} else {
echo "not equal: row_rsProdsї'products_id'] is -$row_rsProdsї'products_id']- and prod_set is -$prod_set-<br>";
}
} while ($row_rsProds = mysql_fetch_assoc($rsProds));
so you see wheter it is a syntax error or if something else is mixed up
Posted: Mon Apr 07, 2003 3:18 am
by twigletmac
Try:
Code: Select all
<td>
<select name="prod_id[]" size="1" multiple id="prod_id[]">
<?php
// you can simplify the code by using a while loop instead of
// a do...while loop
while ($row_rsProds = mysql_fetch_assoc($rsProds)) {
// for debugging it may be useful to do a couple of print_r()s:
echo '<pre>$row_rsProds: ';
print_r($row_rsProds);
echo '</pre>';
echo '<pre>$prod_set: ';
print_r($prod_set);
echo '</pre>';
if (in_array($row_rsProds['products_id'], $prod_set)) {
// you need to concenate the code a bit better and remember
// you can't have double quotes within a double quoted string -
// you could escape them or, as below, use single quotes around
// the string instead.
echo '<option value="'.$row_rsProds['products_id'].'" selected="selected">'. $row_rsProds['products_name'] .'</option>';
} else {
echo '<option value="'.$row_rsProds['products_id'].'">'. $row_rsProds['products_name'] .'</option>';
}
}
// What are you trying to do with the rows below as they could
// probably be safely deleted.
/*
$rows = mysql_num_rows($rsProds);
if($rows > 0) {
mysql_data_seek($rsProds, 0);
$row_rsProds = mysql_fetch_assoc($rsProds);
}
*/
?>
</select>
</td>
Mac
Posted: Mon Apr 07, 2003 11:52 pm
by shylock
Excellent. Thanks.
Although the code still does not work, I think that I can figure it out from here. Plus, I have some reading to do on concat!
It seems as though something in the above is breaking my html because I don't get any php errors, I just get blank page. no html.
I have been commenting/uncommenting by line to see where the error is and haven't been able to track it down. It works without the code in the select tags. I'll have to stare at it for an hour and perhaps the flaw will reveal itself.
Posted: Tue Apr 08, 2003 12:46 am
by Tubbietoeter
check whether you closed all tags. use rights mouse click to view the html source on the php site. try to give the select thing another name, maybe the brackets are the problem.
if you view the sourcecode of the site in your browser, copy the html to a file and have it checked by w3c.
Posted: Wed Apr 09, 2003 12:17 pm
by shylock
Thanks again for the reply.
Thing is, there is no HTML to look at. The page is blank when this code is in the select statement.
So, it's almost as if there is a tag that throws an errant TD tag and then the table disappears. But, I took it out of the table entirely and it still goes away.