PHP/MYSQL - listing multiple column data in dropdown 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
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP/MYSQL - listing multiple column data in dropdown list

Post by cjkeane »

Hi.

I'm having an issue retreiving multiple columns of data and listing them under eachother in a dropdown list.
currently, the result is that all email addresses are listed on one line in the dropdown list separated with a .
I'm wondering if anyone has any suggestions.

Code: Select all

<?php  
$result3=mysql_query("SELECT EMail, EMail_1, Email_2 FROM principal where CompanyName = '".$CompanyName."' AND BranchName='".$CompanyBranch."'"); 
$options3=""; 
	while ($row3=mysql_fetch_array($result3)) { 
		$categoryname3=$row3["EMail"] . '<br />' . $row3["EMail_1"] . '<br />' . $row3["EMail_2"]; 
		$options3.="<OPTION VALUE=\"$categoryname3\">".$categoryname3.'</option>'; 
	} 
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by pickle »

You can't put < br / > inside an option "value", nor in-between the opening & closing tags.

For cleanliness, you should just be retrieving the data here & letting your template format it for output.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by cjkeane »

if that's the case, what's the best way to populate a dropdown list with the data from 3 columns? with a dropdown list displaying it as a list?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by pickle »

Separate them with some character. A comma or dash perhaps?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by cjkeane »

i could do that, but data would appear as follows:
if email 1 existed but not the others: 1@home.ca, ,
if 2 email addresses existed: 1@home.ca, 2@home.ca,
if all 3 emails exist: 1@home.ca, 2@home.ca, 3@home.ca
then when i pass the selected value to a textbox, it would appear as stated above.
what i need to do is list the emails like so:
1@home.ca
2@home.ca
3@home.ca

then when any of the emails are selected, only that email address is passed to the textbox.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by pickle »

Ah.

Then for each row, output 3 options - one for each address.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by cjkeane »

how could that be accomplished? i've tried the below, but nothing displays. i'm assuming because Email_2 is blank.

Code: Select all

<?php  
	$result3=mysql_query("SELECT EMail, EMail_1, Email_2 FROM principal where CompanyName = '".$CompanyName."' AND BranchName='".$CompanyBranch."'"); 
	$options3=""; 
		while ($row3=mysql_fetch_array($result3)) { 
			$categoryname3=$row3["EMail"]; 
			$categoryname3=$row3["EMail_1"];
			$categoryname3=$row3["EMail_2"];
			$options3.="<OPTION VALUE=\"$categoryname3\">".$categoryname3.'</option>'; 
		} 
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by pickle »

You're overwriting $categoryname3 twice. You need three separate variables to store the three separate addresses.

Also, you're not outputting anything - is that done elsewhere?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by cjkeane »

OK. I'm now getting the 3 emails on 3 separate lines

i'm half way there. thanks for the help.

Code: Select all

<?php  
$result3=mysql_query("SELECT EMail, EMail_1, EMail_2 FROM principal where CompanyName = '".$CompanyName."' AND BranchName='".$CompanyBranch."'"); 
	$options3=""; 
	while ($row3=mysql_fetch_array($result3)) { 
		$categoryname3a=$row3["EMail"]; 
		$categoryname3b=$row3["EMail_1"];
		$categoryname3c=$row3["EMail_2"];
		$options3.="<OPTION VALUE=\"$categoryname3a\">".$categoryname3a.'</option>'; 
		$options3.="<OPTION VALUE=\"$categoryname3b\">".$categoryname3b.'</option>'; 
		$options3.="<OPTION VALUE=\"$categoryname3c\">".$categoryname3c.'</option>'; 
	} 
?>
<select name="users3" id="SelectEMailTo3" onchange="showUser3(this.value)">
         <option value="">< select > <?php echo $options3 ?></option>
</select>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by pickle »

That won't work either, as you're putting options within options. Inside your <select>, just echo $options3
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP/MYSQL - listing multiple column data in dropdown lis

Post by cjkeane »

i did echo $options3, but without defining 3 different option values, they didn't show up.
Post Reply