Jump to content

Create Listbox Depending On Variable


MoFish

Recommended Posts

hello everyone.

 

I currently have a variable called $NumPlayers which contains a number between 4 and 8;

 

depending on this value; i would like to create this amount of listboxes on my page. For example if the value of $NumPlayers is 6;

 

I would like 6 listboxes to be displayed on the page with names such as "Lstbox1" "Lstbox2".

 

I'm a bit clueless where to start; and was hoping someone could point me in the right direction.

 

Thanks alot, MoFish

Link to comment
Share on other sites

it looks like i forgot a quote.  it should be:

 

<?php
for($i=1;$i<=$NumPlayers;++$i){
    echo '<select name="LstBox'.$i.'">
            <option value="">Choice</option>
            </select>';
}
?>

 

 

your code is a little messed up. you want the select to be outside of the for loop. also there is even a little more missing as well... the value="" cannot be empty, otherwise it defeats the purpose of having a dropdown menu. no values will pass with the form from the dropdown menu. i'm not sure what value you'd put in there, but it needs to have one. my guess is you want it to look like this:

<?php
        echo "<select name=\"dropdown\">\n";
        for($i = 1; $i <= $NumPlayers; $i++){
                echo "<option value=\"{$i}\">{$i} player". (($i != 1) ? ('s') : ('')) ."\n";
        }
        echo "</select>\n";
?>

Link to comment
Share on other sites

hello again.

 

boo_lolly; i tested your code, and it puts the number of values into one listbox instead of generating the number of individual listboxes; cx323's code seemed to work in the correct mannor.

 

my next problem is populating these listboxes with data from a database. i want each listbox to be populated with the same list of data. I tryed the following code which worked well for the first listbox; but not the rest.

 

once these have been populated i hope to be able to select values from them; and submitted it in a form.

 

		$result = mysql_query("SELECT * FROM `tbl_players`");

			for($i=1;$i<=$NumPlayers;++$i){
    		
			echo '<select name="LstBox'.$i.'">';

			while ($myrow = mysql_fetch_array($result)) {
				$opt = $myrow['PlayerName'];
				echo "<option>$opt</option> ";
			}

			echo '</select>';
}

Link to comment
Share on other sites

do you mean like this:

 

$result = mysql_query("SELECT * FROM `tbl_players`");
while ($myrow = mysql_fetch_array($result)) {
$opt .= '<option value="'.$myrow['PlayerName'].'">'.$myrow['PlayerName'].'</option>';
}
for($i=1;$i<=$NumPlayers;++$i){
echo '<select name="LstBox'.$i.'">'.$opt.'</select>';
}

Link to comment
Share on other sites

sorry, didnt want to make a thread for something small like this ... why does the following formula not work?

 

$1stPlaceWinnings = $PlayerNumber * $BuyInPrice - $BuyInPrice;

 

Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in D:\wampnew\www\APL\ConfirmAndReport.php on line 91

 

the values are for example:

 

$PlayerNumber = 7

$BuyInPrice = 5

 

so - 7 * 5 = 35 then - 5 = 30

 

30 is the answer i need, but the above doesnt seem to work.

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.

Guest
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.