Jump to content

Recommended Posts

if($_GET['module'] == "add") {
include "tpl/addtodiary.tpl";
if(isset($_GET['submit'])) {
	$result = mysql_query(sprintf("SELECT diary_duration FROM diary WHERE diary_date = '$_GET[date]'"));
	while($row = mysql_fetch_array($result)) {
		echo $row['diary_duration'];
		}
	}
}

 

This creates and array and outputs it to screen, how can I add all these results together? I'm making a booking system so I need to see if there is time available on selected day... Is there a add() function or something similar?

Link to comment
https://forums.phpfreaks.com/topic/148280-solved-mysql_fetch_array-and-counting/
Share on other sites

If I understand you correctly use:

 

$result = mysql_query(sprintf("SELECT SUM(diary_duration) FROM diary WHERE diary_date = '$_GET[date]'"));
$row = mysql_fetch_array($result)
echo $row['SUM(diary_duration)'];

 

So you think:

 

if($_GET['module'] == "add") {
include "tpl/addtodiary.tpl";
if(isset($_GET['submit'])) {
	$result = mysql_query(sprintf("SELECT diary_duration FROM diary WHERE diary_date = '$_GET[date]'"));
	$row = mysql_fetch_array($result);
	echo $row['SUM(diary_duration)'];
	}
}

 

Because that just returns a blank.

Ah, I didn't realise that that had been changed. Thanks again, you've really helped me today. :)

 

No problem.

 

Are you trying to create a loop? If so, try the foreach function.

 

He already had a while loop to display each diary_duration but he wanted the sum of all of them, and MySQL has a simple built in SUM function.

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.