badgerchap Posted July 2, 2013 Share Posted July 2, 2013 (edited) Sorry guys, I imagine this is as simple as simple can be. I've only delved into PHP since this morning, and whilst it's actually gone pretty well so far, I'm absolutely stumped with the simplest of tasks. Here is my snippet: <?php $con=mysqli_connect("blahblahblah....."); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM Basket"); echo "<table border='1'> <tr> <th>Product</th> <th>Quantity</th> <th>Price (£)</th> </tr>"; while($column = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $column['Product'] . "</td>"; echo "<td>" . $column['Quantity'] . "</td>"; echo "<td>" . $column['Price'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> This is displaying the contents of a user's basket, and it's doing so just fine, with Product, Quantity and Price column being displayed perfectly. What I can't do, no matter how many of other people's snippets I try and tinker with and fit into my code, I just cannot get a total price for the basket to display. I'm sure there is a simple solution, I just can't work it out myself right now! Any help greatly appreciated, many Thanks, Badgerchap Edited July 2, 2013 by badgerchap Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/ Share on other sites More sharing options...
ginerjm Posted July 2, 2013 Share Posted July 2, 2013 I don't see any code where you tried to do that sum. You also did a nice job of error checking your connection, but what about your query itself? Bad practice... Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439101 Share on other sites More sharing options...
badgerchap Posted July 2, 2013 Author Share Posted July 2, 2013 I haven't put the sum in, because so far I've drawn a complete blank! As for bad practice - c'mon, it's my first day! Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439103 Share on other sites More sharing options...
ginerjm Posted July 2, 2013 Share Posted July 2, 2013 Have you ever programmed before? Use those skills here. You're looping thru all of your results. Accumulate what you need. Then output it at the end of your loop. Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439109 Share on other sites More sharing options...
Solution insidus Posted July 2, 2013 Solution Share Posted July 2, 2013 Your not far off! have alook at this and try to figure it out. $totalPrice = 0; echo "<table border='1'> <tr> <th>Product</th> <th>Quantity</th> <th>Price (£)</th> </tr>"; while($column = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $column['Product'] . "</td>"; echo "<td>" . $column['Quantity'] . "</td>"; echo "<td>" . $column['Price'] . "</td>"; echo "</tr>"; $totalPrice += $column['Price']; } echo "</table>"; echo $totalPrice; Start by initiating a variable to hold the total price before the while loop. Inside the while loop, after you print the table row, increment the total price by column['price']. Once outside the while loop, echo the total price out. $totalPrice += $column['Price'] is the same as $totalPrice = $totalPrice + $column['Price']; Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439113 Share on other sites More sharing options...
badgerchap Posted July 2, 2013 Author Share Posted July 2, 2013 Excellent, I will have a fiddle Many thanks. I will report back! Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439121 Share on other sites More sharing options...
badgerchap Posted July 2, 2013 Author Share Posted July 2, 2013 Excellent! It works a treat, thank you so much! My only problem is that it puts it in a really weird position, but I should be able to solve that by myself. Thanks insidus, you've fixed a real head-bender! Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439122 Share on other sites More sharing options...
insidus Posted July 2, 2013 Share Posted July 2, 2013 Not a problem, glad I could help Can you set this thread as 'answered'? /Insidus Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439123 Share on other sites More sharing options...
ginerjm Posted July 2, 2013 Share Posted July 2, 2013 Add another row to your table, output a ' ' character to all the td elements except where you want to the total, then close the row and the table. Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439124 Share on other sites More sharing options...
badgerchap Posted July 2, 2013 Author Share Posted July 2, 2013 Think I've marked it as such. Thanks also ginerjm, will try that too after dinner Quote Link to comment https://forums.phpfreaks.com/topic/279796-sum-of-column-in-php-mysql-help-required-for-noob/#findComment-1439127 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.