I need help with my page. I am taking two variables from another page as Posts. The two variables are numbers that I will later need to change to names they are assigned to in my table. I then start to build an html table and form.

My SQL table has the first post variable (AwayTeam or HomeTeam) associated with the second post variable (week). The if statements determine if the first post variable is an away or home team as I'd then post the third column (SQL table) as the opponent.

I have a simple function that will convert the number IDs from one table to the actual team names. I return that team name value to the main script to echo to the page.

The script will only see the first away team name. Any assistance?

<?
$teamlist=$_POST['teamlist'];
$seriesweek=$_POST['seriesweek'];

//include file
$con = mysql_connect($serverhost,$account,$key);

if (!$con)
{  die('Could not connect: ' . mysql_error());
}

mysql_select_db("rontoec_teams", $con);

 echo "<form action='testworks.php' method='post'>";
 echo "<table>";
 echo "<tr>";
 echo "<td>Series</td>";
 echo "<td>Home Team</td>";
 echo "<td>Away Team</td>";
 echo "<td></td>";
 echo "</tr><tr>";
 echo "<td>";

 $request = mysql_query("SELECT * FROM schedule WHERE Week = $seriesweek");

while ($array = mysql_fetch_array($request))
{
	if ($array[AwayTeam] == $teamlist)
	 	{
		$opponent=$array['HomeTeam'];
	 	echo $selectedaway=teamselect($teamlist) . " at " . $selectedhome=teamselect($opponent);
        echo "</td>";
        echo "<td><input name='updatewinsaway' type='text' value=" . $array[AwayWins] . " /></td>";
 		echo "<td><input name='updatewinshome' type='text' value=" . $array[HomeWins] . " /></td>";
        }
   if ($array[HomeTeam] == $teamlist)
	 	{
	 	$opponent=$array['AwayTeam'];
	 	echo $selectedaway=teamselect($opponent) . " at " . $selectedhome=teamselect($teamlist);
        echo "</td>";
        echo "<td><input name='updatewinsaway' type='text' value=" . $array[AwayWins] . " /></td>";
 		echo "<td><input name='updatewinshome' type='text' value=" . $array[HomeWins] . " /></td>";
        }
}
 echo "<td><input type='submit'></td></tr></table>";
 echo "</form>";

function teamselect($team) {

$yourteam = mysql_query("SELECT * FROM teams");

while($row = mysql_fetch_array($yourteam))
	{
	if ($team == $row['xid'])
	{
		$selected = $row['name'];
  }
return $selected;
}
}
?>

Recommended Answers

All 3 Replies

You want something like this to return more teams ?

function teamselect($team) 
{ 
  $selected = array ();
  $yourteam = mysql_query("SELECT * FROM teams WHERE xid='$team'"); 
  while ($row = mysql_fetch_array($yourteam))	
  {	
    $selected[] = $row['name'];  
  }
  return $selected;
}

That worked great. I passed two variables for both teams to the function and set up two separate queries/loops and it did return both.

while ($array = mysql_fetch_array($request))
{
	if ($array[AwayTeam] == $teamlist)
	 	{
		$opponent=$array[HomeTeam];
        $team=teamselect($teamlist,$opponent);
	 	echo $team[0] . " at " . $team[1];
        echo "</td>";
        echo "<td><input name='updatewinsaway' type='text' value=" . $array[AwayWins] . " /></td>";
 		echo "<td><input name='updatewinshome' type='text' value=" . $array[HomeWins] . " /></td>";
        }
   elseif ($array[HomeTeam] == $teamlist)
	 	{
	 	$opponent=$array[AwayTeam];
        $team=teamselect($teamlist,$opponent);
	 	echo $team[1] . " at " . $team[0];
        echo "</td>";
        echo "<td><input name='updatewinsaway' type='text' value=" . $array[AwayWins] . " /></td>";
 		echo "<td><input name='updatewinshome' type='text' value=" . $array[HomeWins] . " /></td>";
        }
}

function teamselect($team, $opponent)
{
$selected = array();
$yourteam = mysql_query("SELECT * FROM teams WHERE xid='$team'");
	while ($row = mysql_fetch_array($yourteam))
	{
		$selected[0] = $row['name'];
	}
$theirteam = mysql_query("SELECT * FROM teams WHERE xid='$opponent'");
	while ($row = mysql_fetch_array($theirteam))
	{
		$selected[1] = $row['name'];
	}
return $selected;
}

What I wonder is if functions can be called twice in the same statement? In the original code, I called this function twice (one for each team) in the same echo statement. Is there where php got confused?

Thanks for your help!

No. The problem was in your code. Although you looped all records, you returned only one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.