Jump to content

help organizing mysql array data with foreach


samoht

Recommended Posts

hello,

 

I have a simple form that retrieves data from a table by an interval set by the user:

<form name="form1" action="invoiceForm.php" method="POST">  
<input type="text" name="interval" id="interval" size="30">
<input type="submit" value="Submit" id="submit">
</form>
<?php
$days = $_POST['interval'];
if($_POST['interval']){
$q = mysql_query('SELECT * FROM jos_timesheet WHERE DATE_SUB(CURDATE(), INTERVAL '.$days. ' DAY)') or die (mysql_error()); 

while($row = mysql_fetch_array( $q )){
// I need a foreach here that will separate the clients 
echo "Name: ".$row['ts_client'];
echo " Date: ".$row['ts_created'];
}
}
?>

 

I need to separate the clients and display my data in a separte table for each client within the time range.

 

How do I write the foreach ??

Link to comment
Share on other sites

If you would post an example of your data and what you want the results to be it would help.

 

But, you need to start by ordering the data the way you want. Add an ORDER BY clause to your query -

 

ORDER BY ts_client, ts_created

 

This will cause the rows in the result set to be ordered (grouped) by the ts_client and then by the ts_created within each ts_client. Then you simply output the results.

Link to comment
Share on other sites

here is what I have so far:

<?php
$days = $_POST['interval'];
if($_POST['interval']){
$q = mysql_query('SELECT * FROM jos_chronoforms_timesheet2 WHERE DATE_SUB(CURDATE(), INTERVAL '.$days. ' DAY) ORDER BY ts_client, ts_created') or die (mysql_error()); 

while($row = mysql_fetch_array( $q )){
$clients = array($row['ts_client']);
foreach($clients as $client) { 
echo "<h3>SIGN/OFF CLIENT: ".$row['ts_client']."</h3>\n";
echo '
<table border=0 cellpadding=0 cellspacing=0 width=97% style="border-collapse: collapse;table-layout:fixed;width:53em">
	<tr height=20 bgcolor="#555555;"><th>Date:</th><th>Time:</th><th>Description:</th></tr>';
	for($i=0;$i<count($client);$i++){
		echo '
			<tr>
				<td>'.$client['ts_created'].'</td><td>'.$client['ts_timeout'] - $client['ts_timein'].'</td><td>'.$client['ts_dscr'].'</td>
			</tr>
		</table>';
	}
}
}
}
?>

 

but this spits out:

 

<h3>SIGN/OFF CLIENT: Marclazar.com</h3>

 

<table border=0 cellpadding=0 cellspacing=0 width=97% style="border-collapse: collapse;table-layout:fixed;width:53em">

<tr height=20 bgcolor="#555555;"><th>Date:</th><th>Time:</th><th>Description:</th></tr>0</td><td>M</td>

</tr>

 

</table><h3>SIGN/OFF CLIENT: Surgimap.com</h3>

 

<table border=0 cellpadding=0 cellspacing=0 width=97% style="border-collapse: collapse;table-layout:fixed;width:53em">

<tr height=20 bgcolor="#555555;"><th>Date:</th><th>Time:</th><th>Description:</th></tr>0</td><td>S</td>

</tr>

</table>

 

My table data is simple

CREATE TABLE IF NOT EXISTS `jos_chronoforms_timesheet2` (
  `cf_id` int(11) NOT NULL auto_increment,
  `uid` varchar(255) NOT NULL,
  `recordtime` text NOT NULL,
  `ipaddress` text NOT NULL,
  `cf_user_id` text NOT NULL,
  `ts_timein` time NOT NULL,
  `ts_timeout` time NOT NULL,
  `ts_client` varchar(255) NOT NULL,
  `ts_dscr` varchar(255) NOT NULL,
  `ts_created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`cf_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

 

does this make sense what I am trying to achieve??

Link to comment
Share on other sites

Sorry I missed that you wanted an example of the output I am looking for.

 

I want something like this:

<?php 
<h3>SIGN/OFF CLIENT: Marclazar.com</h3>

<table border=0 cellpadding=0 cellspacing=0 width=97% style="border-collapse: collapse;table-layout:fixed;width:53em">
	<tr height=20 bgcolor="#555555;">
		<th>Date:</th><th>Time:</th><th>Description:</th>

	</tr>
	<tr>
		<td>10/20/2008</td><td>1:12:00</td><td>Work for Marc Lazar</td>
	</tr>
</table>

<h3>SIGN/OFF CLIENT: mywebpage.com</h3>

<table border=0 cellpadding=0 cellspacing=0 width=97% style="border-collapse: collapse;table-layout:fixed;width:53em">
	<tr height=20 bgcolor="#555555;">
		<th>Date:</th><th>Time:</th><th>Description:</th>

	</tr>
	<tr>
		<td>10/20/2008</td><td>2:42:00</td><td>Work for mywebpage</td>
	</tr>
	<tr>
		<td>10/21/2008</td><td>0:38:00</td><td>More work for mywebpage</td>
	</tr>
	<tr>
		<td>10/21/2008</td><td>1:02:00</td><td>Even more work for mywebpage</td>
	</tr>
</table>

ect..

 

This seems straight forward but I am having problems organizing my data by client and understanding how to loop through word done for each client.

 

Thanks for any help!

Link to comment
Share on other sites

here's the logic in pseudocode

 

prevClient = '';                 // store the previous client, blank to start with

while (fetch next row)

    if (client != prevClient)              // have we a new client?
    {
          if (prevClient != '')
              close previous table

          print client name
          start new table

          prevClient = client.                // reset last client
    }
     
     output detail row.
}
close last table

