Using array for "selected" value?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shylock
Forum Newbie
Posts: 20
Joined: Mon Mar 24, 2003 11:50 pm

Using array for "selected" value?

Post 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&#1111;]" size="1" multiple id="prod_id&#1111;]">
      <?php
do &#123;  
?>
      <option value="<?php echo $row_rsProds&#1111;'products_id']?>"
	  <?php 
	  if (!(strcmp($row_rsProds&#1111;'products_id'], $prod_set))) &#123;
	  echo "SELECTED";
	  &#125; 
	  ?>>
	  <?php echo $row_rsProds&#1111;'products_name']?></option>
      <?php
&#125; while ($row_rsProds = mysql_fetch_assoc($rsProds));
  $rows = mysql_num_rows($rsProds);
  if($rows > 0) &#123;
      mysql_data_seek($rsProds, 0);
	  $row_rsProds = mysql_fetch_assoc($rsProds);
  &#125;
?>
    </select>
I'm wondering if I'm not using strcmp the right way, or if it's even the smartest choice here. :?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
shylock
Forum Newbie
Posts: 20
Joined: Mon Mar 24, 2003 11:50 pm

Post by shylock »

Nothing is tagged "selected" using this method. I am trying a loop foreach now.
shylock
Forum Newbie
Posts: 20
Joined: Mon Mar 24, 2003 11:50 pm

Post 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&#1111;]" size="1" multiple id="prod_id&#1111;]">
      <?php
do &#123;  
?>
      <?php 
if (in_array($row_rsProds&#1111;'products_id'], $prod_set)) &#123;
     echo "<option value="$row_rsProds&#1111;'products_id']" selected>"$row_rsProds&#1111;'products_name']"</option>";
&#125; else &#123;  echo "<option value="$row_rsProds&#1111;'products_id']">"$row_rsProds&#1111;'products_name']"</option>";
&#125;
      ?>
      <?php
&#125; while ($row_rsProds = mysql_fetch_assoc($rsProds));
  $rows = mysql_num_rows($rsProds);
  if($rows > 0) &#123;
      mysql_data_seek($rsProds, 0);
	  $row_rsProds = mysql_fetch_assoc($rsProds);
  &#125;
?>
    </select></td>
Could be a great solution for others with the same needs.
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post 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 &#123;  
  if (in_array($row_rsProds&#1111;'products_id'], $prod_set)) &#123; 
     echo "equal: row_rsProds&#1111;'products_id'] is -$row_rsProds&#1111;'products_id']- and prod_set is -$prod_set-<br>";
  &#125; else &#123;  
     echo "not equal: row_rsProds&#1111;'products_id'] is -$row_rsProds&#1111;'products_id']- and prod_set is -$prod_set-<br>";
  &#125; 

&#125; while ($row_rsProds = mysql_fetch_assoc($rsProds));

so you see wheter it is a syntax error or if something else is mixed up
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
shylock
Forum Newbie
Posts: 20
Joined: Mon Mar 24, 2003 11:50 pm

Post 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.
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post 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.
shylock
Forum Newbie
Posts: 20
Joined: Mon Mar 24, 2003 11:50 pm

Post 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.
Post Reply