Mr Chris Posted June 11, 2007 Share Posted June 11, 2007 Hi All, I have a table of appearances Now normally, if I wanted to show the players name in my database on a specific record, I’d normally do it like this: <select name="player_one" id="player_one"> <option value="1"<? if ($section == '1') echo ' selected' ?>>Jamie Jackson</option> <option value="2"<? if ($section == '2') echo ' selected' ?>>Carl Dennison</option> </select> So in my example Carl Dennison would be the value selected from the database and shown on my page. But say rather than me manually write out all the players names in my dropdown box is there anyway I could do it like this. Create an additional table called players with all of all the players in it: Then join the two tables together and where player_one appeared in my appearances table and see what number that was and reference that against the player_id in the players table to output the player_name in the dropdown menu? Any help on this and if it could be achieved would be most welcome! Thanks Chris Quote Link to comment https://forums.phpfreaks.com/topic/55101-selecting-a-value-to-update-dynamically/ Share on other sites More sharing options...
fenway Posted June 11, 2007 Share Posted June 11, 2007 I don't see the queries that are producing the results used for the dropdowns. Quote Link to comment https://forums.phpfreaks.com/topic/55101-selecting-a-value-to-update-dynamically/#findComment-272422 Share on other sites More sharing options...
Mr Chris Posted June 11, 2007 Author Share Posted June 11, 2007 Thanks, But that's what i'm asking. The example i'm showing shows I know how to do it manually - ie writing each players name in the select menu as shown. But I'm wondering if (and IF I can do this dynamically) someone can point me in the right direction by an example or maybe a tutorial that does EXACTLY this as i'd imagine a lot of people come across this problem with dynamic content? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/55101-selecting-a-value-to-update-dynamically/#findComment-272604 Share on other sites More sharing options...
bubblegum.anarchy Posted June 12, 2007 Share Posted June 12, 2007 Consider creating a global function to grab the result set, that is accessible to the entire website (or where applicable): function player_name_result() { static $result; if (!isset($result)) { mysql_query($query = " SELECT * FROM appearances ORDER BY team_id") or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR); } return $result; } The above query would obviously have to be more appropriate... then the function can be called from within the html like this: <SELECT class="MyClass" id="player_one" name="player_one" <OPTION value="0">Select Player</OPTION> <? while ($record = mysql_fetch_assoc(player_name_result())):?> <OPTION value="<?=$record['player_one'];?>"<? if ($record['player_one'] == $selected['player_one']):?> selected<? endif;?>></OPTION><? endwhile;?> </SELECT> The above syntax has not been validated. Quote Link to comment https://forums.phpfreaks.com/topic/55101-selecting-a-value-to-update-dynamically/#findComment-272766 Share on other sites More sharing options...
Mr Chris Posted June 12, 2007 Author Share Posted June 12, 2007 Thanks for posting that - it helped a lot. However using a foreach: <select class="input" name="player_one" size="1" style="width: 145" tabindex="1"> <option value="" selected>Player One</option> <?php $type_array=get_player_names(); foreach ($type_array as $players) { // the below statement worked to pull All the player_ids and player names out of the database // print("<option value=\"".$players['player_id']."\">".$players['player_name']."</option>\n"); // But to get the value currently assigned it does not print("<option value=\"".$players['player_id']."\">."<? if ($players['player_one'] == $selected['player_one']):?> selected<? endif;?>."</option>\n"); } ?> </select> My option value to pull the current data out of the database I can't get right I know it's this line: print("<option value=\"".$players['player_id']."\">."<? if ($players['player_one'] == $selected['player_one']):?> selected<? endif;?>."</option>\n"); Can anyone help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/55101-selecting-a-value-to-update-dynamically/#findComment-273031 Share on other sites More sharing options...
bubblegum.anarchy Posted June 12, 2007 Share Posted June 12, 2007 What does your version of player_name_result() return? - keep in mind that my version returned a result id used with mysql_fetch_assoc() and not an array. Quote Link to comment https://forums.phpfreaks.com/topic/55101-selecting-a-value-to-update-dynamically/#findComment-273076 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.