Wednesday, May 8, 2013

How to configure Zend Framework in Xampp Server


XAMPP and the Zend Framework are both free to download and use. XAMPP comes with the apache web server, php, phpmyadmin, perl, filezilla, mercury mail, and mysql. Zend Framework provides MVC code generators and it is fairly easy to setup with XAMPP. This post will give you instructions on how to install Zend Framework to windows.
Follow the Installation Instructions for your Windows version.

Extract to C:\xampp All of your future website files will be saved at C:\xampp\htdocs. If you save the file “test.php” in the htdocs directory, you can access it with your webbrowser at “http://localhost/test.php” even while you have no Internet connection available.

Add php to your windows system variables: Right Click on ‘My Computer’ > Properties > Advanced Tab > Environment Variables. Environment Variables box will open, View SYSTEM VARIABLES, scroll the list to ’ PATH ‘ > double click ’ PATH ‘ . Click for ScreenShot. Add the following line to the end of the PATH ;C:\xampp\php\php.exe


1.We have to Download the Zend framework full package from http://framework.zend.com/
2.Extract the Zend Framework files to C:\xampp\php\ZendFramework.
3.Edit the php.ini file. You will find it at C:\xampp\php\php.ini. Find the line that says ’ include_path ’ and edit the line: Windows: “\path1;\path2″
include_path = “.;C:\xampp\php\ZendFramework\library”
4.In the C:\xampp\php\ ZendFramework\bin folder you will see a batch file that is used to create the MVC ( model, view, controller ) project structure for you.
5.Create a project inside your C:\xampp\htdocs folder, because apache is setup to serve your files from that directory.
6.Open a shell and cd to C:\xampp\htdocs folder and type this:
C:\xampp\htdocs>C:\xampp\php\ZendFramework\bin\zf.bat create project Zendproject
7.If your shell does not scream any error messages with instructions to you, you can refresh your view of your directory at C:\xampp\htdocs and you will see that new directories have been setup at C:\xampp\htdocs\Zendproject. The new directories in your testproject folder will include >application >library >public >tests and the file .zfprojects.xml

Saturday, April 30, 2011

Triple drop down box from database using Ajax with php

triple drop down box from the database without refreshing page using Ajax and PHP. I’ve put three drop down of countries , states and cities and the drop down’s value changes without refreshing the page. Now let show you how to create it quickly.
First create the  tables of country city and states
First create the following tables of country city and states,
 
CREATE TABLE `countries` (
  `id` int(4) NOT NULL auto_increment,
  `countries` varchar(20) NOT NULL default '',
PRIMARY KEY  (`id`)
) TYPE=MyISAM   ;
CREATE TABLE `states` (
 `id` int(4) NOT NULL auto_increment,
 `countryid` int(4) NOT NULL,
`statename` varchar(40) NOT NULL,
PRIMARY KEY  (`id`)
) TYPE=MyISAM   ;
CREATE TABLE `cities` (
`id` int(4) NOT NULL auto_increment,
`city` varchar(50) default NULL,
`stateid` int(4) default NULL,
`countryid` int(4) NOT NULL,
PRIMARY KEY  (`id`)
) TYPE=MyISAM   ;
Now place the following code  in the index.php file
<form method="post" name="form1">
 <table border="0" cellpadding="0" cellspacing="0" width="60%"><tbody>
  <tr>
   <td width="150">Country</td>
   <td width="150"><select style="background-color: #ffffa0" name="country" onchange="getState(this.value)"><option>Select Country</option><option value="1">USA</option><option value="2">Canada</option>       </select></td>
  </tr>
 <tr>
  <td>State</td>
  <td>
  <p id="statediv">
  <select style="background-color: #ffffa0" name="state"><option>Select Country First</option>       </select></td>
</tr>
<tr>
  <td>City</td>
  <td>
  <p id="citydiv">
  <select style="background-color: #ffffa0" name="city"><option>Select State First</option>       </select></td>
</tr>
</tbody></table>
</form>
 
 
in the onChage event of the country drop down getState() function of 
the javascript is called which change the options values the State drop 
down, let’s look at the code the getState() function. 


function getState(countryId)
{
   var strURL="findState.php?country="+countryId;
   var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
      if (req.readyState == 4)
      {
  // only if "OK"
  if (req.status == 200)
         {
     document.getElementById('statediv').innerHTML=req.responseText;
  } else {
       alert("There was a problem while using XMLHTTP:\n" + req.statusText);
  }
       }
      }
   req.open("GET", strURL, true);
   req.send(null);
   }
}
The code of the PHP file findState.php, which populate the options in the drop down of the state which is fetched from Ajax , is given below
<? $country=intval($_GET['country']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('dbname');
$query="SELECT id,statename FROM states WHERE countryid='$country'";
$result=mysql_query($query);
?>
<select name="state" onchange="getCity(<?=$country?>,this.value)">
 <option>Select State</option>
  <? while($row=mysql_fetch_array($result)) { ?>
    <option value=<?=$row['id']?>><?=$row['statename']?></option>
  <? } ?>
</select>
In the above states dropdown, getCity() function is called in onChage event with countryId and stateId parameter, now let’s look at the code of the getCity() function
function getCity(countryId,stateId)
{
  var strURL="City.php?country="+countryId+"&state="+stateId;
  var req = getXMLHTTP();
  if (req)
  {
    req.onreadystatechange = function()
    {
      if (req.readyState == 4) // only if "OK"
      {
        if (req.status == 200)
        {
          document.getElementById('citydiv').innerHTML=req.responseText;
        } else {
          alert("There was a problem while using XMLHTTP:\n" + req.statusText);
        }
      }
    }
    req.open("GET", strURL, true);
    req.send(null);
  }
}
In the above ajax function, findcity.php is called and this PHP file populate the city dropdown according to the supplied parameters country and state from get method. Now let’s look at the code of findcity.php,
<?php $countryId=intval($_GET['country']);
$stateId=intval($_GET['state']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="SELECT id,city FROM cities WHERE countryid='$countryId' AND stateid='$stateId'";
$result=mysql_query($query);
?>
<select name="city">
 <option>Select City</option>
  <?php while($row=mysql_fetch_array($result)) { ?>
 <option value><?=$row['city']?></option>
<?php } ?>
</select>

Sunday, April 17, 2011

Get the Mysql records in Xml format

Introduction
.................................................................................
This tutorial will show you how to take data that is stored in your mySQL database and easily turn it into XML.

$query ="select* from userdetails";
$resultID = mysql_query($query) or die(mysql_error());


$num = mysql_num_rows($resultID) ;
 if ($num != 0) {

 $file= fopen("xml/results.xml","w");

 $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";

 $_xml .="<userdetails>\r\n";


while ($row = mysql_fetch_array($resultID)) {


$_xml .="\t<user>\r\n";
$_xml .="\t" . $row["userid"] . "\r\n";

$_xml .="\t\t" . $row["username"] . "\r\n";
$_xml .="\t</user>
\r\n";

}

$_xml .="</userdetails>
";

fwrite($file, $_xml);

fclose($file);

//echo "XML has been written";


} else {

echo "No Records found";

}


?>