Get values from array, select it ona multiple select list

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
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

Get values from array, select it ona multiple select list

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

Post 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:
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

Post 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:
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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).
d3_realm
Forum Newbie
Posts: 3
Joined: Thu Aug 28, 2003 8:13 pm

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

See: "Why is $foo[bar] wrong?" in http://uk2.php.net/types.array
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

Post by parkin »

oooo.. okie.. :)

I get it now.. :D

thanks for the help! :D
Post Reply