Link to comment
Share on other sites

Thanks Barand,

 

One question: how do I perform math on the repeating results?

 

when I have:

<?php
$prevclient = '';
while($row = mysql_fetch_array( $q )){
	if($row['ts_client'] != $prevclient){
		if($prevclient != '') {
			echo '</table>'."\n";
		}
		echo "\n<h3>SIGN/OFF CLIENT: ".$row['ts_client']."</h3>\n";
		echo '
		<table border=0 cellpadding=0 cellspacing=0 width=97% style="border-collapse: collapse;table-layout:fixed;width:53em">
			<tr height=20 bgcolor="#555555;">
				<th>Date:</th><th>Time:</th><th>Description:</th>
			</tr>';
		$prevclient = $row['ts_client'];
	}
	echo '
		<tr>
			<td>'.$row['ts_created'].'</td><td>'.$row['ts_timeout'] - $row['ts_timein'].'</td><td>'.$row['ts_dscr'].'</td>
		</tr>';
}
echo '</table>'."\n";

 

my output gets all messed up because of the $row['ts_timeout'] - $row['ts_timein']

but I will need to subtract these to get the total time per entry??

 

Any Thoughts?

Link to comment
Share on other sites

OPPS -

 

I realize I need a time format so I changed it to

 

		echo '
		<tr>
			<td>'.$row['ts_created'].'</td><td>'.date('h:i:s',$row['ts_timeout'] - $row[ts_timein]) .'</td><td>'.$row['ts_dscr'].'</td>
		</tr>';

 

the problem is I get the same result for every row?? (always the first time in/ time out )

 

Link to comment
Share on other sites

You could also select the timediff in seconds then total those. Divide it by 3600 at the end to get the total hours

 

SELECT ... TIMEDIFF(ts_timeout, ts_timein) as totaltime,

      TIME_TO_SEC(TIMEDIFF(ts_timeout, ts_timein)) as totalsecs, ...

 

The pseudocode then becomes

prevClient = '';                 // store the previous client, blank to start with

while (fetch next row)

    if (client != prevClient)              // have we a new client?
    {
          if (prevClient != '')
              close previous table

          print client name
          start new table

          prevClient = client.                // reset last client
    }
     
     output detail row.
     grandtotal += totalsecs
}
close last table
print grandtotal/3600

Link to comment
Share on other sites

I see,

 

However, I need the total seconds for each client?

 

also I need the format to be HH:MM:SS  - but when I try:

<?php
$q = mysql_query('SELECT TIMEDIFF(ts_timeout, ts_timein) as totaltime, TIME_TO_SEC(TIMEDIFF(ts_timeout, ts_timein)) as totalsecs, ts_client, ts_dscr, ts_created FROM jos_chronoforms_timesheet2 WHERE DATE_SUB(CURDATE(), INTERVAL '.$days. ' DAY) ORDER BY ts_client, ts_created') or die (mysql_error()); 

$prevclient = '';
while($row = mysql_fetch_assoc( $q )){
	if($row['ts_client'] != $prevclient){
		if($prevclient != '') {
			echo '
		</table>'."\n";
		}
		echo "\n<h3>SIGN/OFF CLIENT: ".$row['ts_client']."</h3>\n";
		echo '
		<table border=0 cellpadding=0 cellspacing=0 width=97% style="border-collapse: collapse;table-layout:fixed;width:53em">
			<tr height=20 bgcolor="#555555;">
				<th>Date:</th><th>Time:</th><th>Description:</th>
			</tr>';
		$prevclient = $row['ts_client'];
	}
	echo '
			<tr>
				<td>'.$row['ts_created'].'</td><td>'.$row['totaltime'].'</td><td>'.$row['ts_dscr'].'</td>
			</tr>';
	$gt += $row['totalsecs'];
}
echo '
</table>'. date('H:i:s',$gt/3600) ."\n";

I get 19:00:00 as a grand total?? (which is not the total)

Link to comment
Share on other sites

OK - I figured out that gmdate("H:i:s", $gt) gives me the format that I need

 

But I still am not sure how to get a total for each client.

If I place the code just after the first close of table then I will get the totals for each client except the last one??

 

also - using gmdate() I don't have to divide by 3600.

Link to comment
Share on other sites

Thanks very much for the help.

 

I think I have the form set up mostly the way I want it to be. However, I realize that I don't know how I would mail the results once creating this nice invoice page?

 

Is there a way to put the results in a form of its own -> add hidden inputs an a "mail Results" submit button?

 

The difficulty I am having is that my HTML will have several clients info So I would need to loop through the $_POST in a similar way as I looped through the $row  - but how can I do That??

 

Any ideas?

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.