• 0

Help with PHP/mySQL script


Question

I have a script that I've had made for a site I look after, and the coder has created it in a way where it doesn't look to make modifying the design of it all that easy.

It is full PHP, he hasn't added any HTML apart from in the echo.

I know nothing about PHP, hence I asked for this to be made.

The below snippet of code is what I mean, and I'm wondering how I would go about making it so it is a basic HTML file, calling the mySQL data from the DB.


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>";
}
[/CODE]

I appreciate any help. I'm happy to send the file for anyone to look over and provide help.

Thanks in advance.

Link to comment
https://www.neowin.net/forum/topic/1131982-help-with-phpmysql-script/
Share on other sites

12 answers to this question

Recommended Posts

  • 0



function printlogo($data){
$teamA = getTeamA($data);
$teamB = getTeamB($data);
}
[/CODE]

[CODE]
<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>
[/CODE]

should work :)

also you had Function printlogo but the html had getlogo

  • 0



function printlogo($data){
$teamA = getTeamA($data);
$teamB = getTeamB($data);
}
[/CODE]

[CODE]
<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>
[/CODE]

should work :)

also you had Function printlogo but the html had getlogo

That doesn't seem to do anything for me. When I test it, it doesn't show anything.

Here is a bigger section of the code, I'm not sure if there is more there that could be of use.

[CODE]
/**
*
* Returns first team's name
*/

function getTeamA($data){
return $data['team1'];
}


/**
*
* Returns second team's name
*/

function getTeamB($data){
return $data['team2'];
}

function getScoreA($data){
return $data['score1'];
}

function getScoreB($data){
return $data['score2'];
}

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>";
}
[/CODE]

  • 0

Hi there.

Here is the entire code:


<!--Live Scores-->
<?PHP

include('library/database.php');
$db = new database();

/****
* If no match is defined to be showed
* Chooses the first match from the list
*
*/

function randomMatch()
{
$match=-1;
$q = "SELECT * FROM game WHERE active = '1'";
$r = mysql_query($q) or die("Error in selecting first match: ".mysql_error());
if(mysql_num_rows($r)==0){
echo "NO MATCH IS CURRENTLY ACTIVE!";
exit;
}

while($row = mysql_fetch_array($r)){
$match = $row['id'];
break;
}
return $match;
}

/**
*
*
*
*/

function thisMatch(){
return $_REQUEST['match'];
}

/**
*
* Returns Id of the current match.
* if an id is specified then thisMatch();
* else randomMatch is called
*
*/

function getId(){
$id=-1;
if(!isset($_REQUEST['match'])){ //GET parameter null
$id = randomMatch();
}
else $id = thisMatch();

if($id==-1){
reportExit();
return -1;
}
return $id;
}

/**
*
* Data variable keeps the data of the row with fixed id
*
*/

function fetchData(){
$id = getId();

if($id==-1)return -1;
$q1 = "SELECT * FROM game WHERE id = '$id'";
$r1 = mysql_query($q1) or die("Error in fetching first active match, Level: 1:: ".mysql_error());
$data = mysql_fetch_assoc($r1) or die("Error in fetching first active match, Level: 2:: ".mysql_error());;
return $data;
}

/**
*
* If No match is currently active, do exit
*
*/

function reportExit(){
echo "No match is currently active!";
exit;
}

/**
*
* Returns first team's name
*/

function getTeamA($data){
return $data['team1'];
}


/**
*
* Returns second team's name
*/

function getTeamB($data){
return $data['team2'];
}

function getScoreA($data){
return $data['score1'];
}

function getScoreB($data){
return $data['score2'];
}

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 printScore($data){

$scoreA = "<B>".getScoreA($data)."</B>";
$scoreB = "<B>".getScoreB($data)."</B>";

echo "<tr><td align='center' width='35' height='40'>$scoreA</td><td align='center' width='30'></td><td align='center' width='35'>$scoreB</td></tr>";
}

function printPlayers($data,$id){
echo "<TD width='35%' style='border:0' valign='top'>";
$var = 'player'.$id;
$str = $data[$var];
$ar = explode(',',$str);
foreach($ar as $ars){
$ars = ltrim($ars);
echo "   ".$ars."<BR>";
}
echo "</TD>";
}

function printLocation($data){
$location = $data['location'];
$time = $data['time'];
echo "<TD style='margin:5; border:0' width='30%' align='center' valign='middle'>$location<BR>$time<BR>";
$str = $data['link'];
$links=explode(',',$str);
foreach($links as $link){
echo "<a href=$link>".$link."</a><BR>";
}
echo "<BR>Currently Active Games:<BR>";
$q = "SELECT * FROM game WHERE active = '1'";
$r = mysql_query($q) or die("Erro in selecting active games on line: 152".mysql_error());
while($row=mysql_fetch_array($r)){
echo "<a href='index.php?match=".$row['id']."'>".$row['team1']." VS ".$row['team2']."</a><BR>";
}
echo "</TD>";
}

function printTime(){
}

