Sunday, November 19, 2017

Laravel - Create models from database

If you are using MySQL and Laravel 5.1 or above you can use php artisan code:models from this package: reliese/laravel. All you need to do is:
1.      composer require reliese/laravel
2.      Add the service provider to your config/app.php file Reliese\Coders\CodersServiceProvider::class
3.      Publish the config file with php artisan vendor:publish --tag=reliese-models
4.      Make sure your database is correctly configured in config/database.php and .env files.
And finally issue the command: php artisan code:models

Friday, March 24, 2017

Codeigniter and Bootstrap dynamic dialog messages

Creating Alert Messages with Bootstrap




Set flash data in your controller

$this->session->set_flashdata('alert-danger', 'Invalid Log In');
$this->session->set_flashdata('alert-success', 'Success Log In');


View dynamic dialog message with simple helper function


 echo flash_alert();


Helper function :

f(!function_exists('flash_alert')) {
    function flash_alert(){
        $CI = & get_instance();
        $flash_data = $CI->session->flashdata();
        $alertMsg = "";
        if(empty($flash_data)) {
            return $alertMsg;
        }
        foreach($flash_data as $key => $message) {      
                $alertMsg.= '<div class="alert '.$key.' fade in">
                <a href="#" class="close" data-dismiss="alert">&times;</a>
                '.$message.'
                </div>';
        }
        return $alertMsg;
    }
}



Wednesday, July 27, 2016

Email with Multiple Attachments in PHP Script

function mail_attachment($filenames, $mailto, $from_mail, $from_name, $replyto, $subject, $bodyMessge) {

$uid = md5(uniqid(time()));
//$name = basename($file);

$eol = PHP_EOL;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Basic headers
$header = "From: ".$from_name." <".$from_mail.">".$eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"";

// Put everything else in $message
$message = "This is a multi-part message in MIME format.".$eol.$eol.
            "--{$mime_boundary}".$eol;


//$message = "--".$uid.$eol;
$message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= $bodyMessge.$eol;
//$message .= "--".$uid.$eol;
$message .= "--{$mime_boundary}".$eol;

foreach ($filenames as $filename) {
$file = UPLOAD_FILE_PATH.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);

$content = chunk_split(base64_encode($content));
$message .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $content.$eol;
//$message .= "--".$uid."--";
$message .= "--{$mime_boundary}".$eol;
}

if (mail($mailto, $subject, $message, $header)){
  echo "success";
 } else {
 echo "error";
 }
}

Friday, December 12, 2014

PHP MAIL SCRIPT

//first copy & pste the below two lines.
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\n";

// Here comes your to from cc and bcc values
$headers .= "From: ".$fromemail." <".$fromemail.">\n";
$headers .= "Return-Path: ".$fromemail."\n";
$headers .= "Return-Receipt-To: ".$fromemail."\n";

// then comes priority options and mailer version.
$headers .= "X-Priority: 1(Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
//$headers .= "X-Mailer: php\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "Importance: High\n";

if(mail($toEmail,$subject,$message,$headers)){
    echo 'mail sent';
}else{
    echo 'failed';
}

Wednesday, November 26, 2014

PHP Bootstrap Dynamic Pagination

function pagination($limit,$adjacents,$rows,$page){
    $reqPage="";
    if(isset($_REQUEST["page"])&& $_REQUEST["page"]!="")
    $reqPage.="&page=".$_REQUEST["page"];
   
   
   

 $pagination='';
 if ($page == 0) $page = 1; //if no page var is given, default to 1.
 $prev = $page - 1; //previous page is page - 1
 $next = $page + 1; //next page is page + 1
 $prev_='';
 $first='';
 $lastpage = ceil($rows/$limit);
 $next_='';
 $last='';
 if($lastpage > 1)
 {

 //previous button
 if ($page > 1)
 $prev_.= "<li><a class='page-numbers' href=\"?view=$prev$reqPage\">Previous</a></li>";
 else{
 //$pagination.= "<span class=\"disabled\">previous</span>";
 }

 //pages
 if ($lastpage < 5 + ($adjacents * 2)) //not enough pages to bother breaking it up
 {
 $first='';
 for ($counter = 1; $counter <= $lastpage; $counter++)
 {
 if ($counter == $page)
 $pagination.= "<li class=\"active\"><span>$counter</span></li>";
 else
 $pagination.= "<li><a class='page-numbers' href=\"?view=$counter$reqPage\">$counter</a></li>";
 }
 $last='';
 }
 elseif($lastpage > 3 + ($adjacents * 2)) //enough pages to hide some
 {
 //close to beginning; only hide later pages
 $first='';
 if($page < 1 + ($adjacents * 2))
 {
 for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
 {
 if ($counter == $page)
 $pagination.= "<li class=\"active\"><span>$counter</span></li>";
 else
 $pagination.= "<li><a class='page-numbers' href=\"?view=$counter$reqPage\">$counter</a></li>";
 }
 $last.= "<li><a class='page-numbers' href=\"?view=$lastpage$reqPage\">Last</a></li>";
 }

 //in middle; hide some front and some back
 elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
 {
 $first.= "<li><a class='page-numbers' href=\"?view=1$reqPage\">First</a></li>";
 for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
 {
 if ($counter == $page)
 $pagination.= "<li class=\"active\"><span>$counter</span></li>";
 else
 $pagination.= "<li><a class='page-numbers' href=\"?view=$counter$reqPage\">$counter</a></li>";
 }
 $last.= "<li><a class='page-numbers' href=\"?view=$lastpage$reqPage\">Last</a></li>";
 }
 //close to end; only hide early pages
 else
 {
 $first.= "<li><a class='page-numbers' href=\"?view=1$reqPage\">First</a></li>";
 for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
 {
 if ($counter == $page)
 $pagination.= "<li class=\"active\"><span>$counter</span></li>";
 else
 $pagination.= "<li><a class='page-numbers' href=\"?view=$counter$reqPage\">$counter</a></li>";
 }
 $last='';
 }

 }
 if ($page < $counter - 1)
 $next_.= "<li><a class='page-numbers' href=\"?view=$next$reqPage\">Next</a></li>";
 else{
 //$pagination.= "<span class=\"disabled\">next</span>";
 }
 $pagination = "<ul class=\"pagination pagination-sm\">".$first.$prev_.$pagination.$next_.$last;
 //next button

 $pagination.= "</ul>\n";
 }

return $pagination;
}