Jump to content


Photo

Sum of column in PHP/ MySQL - Help required for noob!


Best Answer insidus, 02 July 2013 - 02:40 PM

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'];

Go to the full post


  • Please log in to reply
9 replies to this topic

#1 badgerchap

badgerchap

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 02 July 2013 - 11:36 AM

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 by badgerchap, 02 July 2013 - 11:36 AM.


#2 ginerjm

ginerjm

    Advanced Member

  • Members
  • PipPipPip
  • 1,912 posts
  • LocationVoorheesville NY

Posted 02 July 2013 - 01:35 PM

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...


JG

PS - If you're posting here you should be using:
        error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!


#3 badgerchap

badgerchap

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 02 July 2013 - 02:19 PM

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! :P



#4 ginerjm

ginerjm

    Advanced Member

  • Members
  • PipPipPip
  • 1,912 posts
  • LocationVoorheesville NY

Posted 02 July 2013 - 02:28 PM

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.


JG

PS - If you're posting here you should be using:
        error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!


#5 insidus

insidus

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationEngland

Posted 02 July 2013 - 02:40 PM   Best Answer

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'];



#6 badgerchap

badgerchap

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 02 July 2013 - 02:59 PM

Excellent, I will have a fiddle :) Many thanks. I will report back!



#7 badgerchap

badgerchap

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 02 July 2013 - 03:04 PM

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!



#8 insidus

insidus

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationEngland

Posted 02 July 2013 - 03:06 PM

Not a problem, glad I could help :) Can you set this thread as 'answered'? 

 

/Insidus



#9 ginerjm

ginerjm

    Advanced Member

  • Members
  • PipPipPip
  • 1,912 posts
  • LocationVoorheesville NY

Posted 02 July 2013 - 03:07 PM

Add another row to your table, output a '&nbsp' character to all the td elements except where you want to the total, then close the row and the table.


JG

PS - If you're posting here you should be using:
        error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!


#10 badgerchap

badgerchap

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 02 July 2013 - 03:36 PM

Think I've marked it as such. Thanks also ginerjm, will try that too after dinner ;)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com