Page 1 of 1

Get values from array, select it ona multiple select list

Posted: Wed Aug 27, 2003 9:01 pm
by parkin
Hi...
Can anyone help? :)

I got an array with some names...
Then I got a select list which has all the names..
I'd like to have the names in the array selected "highlighted" on the list..

So far this is what I have done.. I can display the list.. No prob with tht.. but.. nothing seems to be selected..

Could anyone tell me what I am doing wrong?

Thanks alot :mrgreen:

Code: Select all

<?php

// The list...
print "<select name=form[prod][] size=12 multiple>";
MuloptionsSearch ($prod, $_SESSION['an_array']); // prints the list
print "</select>";


// The function..
// print selected options from the search function; normal projects are listed
function MuloptionsSearch($prod, $search)
{
	foreach($prod as $a_row) // get all the prod_names
	 {
		if($a_row[prod_family]!="special") //for normal projects only
		{
			print "<option value="$a_row[prod_id]"";
			
			foreach($search as $rows) // "highlight" the names
			{
				if ($rows == $a_row[prod_id])
				{ print "SELECTED"; }
			}

		 	print ">$a_row[prod_name]"."</option>\n";
		}
	}
}
?>
any help is really appreciated! :wink:
thnx.. :D

Posted: Wed Aug 27, 2003 11:37 pm
by JAM

Code: Select all

if ($rows == $a_row[prod_id]) 
// to...
           if ($rows == $a_row['prod_id']) // adding single quotes
And, you could use something like this:

Code: Select all

// assign selected "SELECTED" if the row = a_row[prod_id], else make it blank.
 $selected = ($rows == $a_row['prod_id'] ? ' SELECTED' : '');
 if ($a_row['prod_family'] != "special") { 
   // insert the "SELECTED". If its blank, it just wont matter.
   echo '<option '.$selected.'value="'.$a_row['prod_id'].'">';
 }
Hope you understand what I meant here...

Posted: Thu Aug 28, 2003 12:43 am
by parkin
hi Jam..

All this php stuff is still kinda new.. :)

hmm.. why do we need to put the single quotes in:

Code: Select all

<?php
if ($rows == $a_row['prod_id'])
?>
Workin on understanding the other set of codes.. ;)

Thanks :mrgreen:

Posted: Thu Aug 28, 2003 1:58 am
by parkin
ooHHH..

i managed to get it! I understand the second set of codes..

But I still don't get why do we need the single quotes? :?:

THANX ALOT JAM :lol:

Posted: Thu Aug 28, 2003 8:50 am
by JAM
I personally cant explain single quotes as it's how I learned it. (That sounded odd...)
You should be able to use double quotes also. But, for example, if you are using a integer (number) value

Code: Select all

$foo = 14;
...you dont use double/single quotes. If you do, it's suddenly a string of text (small, but still).

Posted: Thu Aug 28, 2003 8:37 pm
by d3_realm
It's just bad practice, ... not all PHP builds except [name] as ['name'] or ["name"]. As name could be $name or "name" to PHP 4 + it's just one of them things.

I'm out.

Posted: Thu Aug 28, 2003 8:57 pm
by McGruff
See: "Why is $foo[bar] wrong?" in http://uk2.php.net/types.array

Posted: Thu Aug 28, 2003 10:25 pm
by parkin
oooo.. okie.. :)

I get it now.. :D

thanks for the help! :D