Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display Days of Month in rows 1

Status
Not open for further replies.

pastorandy

IS-IT--Management
Nov 2, 2006
84
GB
Hi
I have an application that lists rows of jobs by date.
What I'd like to do is list them in the same manner but to have a row shown if there is no job on that date but to still have the date displayed at the beginning of the row so that it can be quickly seen that a job can be added in for that day.

Any ideas?
 
sounds easy. what shape is the array or recordset of jobs to dates?
 
Hi

The job is unassigned until it has a date entered for it. The date then is inserted into the job record in the job table.

Code:
<? 
$last_date='';
$style=0;

while($row = mysql_fetch_array($result)) {

     if ($last_date!=$row[job_date]) {
         $last_date=$row[job_date];
         $style=1-$style;
         }

		  
 echo "<tr class=\"row$style\">";
 ?>

I'm doing this to display each row with a different date in an alternate colour at present.
 
instead of just iterating though the recordset and printing the results, first transform the recordset so that it is shaped with the job date as the key. if there is more than one job per day then have the array as mutidimensional

Code:
$jobs[date][] = "something";
$jobs[date][] = "something else";

then, in your output function, iterate through the days and for each day test whether there is anything in the $jobs[date] array and output it. if there is nothing then you have your blank row.
 
how would I transform the recordset? Have you an example?
 
can you show me the shape of the recordset? perhaps post your query?
 
My query is straightforward
Code:
$query= "SELECT * from job
WHERE job_completed = 'No'
ORDER BY job_date asc
";

then output

Code:
<? 
$last_date='';
$style=0;


while($row = mysql_fetch_array($result)) {

     if ($last_date!=$row[job_date]) {
         $last_date=$row[job_date];
         $style=1-$style;
         }

		  
 echo "<tr class=\"row$style\">";
 ?>

 
i was rather hoping for the fieldnames to be present!

the code assumes that the job description field is called "jobDescription"


Code:
<?
$query= "
			SELECT 
				* from job
			WHERE 
				job_completed = 'No'
			ORDER BY 
				job_date asc";

$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
	$jobs[$row['job_date'][]=$row['jobDescription'];
}

$dates = array_keys($jobs);
$curDate = strtotime($dates[0]);
$end = strtotime($dates[count($dates)-1]);
$dates = null;

echo <<<HTML
<table>
	<tr>
		<th>Date</th>
		<th>Job Description</th>
	</tr>

HTML;

$style = "style1";
while ($curDate <= $end){
	$displayDate = date ("Y-m-d", $curDate);
	if (isset($jobs[$displayDate])){
		$curJobs = $jobs[$displayDate];
	}else{
		$curjobs = array ("&nbsp;"); //to create the blank row
	}
	foreach ($curjobs as $job){
	
		echo <<<HTML
	<tr class="$style">
		<td>$displayDate</td>
		<td>{$job['jobDescription']}</td>
	</tr>

HTML;
	} //end of foreach loop
	$style = ($style=="style1") ? "style2" : "style1";
	$curDate = strtotime("+1 day", $curDate);
} //end of while loop

echo "</table>";
?>
 
oops. spotted a couple of typos

Code:
<?
$query= "
			SELECT 
				* from job
			WHERE 
				job_completed = 'No'
			ORDER BY 
				job_date asc";

$result = mysql_query($query);
$jobs = array();
while ($row = mysql_fetch_assoc($result)){
	$jobs[$row['job_date']][]=$row['jobDescription'];
}

$dates = array_keys($jobs);
$curDate = strtotime($dates[0]);
$end = strtotime($dates[count($dates)-1]);
$dates = null;

echo <<<HTML
<table>
	<tr>
		<th>Date</th>
		<th>Job Description</th>
	</tr>

HTML;

$style = "style1";
while ($curDate <= $end){
	$displayDate = date ("Y-m-d", $curDate);
	if (isset($jobs[$displayDate])){
		$curJobs = $jobs[$displayDate];
	}else{
		$curJobs = array ("&nbsp;"); //to create the blank row
	}
	foreach ($curjobs as $job){
	
		echo <<<HTML
	<tr class="$style">
		<td>$displayDate</td>
		<td>{$job['jobDescription']}</td>
	</tr>

HTML;
	} //end of foreach loop
	$style = ($style=="style1") ? "style2" : "style1";
	$curDate = strtotime("+1 day", $curDate);
} //end of while loop

echo "</table>";
?>
 
think i'll paste the code from my page as this looks tricky!
 
Here's my table headers:
<th align="center">Status</th>
<th align="center">Job Date</th>
<th align="left">Time Slot</th>
<th align="left">Customer</th>
<th align="left">Desc</th>
<th align="left">Location</th>
<th align="center">Type</th>
<th align="center">Allocated</th>
<th align="center">Free</th>
<th align="center">Actions</th>

here's my columns
job_colour
job_date
job_time_slot
cust_surname
job_desc
job_location
job_type
<? include ("installer.php");?>
<? include ("free.php");?>
some action buttons here
 
not really sure what the two included files do, so I've just assumed they are dynamically generated content to go into table cells.

Code:
<?
$query= "
			SELECT 
				job_colour,
				job_date,
				job_time_slot,
				cust_surname,
				job_desc,
				job_location,
				job_type 
			from 
				job
			WHERE 
				job_completed = 'No'
			ORDER BY 
				job_date asc";

$result = mysql_query($query);
$jobs = array();
while ($row = mysql_fetch_assoc($result)){
	$tmp = $row;
	unset($tmp[job_date]);
	$jobs[$row['job_date']][]=$tmp;
}

$dates = array_keys($jobs);
if (count($dates) === 0){
$curDate = $end = time();
} else {
	$curDate = strtotime($dates[0]);
	$end = strtotime($dates[count($dates)-1]);
}
$dates = null;

echo <<<HTML
<table>
	<tr>
		<th align="center">Status</th>
		<th align="center">Job Date</th>
		<th align="left">Time Slot</th>
		<th align="left">Customer</th>
		<th align="left">Desc</th>
		<th align="left">Location</th>
		<th align="center">Type</th>
		<th align="center">Allocated</th>
		<th align="center">Free</th>
		<th align="center">Actions</th>
	</tr>

HTML;

$style = "style1";
while ($curDate <= $end){
	$displayDate = date ("Y-m-d", $curDate);
	if (isset($jobs[$displayDate])){
		$curJobs = $jobs;
		$allocation = getContent("installer.php");
		$free = getContent("free.php");
	}else{
		$curJobs['job_colour'] =
		$curJobs['job_time_slot'] =
		$curJobs['cust_surname'] =
		$curJobs['job_desc'] =
		$curJobs['job_location'] =
		$curJobs['job_type'] = 
		$allocation = 
		$free = " ";
	}
	foreach ($curJobs as $job){
		echo <<<HTML
	<tr class="$style">
		<td>{$job['job_colour']}</td>
		<td>$displayDate</td>
		<td>{$job['job_time_slot']}</td>
		<td>{$job['cust_surname']}</td>
		<td>{$job['job_desc']}</td>
		<td>{$job['job_location']}</td>
		<td>{$job['job_type']}</td>
		<td>$allocation</td>
		<td>$free</td>
		<td>PUT SOME BUTTONS HERE</td>
	</tr>

HTML;
	} //end of foreach loop
	$style = ($style=="style1") ? "style2" : "style1";
	$curDate = strtotime("+1 day", $curDate);
} //end of while loop

echo "</table>";

function getContents($file){
	ob_start();
	include $file;
	$contents = ob_get_contents();
	ob_end_clean();
	return $contents;
}
?>
 
JP Thanks for your help with this. I'll give this a go and post back!
 
JP the Free and Allocated files bring in content from other queries. Will it work if they are just included?
 
without seeing the queries and the files, i could not tell you.

if they worked with simple includes previously, then they should work now.
 
JP
>>
Fatal error: Call to undefined function: getcontent()
<<

Code:
  $allocation = getContent("installer.php");
 
done some testing.

please replace lines 55-68 with the following
Code:
	if (isset($jobs[$displayDate])){
		$curJobs = $jobs[$displayDate];
		$allocation = getContent("installer.php");
		$free = getContent("free.php");
	}else{
		$curJobs[0]['job_colour'] =
		$curJobs[0]['job_time_slot'] =
		$curJobs[0]['cust_surname'] =
		$curJobs[0]['job_desc'] =
		$curJobs[0]['job_location'] =
		$curJobs[0]['job_type'] = 
		$allocation = 
		$free = " ";
	}

and change the function at the bottom of the code posted above to getContent instead of getContents
 
JP
In my include files I reference my variable like this:
>>>
WHERE job_id = $row[job_id]
<<<

How would I reference variables now?

 
umm.. damn

ok - i'm going to repost the whole code
Code:
<?
$query= "
			SELECT 
				job_colour,
				job_date,
				job_time_slot,
				cust_surname,
				job_desc,
				job_location,
				job_type 
			from 
				job
			WHERE 
				job_completed = 'No'
			ORDER BY 
				job_date asc";

$result = mysql_query($query);
$jobs = array();
while ($row = mysql_fetch_assoc($result)){
	$tmp = $row;
	unset($tmp[job_date]);
	$jobs[$row['job_date']][]=$tmp;
}

$dates = array_keys($jobs);
if (count($dates) === 0){
$curDate = $end = time();
} else {
	$curDate = strtotime($dates[0]);
	$end = strtotime($dates[count($dates)-1]);
}
$dates = null;

echo <<<HTML
<table>
	<tr>
		<th align="center">Status</th>
		<th align="center">Job Date</th>
		<th align="left">Time Slot</th>
		<th align="left">Customer</th>
		<th align="left">Desc</th>
		<th align="left">Location</th>
		<th align="center">Type</th>
		<th align="center">Allocated</th>
		<th align="center">Free</th>
		<th align="center">Actions</th>
	</tr>

HTML;

$style = "style1";
while ($curDate <= $end){
	$displayDate = date ("Y-m-d", $curDate);
	if (isset($jobs[$displayDate])){
		$curJobs = $jobs[$displayDate];
	}else{
		$curJobs[0]['job_colour'] =
		$curJobs[0]['job_time_slot'] =
		$curJobs[0]['cust_surname'] =
		$curJobs[0]['job_desc'] =
		$curJobs[0]['job_location'] =
		$curJobs[0]['job_type'] = 
		$allocation = 
		$free = " ";
	}
	foreach ($curJobs as $job){
		$row &= $job;
		$allocation = getContent("installer.php");
		$free = getContent("free.php");
		echo <<<HTML
	<tr class="$style">
		<td>{$job['job_colour']}</td>
		<td>$displayDate</td>
		<td>{$job['job_time_slot']}</td>
		<td>{$job['cust_surname']}</td>
		<td>{$job['job_desc']}</td>
		<td>{$job['job_location']}</td>
		<td>{$job['job_type']}</td>
		<td>$allocation</td>
		<td>$free</td>
		<td>PUT SOME BUTTONS HERE</td>
	</tr>

HTML;
	} //end of foreach loop
	$style = ($style=="style1") ? "style2" : "style1";
	$curDate = strtotime("+1 day", $curDate);
} //end of while loop

echo "</table>";

function getContent($file){
	ob_start();
	include $file;
	$contents = ob_get_contents();
	ob_end_clean();
	return $contents;
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top