Jump to content

Recommended Posts

How do i make the equation $percent = round($row['points']/$row['played']*100, 2) ; not run if a player has not yet played a game and have it display 0 instead?

echo "<table width='700'>
<tr>
<td class='h5'>ref</td>
<td class='h5'>last</td>
<td class='h5'>first</td>
<td class='h5'>grade</td>
<td class='h5'>club</td>
<td class='h5'>played</td>
<td class='h5'>Won</td>
<td class='h5'>Drawn</td>
<td class='h5'>Lost</td>
<td class='h5'>Points</td>
<td class='h5'>&#37;</td>
</tr>";

  $result = mysql_query("SELECT * FROM members WHERE club='fen'");

while ($row = mysql_fetch_array($result))
  {

  
  
  $percent = round($row['points']/$row['played']*100, 2) ;

  
echo "<tr>";
  
  echo "<td class='h5'>" . $row['ref'] . "</td>";
  echo "<td class='h5'>" . $row['last_name'] . "</td>";
  echo "<td class='h5'>" . $row['first_name'] . "</td>";
  echo "<td class='h5'>" . $row['grade'] . "</td>";
  echo "<td class='h5'>" . $row['club'] . "</td>";
  echo "<td class='h5'>" . $row['played'] . "</td>";
  echo "<td class='h5'>" . $row['won'] . "</td>";
  echo "<td class='h5'>" . $row['drawn'] . "</td>";
  echo "<td class='h5'>" . $row['lost'] . "</td>";
  echo "<td class='h5'>" . $row['points'] . "</td>";
  echo "<td>" . $percent. "</td>";
  echo "</tr>";
  }
echo "</table>";

  

  mysql_close($con);
?>

Example-

 

2632 Smith John 112 Fen 0 0 0 0 0 0

Best wishes,

John.

Thanks for your help Shawn, i think we are almost there, the amended code=

<?php

include("connect to database goes here"); 

echo "<table width='700'>
<tr>
<td class='h5'>ref</td>
<td class='h5'>last</td>
<td class='h5'>first</td>
<td class='h5'>grade</td>
<td class='h5'>club</td>
<td class='h5'>played</td>
<td class='h5'>Won</td>
<td class='h5'>Drawn</td>
<td class='h5'>Lost</td>
<td class='h5'>Points</td>
<td class='h5'>&#37;</td>
</tr>";

   $result = mysql_query("SELECT * FROM members WHERE club='fen'");

while ($row = mysql_fetch_array($result))
  {

  
  
if($row['points'] > 0) {
  $percent = round($row['points']/$row['played']*100, 2);
}

  
echo "<tr>";
  
  echo "<td class='h5'>" . $row['ref'] . "</td>";
  echo "<td class='h5'>" . $row['last_name'] . "</td>";
  echo "<td class='h5'>" . $row['first_name'] . "</td>";
  echo "<td class='h5'>" . $row['grade'] . "</td>";
  echo "<td class='h5'>" . $row['club'] . "</td>";
  echo "<td class='h5'>" . $row['played'] . "</td>";
  echo "<td class='h5'>" . $row['won'] . "</td>";
  echo "<td class='h5'>" . $row['drawn'] . "</td>";
  echo "<td class='h5'>" . $row['lost'] . "</td>";
  echo "<td class='h5'>" . $row['points'] . "</td>";
  echo "<td>" . $percent. "</td>";
  echo "</tr>";
  }
echo "</table>";

  

  mysql_close($con);
?>

 

and displayed-

ref last first grade club played Won Drawn Lost Points %

249 Smith John       162 Fen 3 2 1 0 2.5 83.33

266 Bloggs Stephen 112 Fen 0 0 0 0 0 83.33

262 Davies Philip 87 Fen 0 0 0 0 0 83.33

143 Jones Anthony 145 Fen 0 0 0 0 0 83.33

262 Thomas Stuart 98 Fen 0 0 0 0 0 83.33

 

Any idea how to fix this? Apologies for my lack of php knowledge.

Best wishes,

John.

 

 

Put all of that echo code inside the IF so that it won't display if played is 0.

 

Also, you want to check $row['played'] is not zero because that is what you are dividing by.  Suppose they played 1 game but didn't score any points?  Then your code wouldn't show the game.

Hi Shawn,

  I have spent several hours trying to put together something that works and no matter what i try it either supplies an error or it doesn't display the result the way i need it to display, would you mind creating the code so that i can simply copy/paste and i can finally move on?

My most recent attempt looks like-

<?php

include("includes/db_conn.php"); 

echo "<table width='700'>
<tr>
<td class='h5'>ref</td>
<td class='h5'>last</td>
<td class='h5'>first</td>
<td class='h5'>grade</td>
<td class='h5'>club</td>
<td class='h5'>played</td>
<td class='h5'>Won</td>
<td class='h5'>Drawn</td>
<td class='h5'>Lost</td>
<td class='h5'>Points</td>
<td class='h5'>&#37;</td>
</tr>";

   $result = mysql_query("SELECT * FROM members WHERE club='fen'");

