Problem developing Class Plotting Schedule feature

I am developing a feature called Class Plotting Schedule in our CMS, this is actually working but I think there is an error like a LOGICAL error,

Here is my Class Plotting Scheduler and I used a table to this with a checkbox

Here is my code

<?php
function create_time_range($start, $end, $interval='30 mins', $format='12'){
	$startTime=strtotime($start);
	$endTime=strtotime($end);
	$returnTimeFormat=($format=='24')?'g:i':'G:i';

	$current=time();
	$addTime=strtotime('+'.$interval,$current);
	$diff=$addTime-$current;

	$times=array();
	while ($startTime<$endTime) {
		$times[]=date($returnTimeFormat,$startTime);
		$startTime+=$diff;
	}
	$times[]=date($returnTimeFormat,$startTime);
	return $times;
}
?>
<div class="table-responsive">
                <form method="POST">
	                <table class="table table-bordered table-hoverable table-striped" id="tblClassScheduler">
	                    <thead>
	                       <tr>
	                         <?php
	                          $days=array("Monday"=>1,"Tuesday"=>2,"Wednesday"=>3,"Thursday"=>4,"Friday"=>5,"Saturday"=>6,"Sunday"=>7);
	                          for ($i=0;$i<COUNT($days);$i++) { 
	                            echo "<th>".date("D m/d",strtotime(" +".$i." days"))."</th>";
	                          }
	                       ?>
	                       </tr>
	                    </thead>
	                    <tbody>
	                    	<?php
	                    		$class_time=create_time_range('08:00','22:00','30 mins');
	                    		$j=0;
	                    		foreach ($class_time as $value) {
	                    			$j++;
	                    			echo "<tr>
	                    				<td>".$value."<br><input type='checkbox' name='class_time_mon".$j."' value='".$value."'></td>
	                    				<td>".$value."<br><input type='checkbox' name='class_time_tues".$j."' value='".$value."'></td>
	                    				<td>".$value."<br><input type='checkbox' name='class_time_wed".$j."' value='".$value."'></td>
	                    				<td>".$value."<br><input type='checkbox' name='class_time_thurs".$j."' value='".$value."'></td>
	                    				<td>".$value."<br><input type='checkbox' name='class_time_fri".$j."' value='".$value."'></td>
	                    				<td>".$value."<br><input type='checkbox' name='class_time_sat".$j."' value='".$value."'></td>
	                    				<td>".$value."<br><input type='checkbox' name='class_time_sun".$j."' value='".$value."'></td>
	                    			</tr>";
	                    		}
	                    	?>
	                    </tbody>
	                    <button type="submit" class="btn btn-primary" id="save_class_schedule" name="save_class_schedule">Save</button>
	                </table>
                </form>
            </div>

You need to define the purpose/meaning of the data, so that you will know what data should be submitted from the form. Your thread title and the current ‘type’ of data being submitted implies this is for a repeating weekly schedule, i.e. each week is the same. In this case, the data being submitted for each checkbox should be a standard day of week number and a standard 24hr format time value (I would use a two dimensional array name for the checkboxes.) However, by displaying the month/day information, anyone using this interface would assume this is a week-by-week schedule, and any choice is specific to the mm/dd date being shown. In this case, the data being submitted for each checkbox should be a standard full date and time value.

If you are writing this to be general-purpose, you would have a configuration value that picks between those two modes and also has a third choice to output a user interface (radio-button) that lets the mode be selected by the user.

Next, you need to define what the ‘current’ date means to the application and what day is the the first day of the week. The current code starts with the current date (by adding zero days to the current date, then 1 days, 2 days, …) This means the user interface will be changing each day, and there’s nothing in the code to relate each checkbox to which day of week it is for (you will only get the correct data in the correct form field names when the current date is a Monday.) Instead, the code should start with the first defined day of the next full week after the current date.

You should also have a mode where the starting date can be entered via a date picker, so that you can define new (future) data or edit existing data other than starting with the ‘current’ date. You would then have a configuration value where you can select between the current date mode only or the date picker mode, which would also control the output of the html and enable the processing code needed to support a user selectable date.

The ‘business’ logic that knows how to get/produce the data needed to display the page should not be inside the html document. The definition of the $days array and calling the create_time_range() function should be before the start of the html document. The html document should be treated as a template with only simple php code in it to test/loop over input data or it should use an actual template system.

Btw - outputting content inside a html table, but not inside of any <th></th> or <td></td> tags causes it to be rendered before the start of the table. If you want the submit button to be placed before the start of the html table, put the markup for it at that point, not inside the html table.

What makes you think that?

I would suggest not doing all the date juggling manually, but instead switch to DatePeriod to do the juggling for you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.