Jump to content

How to get total value result from a column


shmideo
Go to solution Solved by Barand,

Recommended Posts

I get all the results displayed but how can I echo the total value for for all rows found for under column 'duration' which only contains numbers (seconds).

 

$query = "SELECT * FROM asterisk_cdr WHERE calldate LIKE '%$calldate%' AND channel LIKE '%$channel%' AND duration";

$result = mysqli_query($dbcon, $query) or die('Error getting data');
$num_rows = mysqli_num_rows($result);

Link to comment
Share on other sites

"AND duration"  ???  What is this?

 

And - where is the rest of the code?

 

If you just want a total of duration, add that to the query.  Or, if you are going to browse thru the results in a loop, you could just accumulate the value as you go and then output it at the end of the loop (after).

Link to comment
Share on other sites

My question pertained to the fact that your query, as written and posted, would be broken.  I know it's a column.  Do you know that it's a bad query statement?

 

Do you have any more code to show us or did you look up how to make a query calculate a sum for you?

Link to comment
Share on other sites

The query works, but yes I would prefer to know the proper way. This is the code I've been working on (with help from the guys on this forum) for for the past few days!

error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');  

if (isset($_POST['submitted'])) {
	
include('connect1.php');

	if (!empty($_POST['calldate'])) {
		$calldate = mysqli_real_escape_string ($dbcon, $_POST['calldate']);
	} else {
		$calldate = FALSE;
		echo '<p class="error">ha ha, you have not entered a date!</p>';
	}
	
$channel = $_POST['channel'];
$submitted = $_POST['submitted'];
$duration = 'duration';

If($calldate){
$query = "SELECT * FROM asterisk_cdr WHERE calldate LIKE '%$calldate%' AND channel LIKE '%$channel%' AND duration";

$result = mysqli_query($dbcon, $query) or die('Error getting data');
$num_rows = mysqli_num_rows($result);

echo " '$num_rows' calls found for '$calldate'";
echo "<br/>";

echo "<table width='300px'>";
echo "<tr> <th>Time of Call: Call Duration</th>";

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
	
	echo "<table width='400px'>";
	echo "<tr><td>";	
		
	echo $row['calldate']. "   (". $row['duration'] . " sec)" ;
	
	echo "</td></table>";
	
		echo "</td>";


}
Link to comment
Share on other sites

Your query ends with " and duration".  The only thing I see there is that it evaluates duration to 'TRUE' and basically anything with a non-zero duration is selected.  What are you trying to do with that clause tother than confuse others?

 

As for your output - you are generating a table inside of a table for EVERY row of the results.  Why?

 

To get your total, set a var to 0 and then in the loop accumulate the duration into that var each time thru the loop.  When the loop ends, output that var .

Edited by ginerjm
Link to comment
Share on other sites

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.