joemal Posted August 17, 2014 Share Posted August 17, 2014 (edited) Hi guys, I am needing some help of how to place the coding as I can not get it to work. -- Basically I am building a website for the local motorclub and want some code that will "add up" each persons results. Here is what I have come up with but it does not work; <?php $total = mysql_fetch_array("SELECT cat1 + cat2 + cat3 + cat4 + cat5 + cat6 AS total FROM results") ?> <?php $cautotest_results = mysql_query("SELECT * FROM results WHERE cat ='1' ORDER BY cattotal DESC"); if(mysql_num_rows($cautotest_results) == '') { echo "<p>No Results Available."; } else { echo "<table width=\"800\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"center\"> <tr align=\"center\"> <td>Competitor</td> <td>Vehicle</td> <td>Class</td> <td>Skipton</td> <td>Weston</td> <td>King Bros</td> <td>Normans</td> <td>Norwood</td> <td>Uniroyal</td> <td>Total</td> </tr>"; $x=1; while($results_row = mysql_fetch_array($cautotest_results)) { if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif; echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">"; echo "<td>" . $results_row['competitor'] . "</td>"; echo "<td>" . $results_row['catvehicle'] . "</td>"; echo "<td>" . $results_row['catclass'] . "</td>"; echo "<td>" . $results_row['cat1'] . "</td>"; echo "<td>" . $results_row['cat2'] . "</td>"; echo "<td>" . $results_row['cat3'] . "</td>"; echo "<td>" . $results_row['cat4'] . "</td>"; echo "<td>" . $results_row['cat5'] . "</td>"; echo "<td>" . $results_row['cat6'] . "</td>"; echo "<td>" . $total . "</td>"; echo "</tr>"; $x++; } echo "</table>"; } ?> Its the top line of coding... and the echo .total. at the bottom. If you could please correct me I would be ever so grateful. If you need more info please just ask. All help is appreciated. JTM. Edited August 17, 2014 by JTM Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 17, 2014 Share Posted August 17, 2014 you would put the sum logic into the existing query (2nd one in the posted code), so that it would produce a total for each row. Quote Link to comment Share on other sites More sharing options...
joemal Posted August 17, 2014 Author Share Posted August 17, 2014 <?php $cautotest_results = mysql_query("SELECT * FROM results WHERE cat ='1' ORDER BY cattotal DESC"); $total = mysql_fetch_array("SELECT cat1 + cat2 + cat3 + cat4 + cat5 + cat6 AS total FROM results"); if(mysql_num_rows($cautotest_results) == '') { echo "<p>No Results Available."; } else { echo "<table width=\"800\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"center\"> <tr align=\"center\"> <td>Competitor</td> <td>Vehicle</td> <td>Class</td> <td>Skipton</td> <td>Weston</td> <td>King Bros</td> <td>Normans</td> <td>Norwood</td> <td>Uniroyal</td> <td>Total</td> </tr>"; $x=1; while($results_row = mysql_fetch_array($cautotest_results)) { if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif; echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">"; echo "<td>" . $results_row['competitor'] . "</td>"; echo "<td>" . $results_row['catvehicle'] . "</td>"; echo "<td>" . $results_row['catclass'] . "</td>"; echo "<td>" . $results_row['cat1'] . "</td>"; echo "<td>" . $results_row['cat2'] . "</td>"; echo "<td>" . $results_row['cat3'] . "</td>"; echo "<td>" . $results_row['cat4'] . "</td>"; echo "<td>" . $results_row['cat5'] . "</td>"; echo "<td>" . $results_row['cat6'] . "</td>"; echo "<td>" . $total . "</td>"; echo "</tr>"; $x++; } echo "</table>"; } ?> So its like this? then on the page it gets: Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in /home/jet2vi/public_html/download/modules/cautotest.php on line 11 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 17, 2014 Share Posted August 17, 2014 (edited) You really need to read before writing code. You are doing your querying all backwards. The best approach (IMHO): $sql = "my query statement"; $qresults = mysqli_query($link,$sql); while ($row = $qresults->fetch_array() { (handle the contents of this result row) } You have shown two examples of your misunderstanding where you try and process data that you never queried. First you write, second you execute, third you play with the results. Edited August 17, 2014 by ginerjm Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 17, 2014 Share Posted August 17, 2014 @JTM, is this a second account for you? we have another member joemal, that posted code for this same assignment and from the same location you are at. Quote Link to comment Share on other sites More sharing options...
joemal Posted August 18, 2014 Author Share Posted August 18, 2014 @mac_gyver - I didnt login through facebook last when i signed up. I have now logged in via facebook and clicked link account with joemal? Quote Link to comment Share on other sites More sharing options...
joemal Posted August 18, 2014 Author Share Posted August 18, 2014 Also still not managed to get the coding working. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 18, 2014 Share Posted August 18, 2014 Did you read my previous post? Did you make any changes from what you read? Did you read the .... manual? Quote Link to comment Share on other sites More sharing options...
joemal Posted August 18, 2014 Author Share Posted August 18, 2014 I didnt really understand what to do with what you put. I have got the manual yes, not read it yet though. Im new to coding and this is the last bit I need for the webpage. It worked perfectly as I used to "store" the total value on the database. But now I am trying to learn new ways of doing things. thats why my coding may seem back to front to you. so with your code do you mean; <?php $sql = "SELECT * FROM results WHERE cat1='1'"; $qresults = mysqli_query($link,$sql); while ($row = $qresults->fetch_array() if(mysql_num_rows($qresults) == '') { echo "<p>No Results Available."; } else { echo "<table width=\"800\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"center\"> <tr align=\"center\"> <td>Competitor</td> <td>Vehicle</td> <td>Class</td> <td>Skipton</td> <td>Weston</td> <td>King Bros</td> <td>Normans</td> <td>Norwood</td> <td>Uniroyal</td> <td>Total</td> </tr>"; $x=1; while($results_row = mysql_fetch_array($qresults)) { if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif; echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">"; echo "<td>" . $results_row['competitor'] . "</td>"; echo "<td>" . $results_row['catvehicle'] . "</td>"; echo "<td>" . $results_row['catclass'] . "</td>"; echo "<td>" . $results_row['cat1'] . "</td>"; echo "<td>" . $results_row['cat2'] . "</td>"; echo "<td>" . $results_row['cat3'] . "</td>"; echo "<td>" . $results_row['cat4'] . "</td>"; echo "<td>" . $results_row['cat5'] . "</td>"; echo "<td>" . $results_row['cat6'] . "</td>"; echo "<td>" . $total . "</td>"; echo "</tr>"; $x++; } echo "</table>"; } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 19, 2014 Share Posted August 19, 2014 You should really read the manual for the functions you are using. There are many good examples of how they relate to other functions so it will help you construct some good code. 1 - your current code cannot possibly run - Turn On Error Checking at the top of every php script you write while in development mode. error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); 2 - Plan what you want to do - your current process is not in the correct order to do your task. You do a fetch of a query result (with an error that makes the statement non-executable) and THEN you check to see how may rows were returned. Does anything seem backwards here? 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.