Jump to content

[SOLVED] Looping through Dates/Arrays


php_jord

Recommended Posts

I'm trying to create an html table to show actions taken on specific days.  It should have the date and total hours in the first two columns, followed by the action information.  If multiple actions are taken in one day, new rows are created for each action before the next date is posted.

 

This works fine for the first date, regardless of the number of actions, but no actions are shown for any other dates.

 

DateHoursStartTimeEndTimeDescriptionCategory6/082900:00:0024:00:0031SomeInfo[/td][td]12:00:0017:00:0031SomeInfo6/095.006/105.006/11296/125.006/1314.00

 

This is what it looks like.

 

This is the code I'm using:

$first = date("Y-m-d", strtotime($form['pay_begin_dt']));
$last = date("Y-m-d", strtotime($form['pay_end_dt']));
$actions = mysql_query("Select ActionDate, Description, StartTime, EndTime, CategoryDescription
						From Actions a join Categories c on a.CategoryID = c.CategoryID
						Where EmployeeID = '$emp'
						And ActionDate >= '$first'
						And ActionDate <= '$last'", $myConn);

$table = "<table border><tr><th>Date</th><th>Hours</th><th>StartTime</th><th>EndTime</th><th>Description</th><th>Category</th></tr>";
for($j = 0; $j < $i; $j++) {
	$table .= "<tr><td>" .$tableArray[$j][1] . "</td><td>" . $tableArray[$j][0] . "</td>";
	$rowCount = 0;
	while($row = mysql_fetch_array($actions)) {
		//Format actionDate to match date format
		$actionDate = date("n/d", strtotime($row['ActionDate']));
		if($actionDate == $tableArray[$j][1]) {
			if($rowCount == 0) {
				$table .= "<td>" . $row['StartTime'] . "</td><td>" . $row['EndTime'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['CategoryDescription'] . "</td>";
				$rowCount++;
			} else {
				$table .= "</tr><tr><td></td><td></td><td>" . $row['StartTime'] . "</td><td>" . $row['EndTime'] . "</td><td>$row[Description]</td><td>$row[CategoryDescription]</td>";
			}				
		}
	}
	$table .= "</tr>";
}

$table .= "</table>";
echo $table;

 

$tableArray is populated earlier on, but removed here for brevity.

Link to comment
https://forums.phpfreaks.com/topic/164679-solved-looping-through-datesarrays/
Share on other sites

I didn't see an edit/delete button, so I'll throw up the solution in case someone searches for this problem in the future.

 

The mistake was using mysql_fetch_array in the for loop.  The pointer is not reset for each loop, so mysql_data_seek($actions, 0) resets the pointer to the first row, and is placed in front of the while loop.

 

I'm sure there's a more efficient way to do this, but we're not checking many results.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.