Add recurring task php


<?php

$startDate = $postData[‘start_date’];
$endDate = $postData[‘end_date’];
$endDate = date(“Y-m-d”, strtotime($endDate . ” +1 day”));

/* For weekly schedule
* frequency_id:
*      ‘1’ => ‘Daily’,
*      ‘2’ => ‘alternative day’,
*      —-Every——–
*      ‘3’ => ‘Monday’,
*      ‘4’ => ‘Tuesday’,
*      ‘5’ => ‘Wednesday’,
*      ‘6’ => ‘Thursday’,
*      ‘7’ => ‘Friday’,
*      ‘8’ => ‘Saturday’,
*      ‘9’ => ‘Sunday’
*  */
if (in_array ($postData[‘frequency_id’], array(3,4,5,6,7,8,9))) {
$beginDate = new DateTime($startDate);
$endDate1 = new DateTime($endDate);

$interval = DateInterval::createFromDateString(‘1 day’);
$period = new DatePeriod($beginDate, $interval, $endDate1);

$arrDay = array(‘3’ => ‘Monday’, ‘4’ => ‘Tuesday’, ‘5’ => ‘Wednesday’, ‘6’ => ‘Thursday’, ‘7’ => ‘Friday’, ‘8’ => ‘Saturday’, ‘9’ => ‘Sunday’);

foreach ($period as $dt)
if ($dt->format(“l”) == $arrDay[$postData[‘frequency_id’]]) {
$startDate = $dt->format(“Y-m-d”);
break;
}
}

$begin = new DateTime($startDate);
$end = new DateTime($endDate);

if ($postData[‘frequency_id’] == ‘1’) { // daily
$interval = DateInterval::createFromDateString(‘1 day’);
} elseif ($postData[‘frequency_id’] == ‘2’) { // alternative day
$interval = DateInterval::createFromDateString(‘2 day’);
} elseif (in_array ($postData[‘frequency_id’], array(3,4,5,6,7,8,9))) {
$interval = DateInterval::createFromDateString(‘7 day’);
}

$period = new DatePeriod($begin, $interval, $end);

foreach ($period as $dt) {
$postData[‘frequency_post_id’] = $frequencyPostAdd;
$postData[‘post_date’] = $dt->format(“Y-m-d”);

/********** Your Logik *************/
}

Maintain display order field in php


Remember I am considering following two parameters


table name - DisplayOrder
column name to reorder = O_nbr

if you need to shift the O_nbr (order number) from 10 to 1 you just need to call the method as

select UpdateOrder(10,1);
// it will return you the proper message either updated or not.. or whatever

the function code is as below –


DELIMITER $$
create function UpdateOrder (fromOrder int, toOrder int) 
RETURNS varchar(50)
DETERMINISTIC
BEGIN 
  DECLARE reslt varchar(50);
  DECLARE rowCount int;
  SET reslt = 'Error Occured.';
  if( fromOrder < 1) then
    SET reslt='fromOrder can not be less then 1';
  elseif(toOrder < 1) then
    SET reslt='toOrder can not be less then 1';
  else  
    select count(*) into rowCount from DisplayOrder;        
    if(rowCount < fromOrder) then
        SET reslt = concat('Sorry check again, we have only ',rowCount, ' records');
    elseif(rowCount  toOrder) then
              update DisplayOrder set O_nbr = 0 where O_nbr = fromOrder;
              Update DisplayOrder set O_nbr = O_nbr+1 where O_nbr  (toOrder-1) order by O_nbr desc;
              update DisplayOrder set O_nbr = toOrder where O_nbr = 0;
            end if;
            if(fromOrder  fromOrder AND O_nbr < (toOrder+1) order by O_nbr asc;
              update DisplayOrder set O_nbr = toOrder where O_nbr = 0;
            end if;
            SET reslt = 'Successfully done';
        end if;
    end if;         
  end if;     
  RETURN reslt;
END$$
DELIMITER ;

You can replace the DisplayOrder with you table nane and O_nbr with you column name to reorder

copy the code and run it in you mysql database query window.. then to shift the order (O_nbr in DisplayOrder table) use the above described signature..


Select UpdateOrder(fromOrder,  toOrder);

if you try to run with 0 or less value, or any unsuitable condition, it will return you the error message


Select UpdateOrder(10, 0);

//returns "toOrder can not be less then 1"


Select UpdateOrder(11, 1);

//return "Sorry check again, we have only 10 records"


Select UpdateOrder(5, 5);

//returns "No Changes were made."

if you pass the correct parameter..


Select UpdateOrder(10, 2);

//returns "Successfully done"