Page 1 of 1

Populating multiple dropdown boxes and processing them

Posted: Wed Feb 26, 2003 1:21 pm
by sinus
Hi,

I'm trying to create a number of dropdown boxes based on the number of rows returned from a query, which will be different for a number of users.
below is some persuedo code of what i have done.

Code: Select all

$query = query databse to results to populate dropdown boxes,
$result =mysql_query($query);
$numteams = mysql_num_rows($result);

$noteams = 0;


while($noteams <= $numteams) &#123;

$selectid = 1; // used to give the give select html tag a different name
while ($rowhome = mysql_fetch_array($result)) &#123;
$hometeam = $rowhome &#1111;'database field name'];
$hometeamid = $rowhome &#1111;'database field name'];

print ("<select name=hometeam$selectid>");
print ("<option value=$hometeamid>$homteam</option>");
print ("</select"); 

$selectid++;
&#125;

$noteams++;
&#125;
Ok even if this is'nt correct I have got this part working, so that it produces multiple dropdown boxes each with a different name to pass as a variable.

Now i have the problem getting the script to read the various variables
using $_POST as I do not know how many will be passed. so I have tried the following:

Code: Select all

// having passed a varible called numberteams form the form i try a   
// loop put it does not sure if this is posible to do.

$numberteams = $_POST&#1111;'numberteams'];

$selectid = 1;
$noteams = 0;
 while ($noteams <= $numberteams)) &#123;
$hometeam.$selectid = $_POST&#1111;'hometeam.$selectid'];

$selectid++
&#125;
I'm wondering if this can be done this way or is there another way to enter the information, after this stage it will then be validated and enter back into the database.

Posted: Wed Feb 26, 2003 3:47 pm
by decoy1
I'm not sure exactly what you mean when you say 'multiple' drop down boxes, but I think this little snippet will simplify it a bit.

Code: Select all

echo '<select name=hometeam>';
$num = count($numteams);
for($i = 0; $i < $num; $i++)
{ 
  print("<option value=$i>$homteam[$i]</option>");
}
echo '</select>';
If different users see different results, then maybe use cookies and some conditionals.

Again, I'm not sure what your desired result is but try using $HTTP_POST_VARS and see if that works, _POST won't work on 4.1 or earlier...I think.

Posted: Wed Feb 26, 2003 4:49 pm
by sinus
THanks for the help
sorry if i did not explain well.

The idea is a league may have 10 teams, other leagues may have more or less, depending on the number of teams in a given league. So lets say that there are 10 tens in a league, i need to display five
html select tags for home teams and 5 for away teams, I have have been able to do that with each select name=hometeam1, then next time around select name=hometeam2 etc until both hometeam and awayteam are done,

I am using $_POST because the new version of php has global variables turned off. and i don't want to have to go back into the script later on when my host upgades, $_POST works on may present set up.

The problem arises when passing all these variables to the script to process, not knowing how many hometeam1, hometeam2 etc that will be generated until the the script executes and displays the dropdown lists, so i was trying to loop the $variable = $_POST[]; until all homteams and awayteams where passed successfully.

Posted: Wed Feb 26, 2003 8:35 pm
by volka
hm...I'm still confused
while ($rowhome = mysql_fetch_array($result)) {
...
print ("<select name=hometeam$selectid>");
print ("<option value=$hometeamid>$homteam</option>");
print ("</select");
...
}
then you have a number of <select>-elements, each with only one option - nothing to choose from.

Is the purpose to create some kind of clan war manager where one can build a war table like
groupA.team1 vs. groupB.team4
groupA.team2 vs. groupB.team2
groupA.team3 vs. groupB.team1
... ?

Posted: Wed Feb 26, 2003 8:55 pm
by sinus
Hi,
similar what it is to produce is the following

Code: Select all

<select name=homeaway1>
<option value=1>Raiders</option> // 1 being the team id
// more teams based on how many is in the league
</select>

v

<select name=awayteams1>
<option value=1>Raiders</option> // 1 being the team id
// more teams based on how many is in the league
</select>
then more of the above depending on how many teams in league divide by 2
so if there are 4 teams in the it would out the above code plus the following

Code: Select all

<select name=homeaway2>
<option value=1>Raiders</option> // 1 being the team id
// more teams based on how many is in the league
</select>

v

