Category Archives for PHP programming

Scaffolding in Cakephp

About Scaffolding in CakePhp . Application scaffolding is a technique that allows a developer to define and create a basic application that can create, retrieve, update and delete objects. Scaffolding in CakePHP also allows developers to define how objects are related to each other, and to create and break those links.

All that’s needed to create a scaffold is a model and its controller. Once you set the $scaffold variable in the controller, you’re up and running.

CakePHP’s scaffolding is pretty cool. It allows you to get a basic CRUD application up and going in minutes. So cool that you’ll want to use it in production apps. Now, we think its cool too, but please realize that scaffolding is… well… just scaffolding. It’s a loose structure you throw up real quick during the beginning of a project in order to get started. It isn’t meant to be completely flexible, it’s meant as a temporary way to get up and going. If you find yourself really wanting to customize your logic and your views, its time to pull your scaffolding down in order to write some code. CakePHP’s Bake console, covered in the next section, is a great next step: it generates all the code that would produce the same result as the most current scaffold.

Scaffolding is a great way of getting the early parts of developing a web application started. Early database schemas are subject to change, which is perfectly normal in the early part of the design process. This has a downside: a web developer hates creating forms that never will see real use. To reduce the strain on the developer, scaffolding has been included in CakePHP. Scaffolding analyzes your database tables and creates standard lists with add, delete and edit buttons, standard forms for editing and standard views for inspecting a single item in the database.

To add scaffolding to your application, in the controller, add the $scaffold variable:

class CategoriesController extends AppController {

var $scaffold;

}

for more info : http://manual.cakephp.org/view/105/Scaffolding

How to change FeedBurner Reader Statistic Using PHP cURL


If you want to change the traditional feedburner  reader statistic image use the below program in your widgets which will fetch the readers feedburner .Everyone has one: Digg, Feedburner, Pownce, Flickr, Google Maps, etc. FeedBurner provides a sweet “Awareness API” that allows you to pull statistics from your FeedBurner account. Here’s how you do it using PHP cURL.

//open connection  

$ch = curl_init();  

//set the url, number of POST vars, POST data  

curl_setopt($ch,CURLOPT_URL,‘http://api.feedburner.com/awareness/1.0/GetFeedData?id=#######’);  

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);    

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  

     //execute post  

     $content = curl_exec($ch);  

     $subscribers = get_match(‘/circulation=”(.*)”/isU’,$content);  

echo ‘Subscribers:  ’.$subscribers;  

      //close connection  

curl_close($ch);  

/* helper: does the regex */  

function get_match($regex,$content)  

{  

          preg_match($regex,$content,$matches);  

    return $matches[1];  

}  

 

It’s that easy. What makes this even better is that I hate FeedBurner’s “badge” and I can manipulate the result of the above code any way I want.  To learn more about FeedBurner’s Awareness API and the information you can pull about your feed view the below url http://code.google.com/apis/feedburner/awareness_api.html

Export data from MYSQL to EXCEL

Php script to export data from MYSQL to EXCEL. export data from mysql to excelYou can just upload this application and use. It no needs any database or any other external plugin. To get this web application just pay $6  (Click here to Pay) and just send your payment detail and mail id through the contact form.

Smarty with htaccess

If you are using PHP with SMARTY  and want to quickly convert your ugly URLs to pretty URLs, it is best to use a detailed ruleset for mod_rewrite to process. The alternative is to say “if the requested file does not exist, rewrite to a particular script”. This means that requests for images, CSS files, etc. will still return the correct file. Everything else will be sent to the script to process where the requested URL will be available under the server REQUEST_URI variable. To use this method, you would need to create a .htaccess file in your domain root (or wherever you want the rewrite to apply) with the following inside:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]

Replace index.php with the name of whatever script you wish to use. Sticking with the PHP example, I could then create the index.php file as follows:

<?php
$requested = empty($_SERVER['REQUEST_URI']) ? false : $_SERVER['REQUEST_URI'];
 
$smarty=new Smarty();
 
switch ( $requested ) {
 
        case 'latest':
               require_once 'latest_products.php';
               $content=$smarty->fetch(latest.tpl);
               break;
 
        case 'listing':
               require_once 'list_products.php';
               $content=$smarty->fetch(list_products.tpl);
               break;
 
        default:
               include 'default.php';
               $content=$smarty->fetch(default.tpl);
}
 
$smarty->assign(‘header’,$smarty->fetch(header.tpl));
$smarty->assign(‘footer’,$smarty->fetch(footer.tpl));
$smarty->assign(‘bodyContent’,$content);
$smarty->display(main.tpl);
 
?>

 
main .tpl

 {$header}

{$bodyContent}

{$footer}

 

If you need any projects or customization in SMARTY and PHP  , please send me an message via contact form

Create thumbnail using php

One of most searches in php is how to create thumbnail images on the fly using PHP. Since this requires the GD library, you will need an installation of PHP with at least GD 2.0.1 enabled.

The code below creates a function named resizeImage that will get three parameters. The first parameter is correspondingly the path to the directory that contains original images. The second is the image extension The third and fourth parameter is the width and height you want for the thumbnail images.

From the below method you can create thumb of JPG, PNG,GIF.

function resizeImage($remoteImage,$ext,$maxwidth,$maxheight)

        {

        $imagepath=$remoteImage;

        $imagedata=getimagesize($imagepath);

        $imgwidth=$imagedata[0];

        $imgheight=$imagedata[1];

     

          $shrink=1;

         if($imgwidth > $maxwidth)

            {

                $shrink=$maxwidth/$imgwidth;

            }

     

         if($shrink!=1)

            {

              $output_height=$shrink * $imgheight;

              $output_width=$maxwidth;

            }else{

              $output_height=$imgheight;

              $output_width=$imgwidth;

            }

         if( $output_height > $maxheight )

            {

                $shrink = $maxheight / $output_height ;

                $output_width = $shrink * $output_width;

                $output_height = $maxheight;

            }

     

          switch($ext){

          case “.gif”:

            $src_image = @imagecreatefromgif($imagepath);

            $dest_image = @imagecreatetruecolor ($output_width, $output_height);

            imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $output_width, $output_height, $imgwidth, $imgheight);

            imagegif($dest_image, $imagepath, 100);

            break;

          case “.jpg”:

            $src_image = @imagecreatefromjpeg($imagepath);

            $dest_image = @imagecreatetruecolor ($output_width, $output_height);

            imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $output_width, $output_height, $imgwidth, $imgheight);

            imagejpeg($dest_image, $imagepath, 100);

            break;

          case “.jpeg”:

            $src_image = @imagecreatefromjpeg($imagepath);

            $dest_image = @imagecreatetruecolor ($output_width, $output_height);

            imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $output_width, $output_height, $imgwidth, $imgheight);

            imagejpeg($dest_image, $imagepath, 100);

            break;

          case “.png”:

            $src_image = @imagecreatefrompng($imagepath);

            $dest_image = @imagecreatetruecolor ($output_width, $output_height);

            imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $output_width, $output_height, $imgwidth, $imgheight);

            imagepng($dest_image, $imagepath, 100);

            break;

          }

     

        }