kingsbest Posted June 19, 2011 Share Posted June 19, 2011 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'>%</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. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted June 19, 2011 Share Posted June 19, 2011 if($row['played'] > 0) { $percent = round($row['points']/$row['played']*100, 2); } Quote Link to comment Share on other sites More sharing options...
kingsbest Posted June 19, 2011 Author Share Posted June 19, 2011 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'>%</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. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 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. Quote Link to comment Share on other sites More sharing options...
kingsbest Posted June 21, 2011 Author Share Posted June 21, 2011 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'>%</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. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 21, 2011 Share Posted June 21, 2011 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'>&#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. Quote Link to comment Share on other sites More sharing options...
kingsbest Posted June 22, 2011 Author Share Posted June 22, 2011 Thanks that works great, are there any websites on-line that cover this area of php? Once again huge thanks for helping and all the best. John Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 22, 2011 Share Posted June 22, 2011 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) Quote Link to comment Share on other sites More sharing options...
kingsbest Posted June 22, 2011 Author Share Posted June 22, 2011 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. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted June 22, 2011 Share Posted June 22, 2011 In case you were wondering, the $var = (condition) ? val1 : val2; syntax webstyles used is the ternary operator. Going to mark your topic as solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.