Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Unanswered: 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?

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

  3. #3
    Join Date
    May 2008
    Posts
    277
    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>

  4. #4
    Join Date
    May 2010
    Posts
    1
    i think the data insterted wrong

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •