Nashy, on 21 January 2013 - 10:41, said:
<snipped>
function getTeamA($data){
return $data['team1'];
}
function getTeamB($data){
return $data['team2'];
}
function getLogo($team){
$team=strtolower($team);
return "images/$team.png";
}
function printlogo($data){
$teamA = getTeamA($data);
$teamB = getTeamB($data);
echo "<tr><td align='center' width='35' height='40'><img src='".getLogo($teamA)."'></td><td align='center' width='30'></td><td align='center' width='35'><img src='".getLogo($teamB)."'></td></tr>";
}
function printName($data){
$teamA = "<B>".getTeamA($data)."</B>";
$teamB = "<B>".getTeamB($data)."</B>";
echo "<tr><td align='center' width='35' height='40'>$teamA</td><td align='center' width='30'></td><td align='center' width='35'>$teamB</td></tr>";
}
function printData($data){
printlogo($data);
printName($data);
printScore($data);
listplayer($data);
}
echo "<table align='center' border='1' width='1000'>";
$data=fetchData();
printData($data);
printNote($data['id']);
echo "</table>";
?>
Note: do NOT replace the code you posted with the above, I just snipped it down to some of the bits you need to focus on here to understand why Haggis's code will not work and how to fix it!
Haggis, on 21 January 2013 - 10:25, said:
function printlogo($data){
$teamA = getTeamA($data);
$teamB = getTeamB($data);
}
<tr>
<td align='center' width='35' height='40'>
<img src="<?php printlogo($teamA) ?>" />
</td>
<td align='center' width='30'></td>
<td align='center' width='35'>
<img src="<?php printlogo($teamB) ?>" /></td>
</tr>
Haggis, on 21 January 2013 - 10:48, said:
<tr>
<td align='center' width='35' height='40'>
<img src="<?php echo printlogo($teamA); ?>" />
</td>
<td align='center' width='30'></td>
<td align='center' width='35'>
<img src="<?php echo printlogo($teamB); ?>" /></td>
</tr>
Haggis, on 21 January 2013 - 11:17, said:
<tr>
<td align='center' width='35' height='40'>
<img src="<?php echo getLogo($teamA); ?>" />
</td>
<td align='center' width='30'></td>
<td align='center' width='35'>
<img src="<?php echo getLogo($teamB); ?>" /></td>
</tr>
Nashy, on 21 January 2013 - 11:35, said:
The primary flaw in Haggi's code in all three of the above posts is that of scope. The variables $teamA and $teamB only exist
within the printLogo() and printName() functions, not outside of them! Therefore Haggi's code, which exists outside of these functions, tries to use the content of these variables and fails to work.
Additionally, all of the functions in the snipped down set of code (snipped down to show only stuff related to Haggis's code), with the exception of getLogo(), expect to be given a copy of the $data variable, which holds the entire row of data from the database (as an "array"), while Haggis's code is incorrectly trying to give them a variable containing only a team name.
Hence why the HTML in your test page only contains <img src="images/.png"></img> instead of something like <img src="images/brisbane broncos.png"></img>, it's unable to get the name of the team due to incorrect use of the functions your developer created.
The correct code would be:
<tr>
<td align='center' width='35' height='40'>
<img src="<?php getLogo(getTeamA($data)) ?>" />
</td>
<td align='center' width='30'></td>
<td align='center' width='35'>
<img src="<?php getLogo(getTeamB($data)) ?>" />
</td>
</tr>
This makes the printLogo() function redundant btw. The whole purpose of the printLogo() function is to generate a snippit of HTML code to display a logo, but this replaces that, so you can get rid of it.
What is happening here (assuming the OP needs it explaining), is we are calling the getTeamA() and getTeamB() functions respectively, passing in the entire $data variable, as they expect. These functions return the team name from the $data variable. This return value is then given to getLogo() which puts together and returns the url for the image file.
Really, url's shoud be encoded, so replace the getLogo() function with:
function getLogo($team){
return rawurlencode("images/" . strtolower($team) . ".png");
}