Jump to content

SQL > PHP > Table with grouped totals.


Azyrus

Recommended Posts

Currently we have a Microsoft Access database which we use to display a report, we are rolling over to PHP/SQL online for greater control e.t.c.

 

So far we have the report pulling all the information from the database however we need to group this information by Day followed by Employee, sorted by time and then totaled for each Employee for each day.

 

Below is the code we have so far which only prints columns how do we go about displaying it like the access screenshot. Attached is an example of the Microsoft Access report we have.

 

<?php
$username='admin';
$password='password';
$database='planner';

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM reservations ORDER BY start_date, machid, starttime";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

?>

<?php
$i=0;
while ($i < $num) {
$f0=mysql_result($result,$i,"machid");
$f1=mysql_result($result,$i,"start_date");
$f2=mysql_result($result,$i,"end_date");
$f3=mysql_result($result,$i,"starttime");
$f4=mysql_result($result,$i,"endtime");
$f5=mysql_result($result,$i,"first_name");
$f6=mysql_result($result,$i,"last_name");
$f7=mysql_result($result,$i,"company");
$f8=mysql_result($result,$i,"phone_number");
$f9=mysql_result($result,$i,"site_address");
$f10=mysql_result($result,$i,"site_suburb");
$f11=mysql_result($result,$i,"site_postcode");
$f12=mysql_result($result,$i,"postal_address");
$f13=mysql_result($result,$i,"postal_suburb");
$f14=mysql_result($result,$i,"postal_postcode");
$f15=mysql_result($result,$i,"job_type");
$f16=mysql_result($result,$i,"payment_type");
$f17=mysql_result($result,$i,"payment_status");
$f18=mysql_result($result,$i,"quote_outcome");
$f19=mysql_result($result,$i,"quote_amount");
$f20=mysql_result($result,$i,"quote_employee");
$f21=mysql_result($result,$i,"job_income");
$f22=mysql_result($result,$i,"job_materials");
$f23=mysql_result($result,$i,"invoice_number");
$f24=mysql_result($result,$i,"job_notes");

/* START TIME */
$stime = $f3;

$shour = floor($stime/60);
$smin = str_pad($stime % 60,2,0,STR_PAD_LEFT);

//echo $hour.':'.$min;
//echo '<br><br>';

// For AM/PM
$shalf = 'am';
if( $shour > 12 ) {
$shalf = 'pm';
$shour -= 12;
}

$stime = $shour.':'.$smin.' '.$shalf;

/* END TIME */
$etime = $f4;

$ehour = floor($etime/60);
$emin = str_pad($etime % 60,2,0,STR_PAD_LEFT);

//echo $hour.':'.$min;
//echo '<br><br>';

// For AM/PM
$ehalf = 'am';
if( $ehour > 12 ) {
$ehalf = 'pm';
$ehour -= 12;
}

$etime = $ehour.':'.$emin.' '.$ehalf;

?>
<table width="3500px" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="125px"><?php echo $f0; ?></td>
<td width="80px"><?php echo date('d-m-y', "$f1"); ?></td>
<td width="50px" style="text-align:right;"><?php echo $stime; ?></td>
<td width="50px" style="text-align:right; padding-right:20px;"><?php echo $etime; ?></td>
<td width="100px"><?php echo $f5; ?></td>
<td width="100px"><?php echo $f6; ?></td>
<td width="100px"><?php echo $f7; ?></td>
<td width="100px"><?php echo $f8; ?></td>
<td width="100px"><?php echo $f9; ?></td>
<td width="100px"><?php echo $f10; ?></td>
<td width="100px"><?php echo $f11; ?></td>
<td width="100px"><?php echo $f12; ?></td>
<td width="100px"><?php echo $f13; ?></td>
<td width="100px"><?php echo $f14; ?></td>
<td width="100px"><?php echo $f15; ?></td>
<td width="100px"><?php echo $f16; ?></td>
<td width="100px"><?php echo $f17; ?></td>
<td width="100px"><?php echo $f18; ?></td>
<td width="100px"><?php echo $f19; ?></td>
<td width="100px"><?php echo $f20; ?></td>
<td width="100px"><?php echo $f21; ?></td>
<td width="100px"><?php echo $f22; ?></td>
<td width="100px"><?php echo $f23; ?></td>
<td width="100px"><?php echo $f24; ?></td>
<?php
$i++;
}
?>

 

