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 *************/
}

Advertisements

phpmyadmin not working after updating php


Create a link in /var/www like this:

sudo ln -s /usr/share/phpmyadmin /var/www/

Note: since 14.04 you may want to use /var/www/html/ instead of /var/www/ (same for php 5)

If thats not working for you, you need to include phpmyadmin inside apache configuration.

Open apache.conf using your favorite editor, mine is vim 🙂

sudo vim /etc/apache2/apache2.conf

Then add the following line:

Include /etc/phpmyadmin/apache.conf

Hour dropdown


<select id='time'>
<?php for($i = 1; $i <= 24; $i++): ?>
    <option value="<?= $i; ?>"><?= date("h.iA", strtotime("$i:00")); ?></option>
<?php endfor; ?>
</select>
<select id='time'>
    <option value="1">01.00AM</option>
    <option value="2">02.00AM</option>
    <option value="3">03.00AM</option>
    <option value="4">04.00AM</option>
    <option value="5">05.00AM</option>
    <option value="6">06.00AM</option>
    <option value="7">07.00AM</option>
    <option value="8">08.00AM</option>
    <option value="9">09.00AM</option>
    <option value="10">10.00AM</option>
    <option value="11">11.00AM</option>
    <option value="12">12.00PM</option>
    <option value="13">01.00PM</option>
    <option value="14">02.00PM</option>
    <option value="15">03.00PM</option>
    <option value="16">04.00PM</option>
    <option value="17">05.00PM</option>
    <option value="18">06.00PM</option>
    <option value="19">07.00PM</option>
    <option value="20">08.00PM</option>
    <option value="21">09.00PM</option>
    <option value="22">10.00PM</option>
    <option value="23">11.00PM</option>
    <option value="24">12.00AM</option>
</select>

Convert object to array and Vice versa


function objectToArray($d) {
		if (is_object($d)) {
			// Gets the properties of the given object
			// with get_object_vars function
			$d = get_object_vars($d);
		}
 
		if (is_array($d)) {
			/*
			* Return array converted to object
			* Using __FUNCTION__ (Magic constant)
			* for recursive call
			*/
			return array_map(__FUNCTION__, $d);
		}
		else {
			// Return array
			return $d;
		}
	}

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"

Sending HTML Mail via PHP


<?php
 require_once "Mail.php";
 require_once "Mail/mime.php";
 
 // see http://pear.php.net/manual/en/package.mail.mail-mime.php
 // for further extended documentation on Mail_Mime

 $from = "Web Master ";
 $to = "Nobody ";
 $subject = "Test HTML email using PHP Pear w/ SMTP\r\n\r\n";
 $text = "This is a text test email message";
 $html = "

This is an html test email message This Is A Link

"; $crlf = "\n"; // create a new Mail_Mime for use $mime = new Mail_mime($crlf); // define body for Text only receipt $mime->setTXTBody($text); // define body for HTML capable recipients $mime->setHTMLBody($html); // specify a file to attach below, relative to the script's location // if not using an attachment, comment these lines out // set appropriate MIME type for attachment you are using below, if applicable // for reference see http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types $file = "attachment.jpg"; $mimetype = "image/jpeg"; $mime->addAttachment($file, $mimetype); // specify the SMTP server credentials to be used for delivery // if using a third party mail service, be sure to use their hostname $host = "mail.emailsrvr.com"; $username = "webmaster@example.com"; $password = "yourPassword"; $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password)); $body = $mime->get(); $headers = $mime->headers($headers); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo(" " . $mail->getMessage() . " "); } else { echo(" Message successfully sent! "); } ?>