function listPlayer($data){
echo "<TR>";
printPlayers($data,1);
printLocation($data);
printPlayers($data,2);
echo "</TR>";
}

function printData($data){
printlogo($data);
printName($data);
printScore($data);
listplayer($data);
}

function printNote($id){
$q = "SELECT * FROM notes WHERE gameid='$id' ORDER BY id DESC";
$r = mysql_query($q) or die("Error in PrintNote:: ".mysql_error());
while($row=mysql_fetch_array($r)){
$time = $row['time'];
$note = $row['body'];
echo '<tr><td width="100%" colspan="3"><B>';
$fDigit = $time{0};
if($fDigit>='0'&&$fDigit<='9'){
if($time%10==1&&$time!=11)echo $time."st Minute";
else if($time%10==2&&$time!=12)echo $time."nd Minute";
else echo $time."th Minute";
}
else echo $time;

echo "</B><BR>$note</td></tr>";
echo "<tr style='border:0' colspan='3'><td style='border:0'> </td></tr>";
}
}

echo "<table align='center' border='1' width='1000'>";
$data=fetchData();
printData($data);
printNote($data['id']);
echo "</table>";
?>
[/CODE]

Thanks :)

  • 0

i am at work so cant test anything really lol

try


<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>
[/CODE]

  • 0

<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>
[/CODE]

  • 0

&lt;snipped&gt;

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 "&lt;tr&gt;&lt;td align='center' width='35' height='40'&gt;&lt;img src='".getLogo($teamA)."'&gt;&lt;/td&gt;&lt;td align='center' width='30'&gt;&lt;/td&gt;&lt;td align='center' width='35'&gt;&lt;img src='".getLogo($teamB)."'&gt;&lt;/td&gt;&lt;/tr&gt;";
}

function printName($data){

  $teamA = "&lt;B&gt;".getTeamA($data)."&lt;/B&gt;";
  $teamB = "&lt;B&gt;".getTeamB($data)."&lt;/B&gt;";

  echo "&lt;tr&gt;&lt;td align='center' width='35' height='40'&gt;$teamA&lt;/td&gt;&lt;td align='center' width='30'&gt;&lt;/td&gt;&lt;td align='center' width='35'&gt;$teamB&lt;/td&gt;&lt;/tr&gt;";
}

function printData($data){
  printlogo($data);
  printName($data);
  printScore($data);
  listplayer($data);
}

echo "&lt;table align='center' border='1' width='1000'&gt;";
$data=fetchData();
printData($data);
printNote($data['id']);
echo "&lt;/table&gt;";
?&gt;
[/CODE]


[b]Note:[/b] 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!
[CODE]
function printlogo($data){
  $teamA = getTeamA($data);
  $teamB = getTeamB($data);
}
[/CODE]

[CODE]
&lt;tr&gt;
&lt;td align='center' width='35' height='40'&gt;
&lt;img src="&lt;?php printlogo($teamA) ?&gt;" /&gt;
&lt;/td&gt;
&lt;td align='center' width='30'&gt;&lt;/td&gt;
&lt;td align='center' width='35'&gt;
&lt;img src="&lt;?php printlogo($teamB) ?&gt;" /&gt;&lt;/td&gt;
&lt;/tr&gt;
[/CODE]


[CODE]
&lt;tr&gt;
&lt;td align='center' width='35' height='40'&gt;
&lt;img src="&lt;?php echo printlogo($teamA); ?&gt;" /&gt;
&lt;/td&gt;
&lt;td align='center' width='30'&gt;&lt;/td&gt;
&lt;td align='center' width='35'&gt;
&lt;img src="&lt;?php echo printlogo($teamB); ?&gt;" /&gt;&lt;/td&gt;
&lt;/tr&gt;
[/CODE]


[CODE]
&lt;tr&gt;
&lt;td align='center' width='35' height='40'&gt;
&lt;img src="&lt;?php echo getLogo($teamA); ?&gt;" /&gt;
&lt;/td&gt;
&lt;td align='center' width='30'&gt;&lt;/td&gt;
&lt;td align='center' width='35'&gt;
&lt;img src="&lt;?php echo getLogo($teamB); ?&gt;" /&gt;&lt;/td&gt;
&lt;/tr&gt;
[/CODE]


That does give a result, but doesn't show any images.
http://live.thenashy.com/test.php

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 [u]within[/u] 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 &lt;img src="images/.png"&gt;&lt;/img&gt; instead of something like &lt;img src="images/brisbane broncos.png"&gt;&lt;/img&gt;, 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:
[code]
&lt;tr&gt;
	&lt;td align='center' width='35' height='40'&gt;
		&lt;img src="&lt;?php getLogo(getTeamA($data)) ?&gt;" /&gt;
	&lt;/td&gt;
	&lt;td align='center' width='30'&gt;&lt;/td&gt;
	&lt;td align='center' width='35'&gt;
		&lt;img src="&lt;?php getLogo(getTeamB($data)) ?&gt;" /&gt;
	&lt;/td&gt;