Once again thanks everyone at phpfreaks!

post-133508-13482403473345_thumb.gif

Link to comment
Share on other sites

SHOW CREATE TABLE reservations *thanks kicken :)

Table 	Create Table 
reservations	CREATE TABLE `reservations` (
  `resid` char(16) NOT NULL,
  `machid` char(16) NOT NULL,
  `scheduleid` char(16) NOT NULL,
  `start_date` int(11) NOT NULL DEFAULT '0',
  `end_date` int(11) NOT NULL DEFAULT '0',
  `starttime` int(11) NOT NULL,
  `endtime` int(11) NOT NULL,
  `created` int(11) NOT NULL,
  `modified` int(11) DEFAULT NULL,
  `parentid` char(16) DEFAULT NULL,
  `is_blackout` smallint(6) NOT NULL DEFAULT '0',
  `is_pending` smallint(6) NOT NULL DEFAULT '0',
  `summary` text,
  `allow_participation` smallint(6) NOT NULL DEFAULT '0',
  `allow_anon_participation` smallint(6) NOT NULL DEFAULT '0',
  `environment` char(50) DEFAULT NULL,
  `last_name` char(50) DEFAULT NULL,
  `company` char(50) DEFAULT NULL,
  `phone_number` char(50) DEFAULT NULL,
  `site_address` char(50) DEFAULT NULL,
  `site_suburb` char(50) DEFAULT NULL,
  `site_postcode` char(50) DEFAULT NULL,
  `postal_address` char(50) DEFAULT NULL,
  `postal_suburb` char(50) DEFAULT NULL,
  `postal_postcode` char(50) DEFAULT NULL,
  `job_type` char(50) DEFAULT NULL,
  `payment_type` char(50) DEFAULT NULL,
  `payment_status` char(50) DEFAULT NULL,
  `job_source` char(50) DEFAULT NULL,
  `quote_outcome` char(50) DEFAULT NULL,
  `quote_amount` char(50) DEFAULT NULL,
  `quote_employee` char(50) DEFAULT NULL,
  `job_income` char(50) DEFAULT NULL,
  `job_materials` char(50) DEFAULT NULL,
  `invoice_number` char(50) DEFAULT NULL,
  `job_notes` char(50) DEFAULT NULL,
  PRIMARY KEY (`resid`),
  KEY `res_machid` (`machid`),
  KEY `res_scheduleid` (`scheduleid`),
  KEY `reservations_startdate` (`start_date`),
  KEY `reservations_enddate` (`end_date`),
  KEY `res_startTime` (`starttime`),
  KEY `res_endTime` (`endtime`),
  KEY `res_created` (`created`),
  KEY `res_modified` (`modified`),
  KEY `res_parentid` (`parentid`),
  KEY `res_isblackout` (`is_blackout`),
  KEY `reservations_pending` (`is_pending`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Link to comment
Share on other sites

Currently we have a Microsoft Access database which we use to display a report, we are rolling over to PHP/SQL online for greater control e.t.c.

 

So far we have the report pulling all the information from the database however we need to group this information by Day followed by Employee, sorted by time and then totaled for each Employee for each day.

 

Below is the code we have so far which only prints columns how do we go about displaying it like the access screenshot. Attached is an example of the Microsoft Access report we have.

 

<?php
$username='admin';
$password='password';
$database='planner';

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM reservations ORDER BY start_date, machid, starttime";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

?>

<?php
$i=0;
while ($i < $num) {
$f0=mysql_result($result,$i,"machid");
$f1=mysql_result($result,$i,"start_date");
$f2=mysql_result($result,$i,"end_date");
$f3=mysql_result($result,$i,"starttime");
$f4=mysql_result($result,$i,"endtime");
$f5=mysql_result($result,$i,"first_name");
$f6=mysql_result($result,$i,"last_name");
$f7=mysql_result($result,$i,"company");
$f8=mysql_result($result,$i,"phone_number");
$f9=mysql_result($result,$i,"site_address");
$f10=mysql_result($result,$i,"site_suburb");
$f11=mysql_result($result,$i,"site_postcode");
$f12=mysql_result($result,$i,"postal_address");
$f13=mysql_result($result,$i,"postal_suburb");
$f14=mysql_result($result,$i,"postal_postcode");
$f15=mysql_result($result,$i,"job_type");
$f16=mysql_result($result,$i,"payment_type");
$f17=mysql_result($result,$i,"payment_status");
$f18=mysql_result($result,$i,"quote_outcome");
$f19=mysql_result($result,$i,"quote_amount");
$f20=mysql_result($result,$i,"quote_employee");
$f21=mysql_result($result,$i,"job_income");
$f22=mysql_result($result,$i,"job_materials");
$f23=mysql_result($result,$i,"invoice_number");
$f24=mysql_result($result,$i,"job_notes");

/* START TIME */
$stime = $f3;

$shour = floor($stime/60);
$smin = str_pad($stime % 60,2,0,STR_PAD_LEFT);

//echo $hour.':'.$min;
//echo '<br><br>';

// For AM/PM
$shalf = 'am';
if( $shour > 12 ) {
$shalf = 'pm';
$shour -= 12;
}

$stime = $shour.':'.$smin.' '.$shalf;

/* END TIME */
$etime = $f4;

$ehour = floor($etime/60);
$emin = str_pad($etime % 60,2,0,STR_PAD_LEFT);

//echo $hour.':'.$min;
//echo '<br><br>';

// For AM/PM
$ehalf = 'am';
if( $ehour > 12 ) {
$ehalf = 'pm';
$ehour -= 12;
}

$etime = $ehour.':'.$emin.' '.$ehalf;

?>
<table width="3500px" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="125px"><?php echo $f0; ?></td>
<td width="80px"><?php echo date('d-m-y', "$f1"); ?></td>
<td width="50px" style="text-align:right;"><?php echo $stime; ?></td>
<td width="50px" style="text-align:right; padding-right:20px;"><?php echo $etime; ?></td>
<td width="100px"><?php echo $f5; ?></td>
<td width="100px"><?php echo $f6; ?></td>
<td width="100px"><?php echo $f7; ?></td>
<td width="100px"><?php echo $f8; ?></td>
<td width="100px"><?php echo $f9; ?></td>
<td width="100px"><?php echo $f10; ?></td>
<td width="100px"><?php echo $f11; ?></td>
<td width="100px"><?php echo $f12; ?></td>
<td width="100px"><?php echo $f13; ?></td>
<td width="100px"><?php echo $f14; ?></td>
<td width="100px"><?php echo $f15; ?></td>
<td width="100px"><?php echo $f16; ?></td>
<td width="100px"><?php echo $f17; ?></td>
<td width="100px"><?php echo $f18; ?></td>
<td width="100px"><?php echo $f19; ?></td>
<td width="100px"><?php echo $f20; ?></td>
<td width="100px"><?php echo $f21; ?></td>
<td width="100px"><?php echo $f22; ?></td>
<td width="100px"><?php echo $f23; ?></td>
<td width="100px"><?php echo $f24; ?></td>
<?php
$i++;
}
?>

 

Once again thanks everyone at phpfreaks!

 

use GROUP BY http://www.w3schools.com/sql/sql_groupby.asp

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.