while ($row = mysql_fetch_array($result))
  {

if($row['played'] > 0)
$percent = round($row['points']/$row['played']*100, 2);
  echo "<tr>";
  echo "<td class='h5'>" . $row['ref'] . "</td>";
  echo "<td class='h5'>" . $row['last_name'] . "</td>";
  echo "<td class='h5'>" . $row['first_name'] . "</td>";
  echo "<td class='h5'>" . $row['grade'] . "</td>";
  echo "<td class='h5'>" . $row['club'] . "</td>";
  echo "<td class='h5'>" . $row['played'] . "</td>";
  echo "<td class='h5'>" . $row['won'] . "</td>";
  echo "<td class='h5'>" . $row['drawn'] . "</td>";
  echo "<td class='h5'>" . $row['lost'] . "</td>";
  echo "<td class='h5'>" . $row['points'] . "</td>";
  echo "<td>" . $percent. "</td>";
  echo "</tr>";
  
  }
  else {
   ($row['played'] < 1)

  echo "<tr>";
  echo "<td class='h5'>" . $row['ref'] . "</td>";
  echo "<td class='h5'>" . $row['last_name'] . "</td>";
  echo "<td class='h5'>" . $row['first_name'] . "</td>";
  echo "<td class='h5'>" . $row['grade'] . "</td>";
  echo "<td class='h5'>" . $row['club'] . "</td>";
  echo "<td class='h5'>" . $row['played'] . "</td>";
  echo "<td class='h5'>" . $row['won'] . "</td>";
  echo "<td class='h5'>" . $row['drawn'] . "</td>";
  echo "<td class='h5'>" . $row['lost'] . "</td>";
  echo "<td class='h5'>" . $row['points'] . "</td>";
  echo "<td></td>";
  echo "</tr>";
} 
  
  echo "</table>";
  
  

  

  mysql_close($con);
?>

 

i really wanted to try to solve this on my own as i thought it would help me understand how php works but i guess im not up to the task yet.  i appreciate your help.

Best wishes,

John.

You really don't need all of that. try this:

 

include("includes/db_conn.php"); 
echo "<table width='700'>
<tr>
<td class='h5'>ref</td>
<td class='h5'>last</td>
<td class='h5'>first</td>
<td class='h5'>grade</td>
<td class='h5'>club</td>
<td class='h5'>played</td>
<td class='h5'>Won</td>
<td class='h5'>Drawn</td>
<td class='h5'>Lost</td>
<td class='h5'>Points</td>
<td class='h5'>&#38;#37;</td>
</tr>";
$result = mysql_query("SELECT * FROM members WHERE club='fen'");
while ($row = mysql_fetch_array($result)){
$percent = $row['played'] > 0 ? round($row['points']/$row['played']*100, 2) : 0;
  echo "<tr>";
  echo "<td class='h5'>" . $row['ref'] . "</td>";
  echo "<td class='h5'>" . $row['last_name'] . "</td>";
  echo "<td class='h5'>" . $row['first_name'] . "</td>";
  echo "<td class='h5'>" . $row['grade'] . "</td>";
  echo "<td class='h5'>" . $row['club'] . "</td>";
  echo "<td class='h5'>" . $row['played'] . "</td>";
  echo "<td class='h5'>" . $row['won'] . "</td>";
  echo "<td class='h5'>" . $row['drawn'] . "</td>";
  echo "<td class='h5'>" . $row['lost'] . "</td>";
  echo "<td class='h5'>" . $row['points'] . "</td>";
  echo "<td>" . $percent. "</td>";
  echo "</tr>";  
  }
} 
echo "</table>";
mysql_close($con);

 

at the en of the $percent line, where there's : 0; you can change it for : ' '; if you don't want to display anything.

No prob. Glad I could help out.

The only websites I use are php.net and google. This is not really an 'area' of php. it's about programming logic.

the code I gave you:

$percent = $row['played'] > 0 ? round($row['points']/$row['played']*100, 2) : 0;

is basically shorthand for this: (EXACTLY THE SAME THING)

if($row['played'] > 0){
    $percent = round($row['points']/$row['played']*100, 2);
}else{
    $percent = 0;
}

 

basically you set the $percent variable to whatever you want, and always display the same html code (since the only difference was the $percent, it's easier to just display it anyway, than to repeat all the html for each condition)

 

Thanks for breaking it down for me, you make it sound so simple and i think i found a website that might help me understand a little more at http://www.programmingvideotutorials.com/ thanks again, my next task is to build a league table with (chess) clubs, for a win they get 2 points and a draw 1 point, i also have to work out how to show them in order, with the team with the greatest points at the top and descending from there. I wasn't planning on using a CMS to update, i was just going to update using the mysql database, i thought that way would be more secure unless some one tells me otherwise.

Best wishes,

John.

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.