Tutorial on Creating Dynamic Sitemap Using PHP

If you own a website then one very common problem that you might come across is that some of your webpages might not show up in the search results. In such a case, most of us believe that the webpage does not have any links to them on the website. But unless the search engines have banned that web page, there are very less chances of any particular webpage not having links to it on the website. And in such a scenario, you are required to provide a proper navigation structure for your users, but that might not be a feasible option, especially when your website is having lot of content

.

Read more

PHP Frameworks Outcast Core… Or It’s Vice-versa

How will you describe a scripting language which is currently powering 244 Million sites? Big League, Right? In the web arena we call it PHP! Yes! PHP or Hypertext Preprocessor is the most eminently used server-side scripting language, driving almost 39% of the websites operational today. Almost every web development company renders PHP-driven app development as a part of its service portfolio.

Read more

Generating XML with CakePHP

Generating XML with CakePHP
Cake PHP LogoXML plays a pivotal role in transferring and storing data over the World Wide Web. It is a markup language, much like HTML, which is designed to carry data but not display it. If you need to carry out data transmission in any kind of web based application, you need to generate a XML file. The process of generating XML is a smooth sail, if you have the power of CakePHP with you. Let us take a tour of the various steps involved in generating XML using CakePHP development platform.

Steps Involved in XML Generation Via CakePHP:
1.       First off, edit the routes.php file present within the config directory of your CakePHP application. You will need to add the following short code snippet to the routes.php file in order  to enable XML extension parsing in CakePHP:
Router::parseExtensions('xml');
This snippet tells CakePHP to parse the XML extension. We are, now, ready to generate XML for our web application.

2.       In this step, we will set up Controller action that will be helpful in dynamically generating XML file in CakePHP and fetch data from the application. Setting up Controller will result in an error-free and well-formatted XML file in CakePHP. We have the option of configuring the setting of Controller action in such a way that it gets activated only if the XML makes a request. This means, when we have .xml in the end. To facilitate this procedure, you can use the Request Handler component. You have the option of either declaring the Request Handler at the top of the Controller or including it in the AppController so that it can be utilized throughout the application.

The task of a request handler is to detect and process requests. In the current scenario, it will allow us in detecting whether or not a particular request is XML. If the request is XML, we will deliver an XML file view and an empty layout. Otherwise, we will not perform any action. The following code snippet will help you in adding Request Handler to your application:
var$components= array('RequestHandler');
 Here’s a preview of the controller action while generating XML file in CakePHP:

[php]
function xmlgen($id=null){
 if ($this->RequestHandler->isXml())//check if xml request is made
 {
 $this->layout="empty";//empty layout for xml,create an empty layout in app/views/layouts/empty.ctp
// find playlist    from provided id
 $data=$this->Playlist->find("first",array("conditions"=>array("Playlist.id"=>$id));
 $this->set(compact('data'));
 }
Else
{
//don nothing if request is not xml ,or you can redirect somewhere.
}
}
[/php]
 
This action might look simple, just like any other action, but it will be operational only if the file is XML, otherwise it will not work.

3.       In this step, we will set up the view of our XML file in accordance to the Controller Action that we created in the previous step. We will be creating an XMl view for the action xmlgen() that we just created. These views will be created and stored in the XML folder created under the views folder. This means the view xmlgen.ctp will be stored under the following location:
app/views/yourcontrollername/xml/xmlgen.ctp

Suppose the name of our controller is ‘Favorites’, then xmlgen.ctp will be created under the following path:
app/views/favorites/xml/xmlgen.ctp
This is how the view for XML generation in CakePHP will appear:
[php]
<?php
echo $this->Xml->header();//XML header
?>
<favorites version="1" xmlns="http://xspf.org/ns/0/">
<title><?php echo $data['title']; ?></title>
<creator><?php echo $data['author']; ?></creator>
<link>www.eaxmple.com</link>
<trackList>
<?php foreach($data['Songs'] as $plist):?>
<track>
<location><?php echo$plist['Song']['loc']; ?></location>
 <creator><?php echo$plist['Song']['a_name']; ?></creator>
 <album><?php echo$plist['Song']['a_name']; ?></album>
<title><?php echo$plist['Song']['sname']; ?></title>
 <duration><?php echo $data['Song']['Time']; ?></duration>
<link><?php echo$plist['Song']['loc']; ?></link>
</track>
<?php endforeach;?>
</trackList>
</ favorites >
 [/php]

These were some of the essential steps involved in generating XML file using CakePHP framework. These steps show how CakePHP lets you create well-formatted XML file with minimum coding and least efforts.
 
Author Bio:- Steve Graham is a pro in PHP Web Development associated as a web writer with Xicom Technologies Ltd. A CMMI Level 3 firm, offering web based services to it's esteemed clientele worldwide.

Read more

PHP for beginners -Lesson 6

WHILE LOOP IN PHP
 
Code will execute continue till condition reached false.
Syntax:
initialization – set your counter above while loop while (condition)
  {
  code here…
   
  increment/decrement
  }
 
Parameters :  
  condition – Loop will continue till condition is TRUE.
  increment/decrement – increase or decrease your counter , this parameter should be before end of loop.  

Read more

PHP for beginners -Lesson 5

LoopingFOR LOOP IN PHP

Code will execute continue till condition reached false.
Syntax: for (initialization; condition; increment/decrement) { code here… }

Parameters :
initialization – set your counter, execute only once.
condition – Loop will continue till condition is TRUE.
increment/decrement – increase or decrease your counter , this parameter will execute at end of loop.

Example 1 : Display 1 to 5 numbers using For loop. <?php $i=1; for( $i = 1; $i <= 5; $i++ ){ echo $i."\t"; // \t use for TAB } ?>

Output : 1 2 3 4 5

Example 2 : Display table of input number <?php $no=5; for( $i = 1; $i <= 10; $i++ ){ echo $no*$i."\t"; } ?>

Output: 5 10 15 20 25 30 35 40 45 50

Example 3 : Fibonacci series
What is Fibonacci Numbers?
Below sequence called Fibonaci series
0,1,1,2,3,5,8,13,21,34,55,89,,144………. (f3 =f2+f1 and given f1=0, f2=1)

By definition, the first Fibonacci number is 0 and second Fibonacci number is 1. Then each subsequent number is calculated based on sum of the previous two. <?php $f1 = 0; $f2 = 1; $range = 10; for ( $i = 2; $i < $range; $i++ ){ $fn = $f1 + $f2; echo $fn."\t"; $f1 = $f2; $f2 = $fn; } ?>

Output : 1 2 3 5 8 13 21 34

Example 4 : Prime number from 1 to 100
A Prime Number can be divided – only by 1 or itself. And it must be a whole number greater than 1

<?php for( $i = 1; $i <= 100; $i++ ){ for( $j = 2; $j <= $i-1; $j++ ){ if( $i % $j == 0) break; } if($j==$i) echo $j."\t"; } ?>

Output : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

 

Read more

PHP for beginners -Lesson 4

Minimal Basics on Switch statements

What is switch statement in php ?
Switch is a conditional statement , which perform specific action according to various condition. It is equivalent to If else strcutures.
Syntax :

[php]switch (variable) {
    case label1:
        This block will execute if variable=label1;
    break;
    case label2:
        This block will execute if variable=label2;
    break;
    case label3:
        This block will execute if variable=label3;
    break;
    default:
        This block will execute if variable not matching with any label;
}
[/php]
variable can be integer,string, character, float, double etc..
switch is similar to if..elseif..esle statement.
Note : break is required , if you missed break then it will check next case and execute default case also.

<?php
$num = 2;
switch($num){
case 1:
echo "My number is One";
break;
case 2:
echo "My number is Two";
break;
case 3:
echo "My number is Three";
break;
default :
echo "Not matching";
}
?>

Output : My number is Two

1. Checking vowel & consonant(Only for Small case letter) <?php
$c = 'u';
switch($c){
case 'a':
echo "$c is Vowel";
break;
case 'e':
echo "$c is Vowel";
break;
case 'i':
echo "$c is Vowel";
break;
case 'o':
echo "$c is Vowel";
break;
case 'u':
echo "$c is Vowel"; break;
default:
echo "$c is Consonant ";
}
?>

Output : u is Vowel

 
2. Wishing birthday on current date

<?php $dob = date("d/m");
switch($dob){ case "10/02":
echo "Happy birthday Somnath";
break;
case "23/07":
echo "Happy birthday Santosh";
break;
case "18/08":
echo "Happy birthday Anil";
break;
case "04/09":
echo "Happy birthday Jassi";
break;
case "11/12":
echo "Happy birthday Ritesh";
break;
default: echo "No birthday party";
} ?>

if Today's date is – 23/07/2011
Output : Happy birthday Santosh

Read more