If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Populate dropdown with stored array

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-02-10, 11:48
Tim Schutz Tim Schutz is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
Populate dropdown with stored array

My apologies. I cross-posted in the mySQL forum as well.

I have array data that was previously stored like so:

Code:
$allTeams=array('SF','CHI','CIN');
$userTeam=implode(',',$allTeams);
$query = "INSERT INTO users VALUES ('$userTeam')";
Now, I would like to populate a dropdown list with this array data. Here is what I have tried:

Code:
<select name="teamList" id="teamList">
<?php

include 'dbcon.php';

mysql_select_db($database, $conn);
	$query  = "SELECT userTeam FROM users WHERE fullName='Tim Schutz'";
	$result = mysql_query($query);
	while($row = mysql_fetch_assoc($result))
	{ 
	echo '<option value="' . $row['fullName'] . '">' . $row['userTeam'] . '</option>';
	}	

?>
</select>
The dropdown gets populated, but as one long string as a single item. I tried exploding the data, but nothing shows. Can anyone point me in the right direction?
Reply With Quote
  #2 (permalink)  
Old 04-02-10, 11:59
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by Tim Schutz
Can anyone point me in the right direction?
Do I understand you correctly ...
You've stored the value 'SF','CHI','CIN' into a table and now you're confused when you select the data from this table and it returns the value 'SF','CHI','CIN'.
Can I suggest you store the values separately and that you perhaps read up on normalisation
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 04-02-10, 12:10
futurity futurity is offline
Registered User
 
Join Date: May 2008
Posts: 270
Either you're inserting your data wrong, or you table design is not normalized.

Here's what your INSERT looks like:
Code:
INSERT INTO users VALUES ('SF,CHI,CIN')
So what you're selecting is:
Code:
'Tim Schutz', 'SF,CHI,CIN'
What I'm assuming you want is:
Code:
INSERT INTO users VALUES ('SF'), ('CHI'), ('CIN')
Which would give you:
Code:
'Tim Schutz', 'SF'
'Tim Schutz', 'CHI'
'Tim Schutz', 'CIN'
However, this still won't help you build a correct select list, since you're going to end up with:
Code:
<select name="teamList" id="teamList">
  <option value="Tim Schutz">SF</option>
  <option value="Tim Schutz">CHI</option>
  <option value="Tim Schutz">CIN</option>
</select>
Reply With Quote
  #4 (permalink)  
Old 05-28-10, 03:37
Sammach Sammach is offline
Registered User
 
Join Date: May 2010
Posts: 1
i think the data insterted wrong
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On