Jump to content

Grouping and PHP with mysql


dvacole

Recommended Posts

Hi

 

I'm fairly noobish with both php and mysql and learning as I go. However I am running into trouble with a report I want to render.

 

My plan is to have a user select the date range that they would like the report to be for and then have then grouped by week for that date range

 

For example let's say the users wants the report for 01/01/2008 to 03/01/2008. I'm just fine pulling up the entire list with an array and displaying the whole range, but I don't know how to have it grouped by week in PHP using a while statement or if that is even the correct way of doing it.

 

I would like the end result to look somthing like this:

 

 

Title title title
date total total
date Subtotals
date total

and so one for the date range.

Link to comment
Share on other sites

I think what you wanted is something like the following, where you order by one field in the query, cycle through the results and only ouput that field if the value is different from last time the loop ran.

 


$sql ="SELECT field1,field2,field3 FROM yourtable ORDER BY field1";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$prev = '';
while($row = mysql_fetch_assoc($result)){
if($row['field1'] != $prev){//the value has changed since last time the loop ran
	echo $row['field1'].'<br />';
	$prev = $row['field1'];
}
echo $row['field2'].$row['field3'].'<br />';
}

 

Bit difficult to give you anything more than that without more information.

Link to comment
Share on other sites

Okay, I tried that and I'm getting an invalid supplied argument.

 

This is what I have:

 

$week1 = "SELECT week((date(shift.sdate))),UNIX_TIMESTAMP(date(shift.sdate)), shift.sdate, sum(fast.level1), sum(fast.level2), sum(fast.level3), sum(fast.level4), sum(fast.sales), sum(fast.redeemed) from `fast`, `shift` Where sdate between $startdate and $enddate GROUP BY date(shift.sdate)";

$result=mysql_query($week1);

while ($row = mysql_fetch_array($result))

{

$prev=$row{'week(date(shift.sdate))'};

if ($row{'week(date(shift.sdate))'}=$prev){;

        and formating info blah....//this should hopefully format the individul days that match the same week as the other//

}

else {

;

$weektotal="SELECT week((date(shift.sdate))), sum(fast.level1), sum(fast.level2), sum(fast.level3), sum(fast.level4), sum(fast.sales), sum(fast.redeemed) from `fast`, `shift` Where week(sdate) = $weeknumber group by week(sdate) LIMIT 0, 30 ";

$weektotallookup=mysql_query($weektotal);

while ($test=mysql_fetch_array($weektotallookup));{

// and this should inclide a row that has the sums fot the week.

 

 

However I'm getting an invalid arugement for $test.

 

I'm totally lost.

I've inlcuded a link to the entire code if it would help as a text file.

www.dvacole.com/famonthlyreport.txt

 

 

 

 

Link to comment
Share on other sites

You need to debug your queries. Basic idea:

 

$sql = "SELECT * FROM..."
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_assoc($result)){
        //loop through results
    }
}else{
    echo 'No results found';
}

 

Also, please try to use


tags when you post.

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.