Jump to content

arrays


lukep11a

Recommended Posts

Hi, I am trying to specify a new variable that points to two columns from a table, I think I need to use an array but not sure how to code it. The reason I want one variable instead of two is because it will be used as a dynamic link for home teams and away teams to link to the team page, hope that makes sense. Here is the code I currently have:

 

<?php
	$query = "SELECT tr.competition, tr.date, tr.htp, tr.hts, tr.ats, tr.atp, tr.et, tr.htpts, tr.atpts, tth.team as hometeam, tta.team as awayteam
FROM test_selections ts 
LEFT JOIN (test_results tr, test_teams tth, test_teams tta) 
ON (tth.teamid = tr.hometeam AND tta.teamid = tr.awayteam)
WHERE ts.userid = '{$_SESSION['userid']}' AND (tr.hometeam = ts.teamid OR tr.awayteam = ts.teamid)
GROUP BY tr.resultid
ORDER BY tr.resultid DESC";
        $result = mysql_query($query) or die(mysql_error());
        
        while($row = mysql_fetch_assoc($result))
	{
	$team = array($row['hometeam'], $row['awayteam']);
	?>

 

Any help would be very much appreciated.

Link to comment
Share on other sites

<?php 
$team = array();
while($row = mysql_fetch_assoc($result)) {
$team[] = array($row['hometeam'], $row['awayteam']);
}
print_r($team);
?>

 

You are selecting a lot of information in your query that you don't seem to be using. I suggest removing unnecessary columns to make it more efficient.

Link to comment
Share on other sites

I am using the selected columns later on, just didnt think I need to show it all but actually, I'm not sure how to call the array, this is the code I had before that doesn't seem to be working with the new code:

 

<tr>
	<td width="85" class="fixtures_date"><?php echo $row['date']; ?></td>
	<td width="30" class="fixtures_comp"><?php echo $row['competition']; ?></td>
	<td width="135" class="fixtures_home_teams"><?php echo "<a href='data/teams.php?team=$team' title='$team Team Data' />";
        					  echo $team;
        					  echo "</a>"; ?></td>
        <td width="25" class="fixtures_center"><?php echo $row['htp']; ?></td>
        <td width="25" class="fixtures_center"><?php echo $row['hts']; ?></td>
	<td width="25" class="fixtures_center">-</td>
        <td width="25" class="fixtures_center"><?php echo $row['ats']; ?></td>
        <td width="25" class="fixtures_center"><?php echo $row['atp']; ?></td>
	<td width="135" class="fixtures_away_teams"><?php echo $row['awayteam']; ?></td>
        <td width="25" class="fixtures_center"><?php echo $row['et']; ?></td>
        <td width="50" class="fixtures_center"><?php echo $row['htpts']; ?></td>
        <td width="50" class="fixtures_center"><?php echo $row['atpts']; ?></td>
	</tr>

 

 

Link to comment
Share on other sites

This is the full set of code for the original query:

 

<table width="635" border="0">
	<?php
	$query = "SELECT tr.competition, tr.date, tr.htp, tr.hts, tr.ats, tr.atp, tr.et, tr.htpts, tr.atpts, tth.team as hometeam, tta.team as awayteam
FROM test_selections ts 
LEFT JOIN (test_results tr, test_teams tth, test_teams tta) 
ON (tth.teamid = tr.hometeam AND tta.teamid = tr.awayteam)
WHERE ts.userid = '{$_SESSION['userid']}' AND (tr.hometeam = ts.teamid OR tr.awayteam = ts.teamid)
GROUP BY tr.resultid
ORDER BY tr.resultid DESC";
        $result = mysql_query($query) or die(mysql_error());
        
        while($row = mysql_fetch_assoc($result))
	{
	$team = $row['team'];
	?>
	<tr>
	<td width="85" class="fixtures_date"><?php echo $row['date']; ?></td>
	<td width="30" class="fixtures_comp"><?php echo $row['competition']; ?></td>
	<td width="135" class="fixtures_home_teams"><?php echo $row['hometeam']; ?></td>
        <td width="25" class="fixtures_center"><?php echo $row['htp']; ?></td>
        <td width="25" class="fixtures_center"><?php echo $row['hts']; ?></td>
	<td width="25" class="fixtures_center">-</td>
        <td width="25" class="fixtures_center"><?php echo $row['ats']; ?></td>
        <td width="25" class="fixtures_center"><?php echo $row['atp']; ?></td>
	<td width="135" class="fixtures_away_teams"><?php echo $row['awayteam']; ?></td>
        <td width="25" class="fixtures_center"><?php echo $row['et']; ?></td>
        <td width="50" class="fixtures_center"><?php echo $row['htpts']; ?></td>
        <td width="50" class="fixtures_center"><?php echo $row['atpts']; ?></td>
	</tr>
	<?php
	}
	?>
	</table>

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.