tebrown Posted April 26, 2012 Share Posted April 26, 2012 Hey Guys, I know it may seem pretty simple, but im having trouble populating a drop down list. Here is my code at the moment, but what it's doing is displaying the names all in one value, where it should be in separate select values. *Note that i have only done it to the first one. See attachment. 'AntonMatt' are next to each other, they should be separate select values. <? $id = $_GET['id']; $selectplayers="SELECT * FROM players WHERE club='$club' AND team='$team'"; $player=mysql_query($selectplayers); ?> <table class='lineups' width="560" cellpadding="5"> <tr> <td colspan="2">Starting Lineup</td> <td colspan="2">On the Bench</td> </tr> <tr> <td width="119"> </td> <td width="160"> </td> <td width="69"> </td> <td width="160"> </td> </tr> <tr> <td>Prop</td> <td><select name="secondary" style="width: 150px"> <option value='' selected="selected"><? while($rowplayer = mysql_fetch_array($player)) { echo $rowplayer['fname']; } ?></option> </select></td> <td>16.</td> <td><select name="secondary16" style="width: 150px"> <option value='' selected="selected">Secondary Position</option> </select></td> </tr> <tr> <td style="padding-top: 8px;">Hooker</td> <td style="padding-top: 8px;"><select name="secondary2" style="width: 150px"> <option value='' selected="selected">Secondary Position</option> </select></td> <td style="padding-top: 8px;">17.</td> <td style="padding-top: 8px;"><select name="secondary17" style="width: 150px"> <option value='' selected="selected">Secondary Position</option> </select></td> </tr> </table> </div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/ Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 For each one you need to echo a new <option> element. You have your while loop inside of one option instead of creating the options. Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1340676 Share on other sites More sharing options...
tebrown Posted April 26, 2012 Author Share Posted April 26, 2012 Thanks for the reply. I gave it a go, although im having trouble again. At the moment, its populating the first dropdown list, but now i want to populate another. Although its not working.... is this right? <? $selectplayers="SELECT * FROM players WHERE club='$club' AND team='$team'"; $player=mysql_query($selectplayers); ?> <table class='lineups' width="560" cellpadding="5"> <tr> <td colspan="2">Starting Lineup</td> <td colspan="2">On the Bench</td> </tr> <tr> <td width="119"> </td> <td width="160"> </td> <td width="69"> </td> <td width="160"> </td> </tr> <tr> <td>Prop</td> <td><select name="secondary" style="width: 150px"> <? while($rowplayer = mysql_fetch_array($player)) { ?> <option value=''><?php echo $rowplayer['fname']; ?></option> </select></td> <td>16.</td> <td><select name="secondary16" style="width: 150px"> <? while($rowplayer = mysql_fetch_array($player)) ?> <option value=''><?php echo $rowplayer['fname']; } ?></option> </select></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1340861 Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 Try this instead. You can't access $player again you've already "used it up" basically. <?php $selectplayers="SELECT * FROM players WHERE club='$club' AND team='$team'"; $player=mysql_query($selectplayers); $option_str = ''; while($rowplayer = mysql_fetch_array($player)) { $option_str .= '<option value="{you really need a value in here, probably their id}">'.$rowplayer['fname'].'</option>'; } ?> Then in your HTML <tr> <td>Prop</td> <td><select name="secondary" style="width: 150px"> <?php echo $option_str; ?> </select></td> <td>16.</td> <td><select name="secondary16" style="width: 150px"> <?php echo $option_str; ?> </select></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1340863 Share on other sites More sharing options...
tebrown Posted April 26, 2012 Author Share Posted April 26, 2012 Thanks, how would i make it so that the value of each dropdown will be the one i select in the actual dropdown itself? So then i can then insert that into the database. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1340870 Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 What? Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1340890 Share on other sites More sharing options...
tebrown Posted April 26, 2012 Author Share Posted April 26, 2012 Let me explain. I've got a table in my database called 'fixtures'. I have already input 2 fixtures within the 'fixtures' table. (Have a look at first screenshot). Now with the script below i want to UPDATE that fixture and insert the players (1,2,3...20,21,22). These players will be selected from my 'players' table. Thats why i have been asking the following questions regarding how to get the players into the drop down list. <?php $id = $_GET['id']; $selectplayers="SELECT * FROM players WHERE club='$club' AND team='$team' ORDER by lname DESC"; $player=mysql_query($selectplayers); $option_str = ''; while($rowplayer = mysql_fetch_array($player)) { $option_str .= '<option value='. $rowplayer['fname'] . ' ' . $rowplayer['lname'] .'>'.$rowplayer['fname'].' '.$rowplayer['lname'].'</option>'; } ?> <form id="form" action="fixtures.php" name="lineup" method="post"> <table class='lineups' width="560" cellpadding="5"> <tr> <td colspan="2">Starting Lineup</td> <td colspan="2">On the Bench</td> </tr> <tr> <td width="119"> </td> <td width="160"> </td> <td width="69"> </td> <td width="160"> </td> </tr> <tr> <td>Prop</td> <td><select name="1" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td>16.</td> <td><select name="16" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px;">Hooker</td> <td style="padding-top: 8px;"><select name="2" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td style="padding-top: 8px;">17.</td> <td style="padding-top: 8px;"><select name="17" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px">Prop</td> <td style="padding-top: 8px"><select name="3" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td style="padding-top: 8px;">18.</td> <td style="padding-top: 8px;"><select name="18" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px;">Lock</td> <td style="padding-top: 8px;"><select name="4" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td style="padding-top: 8px;">19.</td> <td style="padding-top: 8px;"><select name="19" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px;">Lock</td> <td style="padding-top: 8px;"><select name="5" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td style="padding-top: 8px;">20.</td> <td style="padding-top: 8px;"><select name="20" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px;">Blindside Flanker</td> <td style="padding-top: 8px;"><select name="6" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td style="padding-top: 8px;">21.</td> <td style="padding-top: 8px;"><select name="21" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px;">Openside Flanker</td> <td style="padding-top: 8px;"><select name="7" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td style="padding-top: 8px;">22.</td> <td style="padding-top: 8px;"><select name="22" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px;">Number 8</td> <td style="padding-top: 8px;"><select name="8" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td> </td> <td> </td> </tr> <tr> <td style="padding-top: 8px;">Scrum Half</td> <td style="padding-top: 8px;"><select name="9" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td style="padding-top: 8px;">Kicker</td> <td style="padding-top: 8px;"><select name="23" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px;">Fly Half</td> <td style="padding-top: 8px;"><select name="10" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td style="padding-top: 8px;">Captain</td> <td style="padding-top: 8px;"><select name="24" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> </tr> <tr> <td style="padding-top: 8px;">Left Wing</td> <td style="padding-top: 8px;"><select name="11" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td> </td> <td> </td> </tr> <tr> <td style="padding-top: 8px;">Inside Centre</td> <td style="padding-top: 8px;"><select name="12" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td> </td> <td> </td> </tr> <tr> <td style="padding-top: 8px;">Outside Centre</td> <td style="padding-top: 8px;"><select name="13" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td> </td> <td> </td> </tr> <tr> <td style="padding-top: 8px;">Right Wing</td> <td style="padding-top: 8px;"><select name="14" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td> </td> <td> </td> </tr> <tr> <td style="padding-top: 8px;">Fullback</td> <td style="padding-top: 8px;"><select name="15" style="width: 150px"> <option>Select Player</option> <?php echo $option_str; ?> </select></td> <td> </td> <td> </td> </tr> </table> <input type="submit" name="lineup" value="Save Lineup" /> <?php if (isset($_POST['submit'])) { $prop1 = trim($_POST['1']); $hooker = trim($_POST['2']); $prop3 = trim($_POST['3']); $lock4 = trim($_POST['4']); $lock5 = trim($_POST['5']); $blindside = trim($_POST['6']); $openside = trim($_POST['7']); $number8 = trim($_POST['8']); $scrumhalf = trim($_POST['9']); $flyhalf = trim($_POST['10']); $leftwing = trim($_POST['11']); $inside = trim($_POST['12']); $outside = trim($_POST['13']); $rightwing = trim($_POST['14']); $fullback = trim($_POST['15']); $r16 = trim($_POST['16']); $r17 = trim($_POST['17']); $r18 = trim($_POST['18']); $r19 = trim($_POST['19']); $r20 = trim($_POST['20']); $r21 = trim($_POST['21']); $r22 = trim($_POST['22']); if (empty($_POST['1']) || empty($_POST['2'])) { echo "<div id='error'>Please fill out Prop and Hooker</div>"; } else { $query = mysql_query("UPDATE fixtures WHERE id='$id' ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22') VALUES ('$prop1','$hooker','$prop3','$lock4','$lock5','$blindside','$openside','$number8','$scrumhalf','$flyhalf','$leftwing','$inside','$outside','$rightwing','$fullback','$r16','$r17','$r18','$r19','$r20','$r21','$r22')"); if($query) { return true; } else { throw new Exception(mysql_error()); return false; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1340903 Share on other sites More sharing options...
reelmark Posted April 27, 2012 Share Posted April 27, 2012 please explain how u debug the code. Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1340936 Share on other sites More sharing options...
tebrown Posted April 27, 2012 Author Share Posted April 27, 2012 Aye? Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1340961 Share on other sites More sharing options...
Jessica Posted April 27, 2012 Share Posted April 27, 2012 Are you saying to want the select dropdown to be already selected, set to the value in the DB? You'll need a separate string for each option list, so you can set selected="selected" on the right one for each. Quote Link to comment https://forums.phpfreaks.com/topic/261633-populate-dropdown-list/#findComment-1341092 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.