&lt;/tr&gt;

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");
}

  • 0

Most of what theblazingangel said is correct, but the final product he gave you is not... There were definitely some redundancies in the code and it was far from professionally done, but the image name still needs echoed out. You only return data that you want echoed out when you are calling the function from within an echo statement:


function getLogo($team){
return rawurlencode("images/" . strtolower($team) . ".png");
}
<tr>
<td align='center' width='35' height='40'>
<img src="<?php echo (getLogo(getTeamA($data))); ?>" />
</td>
<td align='center' width='30'></td>
<td align='center' width='35'>
<img src="<?php echo (getLogo(getTeamB($data))); ?>" />
</td>
</tr>
[/CODE]

Otherwise, the code will not output as you would like it to. Another option is to leave the html as is, and change the return statement to an echo statement:

[CODE]
function getLogo($team){
echo rawurlencode("images/" . strtolower($team) . ".png");
}
[/CODE]

After doing one of those two options, you shouldn't have any more issues.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Apple is expanding Private Cloud Compute beyond its own data centers by Pradeep Viswanathan At WWDC 2026, as part of the improved Apple Intelligence capabilities, Apple today announced that it is expanding Private Cloud Compute (PCC), its privacy-focused cloud infrastructure for Apple Intelligence, beyond its own data centers for the first time. Private Cloud Compute was designed to handle Apple Intelligence requests that are too complex to run fully on-device. The PCC system does not store user data and does not allow Apple or anyone else to access user requests. Last year, Apple also expanded its Security Bounty program with rewards of up to $1 million for researchers who could find serious vulnerabilities in PCC. Until now, Apple's PCC data centers were using Apple's own silicon. As part of the expansion, Apple is working with Google and NVIDIA to run new Apple Intelligence workloads on Google Cloud systems powered by NVIDIA GPUs. Apple will be using this new infrastructure to execute more demanding AI tasks while maintaining the same privacy and security guarantees of PCC. The new implementation uses NVIDIA Confidential Computing with NVIDIA GPUs, Intel CPUs with TDX, and Google’s Titan chip. Apple says it has worked with Google to build additional protections beyond a traditional confidential computing deployment. Despite the expansion to third-party data centers, Apple claims that its core PCC requirements remain unchanged, including stateless computation, no privileged runtime access, non-targetability, and verifiable transparency. The company highlighted that it will continue to control the PCC software stack, and Apple devices will only trust PCC software that has been cryptographically approved by Apple. To take security to the next level, Apple mentioned that it is maintaining an append-only ledger of Google Cloud hardware that is part of the PCC fleet. The company claims this will help reduce the risk of supply chain attacks. In addition to AI infrastructure, Apple also worked with Google to use technologies behind the Gemini family of models to build the next generation of Apple Foundation Models to power Apple Intelligence features across on-device and cloud workloads. As expected, for more demanding AI tasks like agentic tool use and complex reasoning, Apple will rely on the expanded PCC infrastructure running on Google Cloud. The expansion of PCC on Google Cloud will gradually ramp toward the full set of protections during the summer preview period. As before, Apple will also publish binaries for public inspection, provide research tooling, and give researchers access to live PCC nodes in research mode through the Apple Security Bounty Program.
    • my problem with outlook (new) is that it connects only to outlook.com. all connections to external providers goes through there. Got your mail server and want to use imap directly? no way... it adds a connector on outlook.com. last bug; if your email on an external provider if the same as principal email of your microsoft account, it doesn't work...
    • It's the only reason I finally have an iPhone (for work) and enjoy using it so much that I'm tempted to move from android next time I need to replace my own device
    • So is Russia, China, Iran, North Korea, just to mention a few. What's your point? Everyone is a threat from their enemies' perspective. I'd say that Israel is only a threat to their immediate enemies like Hamas, Hezbollah and the Iranian regime, not to anyone else.
    • The government is not the good guy either. You propose 99% of people require that the government overreach and govern their freedom of information and privacy, while ignoring the government is made up 100% of people, of which 99% are (as you described) brain dead. You can't have both. The reality is Signal is absolutely right and the government is doing what it has always done. Ignoring that we are their boss and grabbing all the power they possibly can to make sure we aren't. Your (societies) ###### parenting is not reason enough as to why I can't have a safe platform for my data/information. Thinking the government is helping is precisely what they are targeting psychologically to take suckers like you for a ride. "Think of the children" was, has, is, and will always be a mechanism of control. In the rare occasion it's actually essential the mass consensus has always been there and it doesn't become a debate.
  • Recent Achievements

    • Very Popular
      Captain_Eric earned a badge
      Very Popular
    • One Month Later
      amusc earned a badge
      One Month Later
    • One Month Later
      DJC50PLUS earned a badge
      One Month Later
    • Week One Done
      DJC50PLUS earned a badge
      Week One Done
    • Proficient
      Eric Biran went up a rank
      Proficient
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      PsYcHoKiLLa
      222
    3. 3
      ATLien_0
      92
    4. 4
      +Edouard
      86
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!