News:

Wondering if this will always be free?  See why free is better.

Main Menu

Problem with loops... How to make a week at a glance

Started by Les Mackenzie, September 17, 2004, 02:31:11 PM

Previous topic - Next topic

Les Mackenzie

Well without preamble here it is...

The Challenge: I am creating a script that once a date is selected selects the first day and 4 days after in individual columns (ie. $fday=13 $eday=$fday+5)...  After the columns are created with date headers I want to query the database and match all the columns with activities entered.  Basically a 5 day calendar listing activities for each day.

What I've Tried...

a 'for' loop to count the 5 days needed.
followed by a while loop 'while($fday < $eday)' to generate the dates and columns
within the while loop I've created a query that selects the data based on the current column ($fromdate) determined by the counter.

Here's the code...

function generate_managers_report(){

global 
$tvar$database;

echo 
'<table><tr>';
	
	

$fmonth '09';
$day 13;
$fyear 2004;
$fday $day+5;

while(
$day $fday)
	
{
	
$fromdate $fmonth.'/'.$day++.'/'.$fyear;
	
$query "SELECT 
	
  `client_dir`.`company`,
	
  `user_dir`.`f_name`,
	
  `user_dir`.`l_name`,
	
  `int_activity`.`act_description`,
	
  `managers_act`.`comment`,
	
  `managers_act`.`date`,
	
  `AB`.`f_name` AS `f_name1`,
	
  `AB`.`l_name` AS `l_name1`
	
FROM
	
  `int_activity`
	
  INNER JOIN `managers_act` ON (`int_activity`.`act_id` = `managers_act`.`activity_code`)
	
  INNER JOIN `client_dir` ON (`managers_act`.`CID` = `client_dir`.`CID`)
	
  INNER JOIN `user_dir` ON (`managers_act`.`user_id` = `user_dir`.`user_id`)
	
  INNER JOIN `user_dir` `AB` ON (`managers_act`.`user_logged` = `AB`.`user_id`)
                  WHERE date = 
$fromdate";
	
$result mysql_db_query($database$query) or die ("Error in query: $query.".mysql_error());
	
$tvar['report'] = array();

	
while (
$row mysql_fetch_assoc($result))
        {
	
$tvar['report'][] = array(
        
	
	
'company' => $row['company'],
        
	
	
'date' => $row['date'],
        
	
	
'comment' => $row['comment'],
        
	
	
'firstname' => $row['f_name'],
        
	
	
'lastname' => $row['l_name'],
        
	
	
'description' => $row['act_description']
        
	
);
        }
foreach (
$tvar['report'] as $tiles)
{
	
	
echo 
'<td>';
	
	
echo 
'<table border="0" width="100%" id="table1" class="tborder">
	
	
	
<tr>
	
	
	
	
<td colspan="2">'
$fromdate ,'</td>
	
	
	
</tr>
	
	
	
<tr bgcolor="#EEEEEE">
	
	
	
	
<td>'
$tiles[company] ,'</td>
	
	
	
	
<td>'
$tiles[firstname],' '$tiles[lastname],'</td>
	
	
	
</tr>
	
	
	
<tr>
	
	
	
	
<td>'
$tiles[description] ,'</td>
	
	
	
	
<td>&nbsp;</td>
	
	
	
</tr>
	
	
	
<tr>
	
	
	
	
<td colspan="2">'
$tiles[comment] ,'</td>
	
	
	
</tr>
	
	
</table><br>'
;
	
	
echo 
'</td>';
}
}
echo 
'</tr></table>';
}
	



Can anyone help?  Or at least lead me in the right direction? Thanks!
BLOGS SUCK! - HERE READ MINE

Please Note:  Arguing on the internet is like running in the Special Olympics...  Even if you win you're still retarded.

[Unknown]

What does it do? (it's a lot easier to help when we know what your code is doing wrong...)

-[Unknown]

Les Mackenzie

I sent you a link via PM.  Any assistance you could give is welcome thanks:) 
BLOGS SUCK! - HERE READ MINE

Please Note:  Arguing on the internet is like running in the Special Olympics...  Even if you win you're still retarded.

[Unknown]

