Jump to content

Selecting a value to update dynamically

Mr Chris

Recommended Posts

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>


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!







Link to comment
Share on other sites



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?



Link to comment
Share on other sites

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;?>


The above syntax has not been validated.

Link to comment
Share on other sites

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>
           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");


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?



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.