[eluser]brucebat[/eluser]
Hi all,
I am trying to make a formvalidation function that compares a series of time inputs and makes sure they are in a chronological order.
I have spent a few hours trying to get it to work but my logic is flawed as there isnt a way of me preserving a value so the function can compare that value.
So my inputs are like this:
time0, time1, time2, time3
So if its time0 field then the validaiton function should recognize that its the first time and not perform validation obviously because there is no time before it.
However if its not the first time then it must compare that time to the time before it.
Here is my for calling the validation for all the fields:
Code:
for ($i=0 ; $i<$completedeventrows; $i++)
{
//this stores a value of the count for time_order validation function
$this->session->set_flashdata('count', $i);
//performs standard CI validations + custom validation to check the time is in correct format and also check the times are in chrono order
$this->form_validation->set_rules('time'.$i, 'Time'.($i+1), 'required|xss_clean|callback_valid_time|callback_time_order'); // time0, time1, time2 etc
}
The validation function "time_order()"
Code:
//this function checks to ensure all events are in a chronological order
public function time_order ($str)
{
//if the time is the first event then do not compare to previous time as there is none
if ($this->session->flashdata('count') <= 0)
{
//This part seperates the string into three segments using the ":" as the divide
$temp = explode(':',$str);
return TRUE;
}
//compare the current event time to its previous one
else
{
//get current event time
$current = explode (':', $str);
//if the current times hours part is less than previous hours part then false
if ($current[0] < $temp[0])
{
$this->form_validation->set_message('time_order', 'The %s fields Hour segment is before the the previous events Hour');
return FALSE;
}
//if current time hours sgement is greater than or equal then check minutes segment
else if ($current[0] == $temp[0])
{
if ($current[1] < $temp[1])
{
$this->form_validation->set_message('time_order', 'The %s fields Minutes segment of time is before the the events time');
return FALSE;
}
else if ($current[1] == $temp[1])
{
//if seconds are lesser than previous times seconds then false
if ($current[2] < $temp[2])
{
$this->form_validation->set_message('time_order', 'The %s fields Seconds segment of time is before the the previous events time ');
return FALSE;
}
else
{
return TRUE;
}
}
else
{
return TRUE;
}
}
//if current times hour segment is greater then no need to check mins or seconds
else
{
return TRUE;
}
}
}
The problem is that I keep losing Temp so there is no way to compare as
Any suggestions?
Thanks