<?php

function generate_managers_report()
{
global $tvar$database;

echo '
<table>
<tr>'
;

$fmonth '09';
$day 13;
$fyear 2004;

for ($fday $day 5$day $fday$day++)
{
$fromdate $fyear '-' $fmonth '-' $day;
$displaydate $fmonth '/' $day '/' $fyear;

$result mysql_db_query($database"
SELECT
cdir.company, udir.f_name, udir.l_name, iact.act_description,
mact.comment, mact.date, udir2.f_name AS f_name1, udir2.l_name AS l_name1
FROM int_activity AS iact
INNER JOIN managers_act AS mact ON (iact.act_id = mact.activity_code)
INNER JOIN client_dir AS cdir ON (mact.CID = cdir.CID)
INNER JOIN user_dir AS udir1 ON (mact.user_id = udir1.user_id)
INNER JOIN user_dir AS udir2 ON (mact.user_logged = udir2.user_id)
WHERE mact.date = '
$fromdate'") or die ('Error in query: ' $query ' in ' __FILE__ ' on line ' __LINE__ '. ' mysql_error());
$tvar['report'] = array();
while ($row mysql_fetch_assoc($result))
{
$tvar['report'][] = array(
        
'company' => $row['company'],
        
'date' => $row['date'],
        
'comment' => $row['comment'],
        
'firstname' => $row['f_name'],
        
'lastname' => $row['l_name'],
        
'description' => $row['act_description']
         );
        
}

echo '
<td>'
$displaydate'</td>';

foreach ($tvar['report'] as $tiles)
{
echo '
<td>
<table border="0" width="100%" id="table1" class="tborder">
<tr style="background-color: #EEEEEE;">
<td>'
$tiles['company'], '</td>
<td>'
$tiles['firstname'], ' '$tiles['lastname'], '</td>
</tr>
<tr>
<td>'
$tiles['description'], '</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2">'
$tiles[comment] ,'</td>
</tr>
</table>
<br />
</td>'
;
}
}

echo '
</tr>
</table>'
;
}

?>


Sorry, had to get home.  Had to reformat code to understand your question - is this what you meant?  I'm still not sure I understand.

-[Unknown]

Les Mackenzie

I haven't got a chance to test the above but I'll keep you posted.  Thanks [Unk] :D :D :D
BLOGS SUCK! - HERE READ MINE

Please Note:  Arguing on the internet is like running in the Special Olympics...  Even if you win you're still retarded.

Les Mackenzie

BLOGS SUCK! - HERE READ MINE

Please Note:  Arguing on the internet is like running in the Special Olympics...  Even if you win you're still retarded.

Les Mackenzie

Just to revisit this script.  I've found a bit of a problem that might be solved by the community, since I can't get it to work.  The above script creates a week, at a glance and all activity within the selected week.  Problem is, if I click the 31st of the month the month continues on to create 32, 33, 34 and so on instead of generating the new days 01,02,03 etc.

I use a for loop to create the columns and dates (after the initial date is selected) so how would I make the desired effect present itself?  Thanks in advance.

[php]
for ($fday = $day + 5; $day < $fday; $day++)
{


Cheers,

Les
BLOGS SUCK! - HERE READ MINE

Please Note:  Arguing on the internet is like running in the Special Olympics...  Even if you win you're still retarded.

[Unknown]

Maybe try, after that:

$realday = $day > $number_of_days_in_this_month ? $day % $number_of_days_in_this_month : $day;

(% means "mod" or the remainder after division - for example, 32 % 31 = 1... but 31 % 31 = 0.)

-[Unknown]

Les Mackenzie

Thanks [Unknown] but I think I overthought the problem.  Here's what I used to generate the date in the end...


for ($fday = $day + 5; $day < $fday; $day++)
{  
$display = date("M-d-Y", mktime(0, 0, 0, $month, $day, $year));
$fromdate = date("m/d/Y", mktime(0, 0, 0, $month, $day, $year));


D'oh! :P
BLOGS SUCK! - HERE READ MINE

Please Note:  Arguing on the internet is like running in the Special Olympics...  Even if you win you're still retarded.

Advertisement: