Author Topic: Drop Down Box with a Pre-Selected Value based on a Variable  (Read 887 times)

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Drop Down Box with a Pre-Selected Value based on a Variable
« on: September 20, 2009, 10:01:10 AM »
Long topic title I know, anyway...Basically what I have is a clan system included in my BBG, and I've added a system to allow the Clan Leader's to assign custom ranks, and I'm doing the member listing in a table, with a column for name, rank, title and clan donations.

The rank change I planned to have was for a drop down box to exist in each row of the table for each user that allows you to select a new rank and change it, it only appears to the leader though, I have all that set up, but I wanted to make it smoother, and have the user's current rank pre-selected from the drop down box.
Code: [Select]
<select name=RankChange>
<option value="<? echo $QueryRanks['Rank10']?>"><?echo $QueryRanks['Rank10']?></option>
<option value="<? echo $QueryRanks['Rank9']?>"><?echo $QueryRanks['Rank9']?></option>
<option value="<? echo $QueryRanks['Rank8']?>"><?echo $QueryRanks['Rank8']?></option>
<option value="<? echo $QueryRanks['Rank7']?>"><?echo $QueryRanks['Rank7']?></option>
<option value="<? echo $QueryRanks['Rank6']?>"><?echo $QueryRanks['Rank6']?></option>
<option value="<? echo $QueryRanks['Rank5']?>"><?echo $QueryRanks['Rank5']?></option>
<option value="<? echo $QueryRanks['Rank4']?>"><?echo $QueryRanks['Rank4']?></option>
<option value="<? echo $QueryRanks['Rank3']?>"><?echo $QueryRanks['Rank3']?></option>
<option value="<? echo $QueryRanks['Rank2']?>"><?echo $QueryRanks['Rank2']?></option>
<option value="<? echo $QueryRanks['Rank1']?>"><?echo $QueryRanks['Rank1']?></option>
</select>
Each option in the select box has a value that corresponds exactly to one of the clan's custom ranks, so I wondered if there was some code I could use to work out which option should be pre-selected based on the user's current rank?

Thanks in advance for any help, hopefully I've been clear about my problem :)

~Glenugie~

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #1 on: September 20, 2009, 10:12:14 AM »
You can try something like:

Code: [Select]
<select name=RankChange>
<?php foreach(array_reverse($QueryRanks) as $qr){
  echo 
"<option value=\"$qr\"";
  if( 
$player["rank"]==$qr ) echo " selected=\"selected\"";
  echo 
">$qr</option>";
  } 
?>

</select>
« Last Edit: September 20, 2009, 10:38:08 AM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #2 on: September 20, 2009, 10:23:56 AM »
No, doesn't seem to be working, even with a few changes. I don't know if it'll help, but the query Im using to get the Custom Ranks is:
Code: [Select]
$QueryRanks = mysql_query("SELECT Rank1, Rank1Karma, Rank2, Rank2Karma, Rank3, Rank3Karma, Rank4, Rank4Karma, Rank5, Rank5Karma, Rank6, Rank6Karma, Rank7, Rank7Karma, Rank8, Rank8Karma, Rank9, Rank9Karma, Rank10, Rank10Karma, LeaderRank FROM Clan WHERE ClanID='$ClanID'");
$QueryRanksRow = mysql_fetch_array($QueryRanks);

The result from the code you provided is an select box with about 15 empty options.

Thanks for the reply though :)