<select name=awayteams2>
<option value=1>Raiders</option> // 1 being the team id
// more teams based on how many is in the league
</select>
so ten teams would produce
the above until select name=hometeam5 and select name=awayteam5,
In the previous explainations i have not include the away teams as the principle for how the hometeams are produce is similar. it's so that when entering in fixtures for the league all fixtures for a certain date can be entered in one go. I already written the code that produces the above output. the problem arises when validationing etc as I said previously I am using $_POST, and i am trying to loop this for each hometeam and awayteam entered so as not have to
write

Code: Select all

$hometeam1 = $_POST&#1111;'hometeam1'];
$hometeam2 = $_POST&#1111;'hometeam2'];
//etc
just to make sure that I have done enough to cover all teams in the league. this problem also applies to validation that select options are not duplicates etc, and to writing the information into the database
rather than write 20 different queries and change $hometeam1 to $hometeam2 to $hometeam3 etc for each different one.

Hopefully this clears it up,
thanks[/quote]

Posted: Thu Feb 27, 2003 12:54 am
by volka
I have been in that mood to write a skeleton script.
There's certainly a better way to accomplish the same and you still have to change a quite bit to make this working (e.g. the initialization, all fields are set to -- not selected --)

Code: Select all

<?php
// example-array
$teams = array(
	array('name'=>'team1', 'id'=>'1'),
	array('name'=>'team2', 'id'=>'2'),
	array('name'=>'team3', 'id'=>'3'),
	array('name'=>'team4', 'id'=>'4'),
	array('name'=>'team5', 'id'=>'5'),
	array('name'=>'team6', 'id'=>'6'),
	array('name'=>'team7', 'id'=>'7'),
	array('name'=>'team8', 'id'=>'8'),
	array('name'=>'team9', 'id'=>'9'),
	array('name'=>'team10', 'id'=>'10'),
);
?>
<html>
<head>
	<script type="text/javascript">
		var gOptions = new Array(
						new Array('-- nothing selected --', -1)
<?php
foreach($teams as $team)
{ ?>
						, new Array("<?php echo addslashes($team['name']); ?>", <?php echo $team['id']; ?>)
<?php
}
?>
					);
	</script>
	<script type="text/javascript" src="teammanage.js"></script>	
</head>
<body>
	<pre><?php print_r($_POST); ?></pre>
	<form method="POST">
		<table>
<?php
for ($i=0; $i<count($teams)/2; $i++)
{
?>
			<tr>
				<td><select name="match[<?php echo $i; ?>][0]" onChange="mcheck(this);" ></select></td>
				<td><select name="match[<?php echo $i; ?>][1]" onChange="mcheck(this);" ></select></td>
			</tr>
<?php
}	
?>		
			<tr><td colspan="2"><input type="submit" /></td></tr>
		</table>
	</form>
	<script type="text/javascript">minit();</script>
</body>
</html>
and teammanage.ja contains

Code: Select all

function minit()
&#123;
	collSelect = document.getElementsByTagName("select");
	for (i=0; i!=collSelect.length; i++)
	&#123;
		for (j=0; j!=gOptions.length; j++)
		&#123;
			opt = document.createElement("OPTION");
			opt.appendChild(document.createTextNode(gOptions&#1111;j]&#1111;0]));
			opt.value = gOptions&#1111;j]&#1111;1];
			collSelect&#1111;i].appendChild(opt);
		&#125;
	&#125;
&#125;

function mcheck(which)
&#123;
	collSelect = document.getElementsByTagName("select");
	for (i=0; i!=collSelect.length; i++)
	&#123;
		for (j=0; j!=collSelect&#1111;i].options.length; j++)
			collSelect&#1111;i].options&#1111;j].style.color = "black";
	&#125;
	
	for (i=0; i!=collSelect.length; i++)
	&#123;
		if (collSelect&#1111;i].value == which.value &amp;&amp; collSelect&#1111;i] != which)
			collSelect&#1111;i].value = -1;
		else
		&#123;
			for (j=0; j&lt;collSelect.length; j++)
			&#123;
				for(k=0; k!=collSelect&#1111;j].options.length; k++)
				&#123;
					if (collSelect&#1111;i].value != -1 &amp;&amp; collSelect&#1111;j].options&#1111;k].value == collSelect&#1111;i].value &amp;&amp; j!=i)
						collSelect&#1111;j].options&#1111;k].style.color = "red";
				&#125;
			&#125;
		&#125;
	&#125;
&#125;
note that the fields are named match[<?php echo $i; ?>][0/1]
take a look at the output of print_r($_POST); after you've submitted the form

Posted: Sun Mar 09, 2003 10:19 am
by sinus
hi,
thanks for the help i'll play around with this to see if i can get it working for my needs
bye sinus