~Glenugie~

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #3 on: September 20, 2009, 10:40:19 AM »
Don't have a clue why it's not working :/ , but basicly for each <option> you compare it's value with the player's value and if it matches then you insert selected="selected" there as the option's attribute
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Glenugie

  • Level 10
  • *
  • Posts: 64
  • Reputation: +0/-0
    • View Profile
    • The Land of Ennui
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #4 on: September 20, 2009, 10:57:59 AM »
I think it may have been the loop/array structure that was causing it, I fixed it by using an if statement for each of the 10 ranks, pretty innefficient, but it'll get the job done for now:
Code: [Select]
$Rank10= $QueryRanks['Rank10'];
$Rank9= $QueryRanks['Rank9'];
$Rank8= $QueryRanks['Rank8'];
$Rank7= $QueryRanks['Rank7'];
$Rank6= $QueryRanks['Rank6'];
$Rank5= $QueryRanks['Rank5'];
$Rank4= $QueryRanks['Rank4'];
$Rank3= $QueryRanks['Rank3'];
$Rank2= $QueryRanks['Rank2'];
$Rank1= $QueryRanks['Rank1'];
echo "<option value='$Rank10'";if ($QueryRanks['Rank10']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank10</option>";
echo "<option value='$Rank9'";if ($QueryRanks['Rank9']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank9</option>";
echo "<option value='$Rank8'";if ($QueryRanks['Rank8']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank8</option>";
echo "<option value='$Rank7'";if ($QueryRanks['Rank7']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank7</option>";
echo "<option value='$Rank6'";if ($QueryRanks['Rank6']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank6</option>";
echo "<option value='$Rank5'";if ($QueryRanks['Rank5']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank5</option>";
echo "<option value='$Rank4'";if ($QueryRanks['Rank4']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank4</option>";
echo "<option value='$Rank3'";if ($QueryRanks['Rank3']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank3</option>";
echo "<option value='$Rank2'";if ($QueryRanks['Rank2']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank2</option>";
echo "<option value='$Rank1'";if ($QueryRanks['Rank1']==$Row['ClanRank']) { echo " selected= 'selected'";}echo ">$Rank1</option>";

Offline karnedge

  • Level 17
  • *
  • Posts: 170
  • Reputation: +4/-0
  • ctrlHack provides the server, you bring the skill.
    • View Profile
    • ctrl://Hack.game
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #5 on: September 20, 2009, 01:21:00 PM »
Code: [Select]
<?php
echo "<select name=\"RankChange\">\n";
foreach(
$QueryRanks as $rank){
  echo 
"\t<option value=\"$rank\"";
  if(
$rank == $Row['ClanRank']) echo ." selected=\"selected\"";
  echo .
">$rank</option>\n";
}
echo 
"</select>";
?>

This could be done better. And the MYSQL you provided doesn't seem to match or go together with the second coding you did. I based this off the second coding. Keep in mind that it's still not very clean.
ctrlHack - Hacking simulation RPG in development.
Latest blog: Back on Track
bbgFramework v0.1.3

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #6 on: September 20, 2009, 01:29:54 PM »
Like karnedge I based this off your second posting, his seems like it will work also but here is another way of doing it using variable variables and an immediate if:

Code: [Select]
<?php
for($i=10$i<=0$i--) {
  
$fld 'Rank' $i;
  
$selected = ( $QueryRanks[$$fld] == $Row['ClanRank'] ) ? 'selected' '';
  echo 
"<option value='$$fld$selected>$$fld</option>";
}
?>

Creating online addictions, one game at a time:

Offline karnedge

  • Level 17
  • *
  • Posts: 170
  • Reputation: +4/-0
  • ctrlHack provides the server, you bring the skill.
    • View Profile
    • ctrl://Hack.game
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #7 on: September 20, 2009, 04:13:38 PM »
The way mine is done will use every single ranking you have in the table so you will end up with 15+ entries. With Codestryke's example, it will take each rank 1 through 10 and echo those only instead which it probably what you want.

It just seems like the way you have your table set up is a bit redundant with "karma" ranks but it would take more knowledge of what you're doing and how your using the data to figure out a better table setup.
ctrlHack - Hacking simulation RPG in development.
Latest blog: Back on Track
bbgFramework v0.1.3

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #8 on: September 21, 2009, 09:11:14 AM »
And here's another method, just for good measure. :P

Code: (php) [Select]
foreach($QueryRanks as $rank){
    printf ('%s<option%s value="%s">%s</option>%s', "\t", (($Row ['ClanRank'] === $rank) ? ' selected="selected"' : ''), $rank, $rank, "\n");
}
« Last Edit: September 21, 2009, 09:14:01 AM by JGadrow »
Idiocy - Never underestimate the power of stupid people in large groups.


Offline Crazy-T

  • Level 19
  • *
  • Posts: 197
  • Reputation: +0/-0
  • Building Games
    • View Profile
Re: Drop Down Box with a Pre-Selected Value based on a Variable
« Reply #9 on: September 25, 2009, 10:01:45 AM »

echo "<select class=\"dropdown\" name=\"rank\">\n";
foreach (
$QueryRanks as $rank)  echo "<option value=\"$rank\"". ($row['clanRank'] == $rank " selected=\"selected\"" "") .">$rank</option>\n"
echo 
"</select>";

There was my go at it lol
Crazy-T

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal