Thursday, 17 November 2011
Wednesday, 9 November 2011
How To Submit form without Submit Button
Step 1 : Save Below Script any name ex : ( abc.html )
<script> function submitForm() { if(document.formName.name.value==''){ alert("Please Enter the name"); document.formName.name.focus(); return false; }else{ document.formName.submit(); } } </script> <body> <form method='post' name="formName" action="formaction.php"> Enter Name & Press Tab : <input type="text" id="name" name="name" onBlur="return submitForm();" /> </form> </body>
<?php echo "Thank You ".$_POST['name']." Your Form is Submited."; ?>
Monday, 7 November 2011
Create Dojo grid in Zend Framework- a nice example
About a month or two earlier when I used dojo calendar in my application
and write article about the Zend Framework and dojo I was wondering why
zend make collaboration with zend. But today when I created a grid
using zend dojo I feel the real power of the dojo in Zend framework. In
this article I am going to give a simple example and create a grid using
Zend framework and dojo.
At the end of this tutorial we will have a grid like the following

So let’s feel the power of dojo in zend.
First execute the following query for creating a table.
This will create a table in your database.
Create a model in your models directory and write the following code in it.
and save this table as Jobs.php
In the code above, we first extend our model from Zend_Db_Table_Abstract, define its name and define our custom function getJobsData() that fetch all the rows in the jobs table. We return the results as an array.
Next create a controller JobsController in you controller’s directory and place the following code in it.
In the above code we create two action view and records. We have placed nothing in the view action. In the recordsAction, we create an instance of Jobs model class.and then call its getJobsData() method which will give us an array of records. Next we create an instance of Zend_Dojo_Data() and give the fetched data to it. And finally we echo the data converted it into the Json.
We don’t need to create a view for the records action because we have placed exit it the end of this action which will stop rendering of the view. As we will call this action form our grid view template page so no need to define phtml file for it.
However we will need to create template file for our viewAction. In scripts/views/ directory create view.phtml file and place the following code in it.
Keep in mind this is the main template file where we are creating our gird.
In the first statement I told zend to use declarative instead of programmatic behavior which is default. Next I set local path and add specific stylesheets. Then in the javascript code I used statement
Dojo.requre(’dojox.data.’) etc
To include specific dojo js modules.
In the body tag I specify a class “tundra”. This is important for the stylesheet I have added.
Next tow div’s and table are the main code for creating grid.
The first div fetch the records from the url specified. This url may be different if you fetch records form the other controller or source. Please specify valid url otherwise you will not get what you want.
The next div act as a bridge between our first div and the table. This div take data from the first div and give it to the table for display purposes.
Keep in mind that all the attributes of the div’s and table are compulsory. Removing any of the attribute may result in unvalid records or empty page. So define everything that I have defined. Once you become able to display your records, you can play with these attributes.
At the end of this tutorial we will have a grid like the following

So let’s feel the power of dojo in zend.
First execute the following query for creating a table.
CREATE TABLE 'jobs' (
'id' int(11) NOT NULL auto_increment,
'title' varchar(50) NOT NULL,
'description' text,
'posted_date' datetime default NULL,
PRIMARY KEY ('id')
)
This will create a table in your database.
Create a model in your models directory and write the following code in it.
class Jobs extends Zend_Db_Table_Abstract
{
protected $_name='jobs';
public function getJobsData()
{
$select = $this->_db->select()
->from($this->_name);
$results = $this->getAdapter()->fetchAll($select);
return $results;
}
}
and save this table as Jobs.php
In the code above, we first extend our model from Zend_Db_Table_Abstract, define its name and define our custom function getJobsData() that fetch all the rows in the jobs table. We return the results as an array.
Next create a controller JobsController in you controller’s directory and place the following code in it.
<?php
class JobsController extends Zend_Controller_Action
{
public function viewAction()
{
}
public function recordsAction()
{
$jobs = new Jobs();
$data= $jobs->getJobsData();
$dojoData= new Zend_Dojo_Data(’id’,$data,’id’);
echo $dojoData->toJson();
exit;
}
}
In the above code we create two action view and records. We have placed nothing in the view action. In the recordsAction, we create an instance of Jobs model class.and then call its getJobsData() method which will give us an array of records. Next we create an instance of Zend_Dojo_Data() and give the fetched data to it. And finally we echo the data converted it into the Json.
We don’t need to create a view for the records action because we have placed exit it the end of this action which will stop rendering of the view. As we will call this action form our grid view template page so no need to define phtml file for it.
However we will need to create template file for our viewAction. In scripts/views/ directory create view.phtml file and place the following code in it.
Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
$this->dojo()->setLocalPath('http://localhost/Zend/js/dojo/dojo/dojo.js')
->addStyleSheetModule(’dijit.themes.tundra’)
->addStylesheet('http://localhost/Zend/js/dojo/dojox/grid/_grid/tundraGrid.css');
echo $this->dojo();
<script type="text/javascript">
dojo.require("dojox.data.QueryReadStore");
dojo.require("dojox.grid.Grid");
dojo.require("dojo.parser");
</script>
<body class="tundra">
<div dojoType="dojox.data.QueryReadStore" jsId="activeStore", url="records"></div>
<div dojoType="dojox.grid.data.DojoData" jsId="model" rowsPerPage="20″ store="activeStore"></div>
<table id="activePastes" dojoType="dojox.grid.Grid" model="model" style="height:300px; width:700px;">
<thead>
<tr>
<th field="id">Id</th>
<th field="title">Title</th>
<th field="description">Description</th>
<th field="posted_date">Posted</th>
</tr>
</thead>
</table>
</body>
Keep in mind this is the main template file where we are creating our gird.
In the first statement I told zend to use declarative instead of programmatic behavior which is default. Next I set local path and add specific stylesheets. Then in the javascript code I used statement
Dojo.requre(’dojox.data.’) etc
To include specific dojo js modules.
In the body tag I specify a class “tundra”. This is important for the stylesheet I have added.
Next tow div’s and table are the main code for creating grid.
The first div fetch the records from the url specified. This url may be different if you fetch records form the other controller or source. Please specify valid url otherwise you will not get what you want.
The next div act as a bridge between our first div and the table. This div take data from the first div and give it to the table for display purposes.
Keep in mind that all the attributes of the div’s and table are compulsory. Removing any of the attribute may result in unvalid records or empty page. So define everything that I have defined. Once you become able to display your records, you can play with these attributes.
PHP Regular Expression
Step 1 - Regular expressions basics
PHP regular expression tutorial
PHP
regular expressions seems to be a quite complicated area especially if
you are not an experienced Unix user. Historically regular expressions
were originally designed to help working with strings under Unix
systems. Using regular expressions you can easy find a pattern in a string and/or replace it if you want. This is a very powerful tool in your hand, but be careful as it is slower than the standard string manipulation functions.
Regular expression types
There are 2 types of regular expressions:
- POSIX Extended
- Perl Compatible
The regular expressions basic syntax
To use regular expressions first you need to learn the syntax of the patterns. We can group the characters inside a pattern like this:
- Normal characters which match themselves like hello
- Start and end indicators as ^ and $
- Count indicators like +,*,?
- Logical operator like |
- Grouping with {},(),[]
Code:^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$
Code:
- $pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
- $email = "jim@demo.com";
Code:
- $pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$";
- $email = "jim@demo.com";
Now let's see a detailed pattern syntax reference:
Regular expression (pattern) | Match (subject) | Not match (subject) | Comment |
world | Hello world | Hello Jim | Match if the pattern is present anywhere in the subject |
^world | world class | Hello world | Match if the pattern is present at the beginning of the subject |
world$ | Hello world | world class | Match if the pattern is present at the end of the subject |
world/i | This WoRLd | Hello Jim | Makes a search in case insensitive mode |
^world$ | world | Hello world | The string contains only the "world" |
world* | worl, world, worlddd | wor | There is 0 or more "d" after "worl" |
world+ | world, worlddd | worl | There is at least 1 "d" after "worl" |
world? | worl, world, worly | wor, wory | There is 0 or 1 "d" after "worl" |
world{1} | world | worly | There is 1 "d" after "worl" |
world{1,} | world, worlddd | worly | There is 1 ore more "d" after "worl" |
world{2,3} | worldd, worlddd | world | There are 2 or 3 "d" after "worl" |
wo(rld)* | wo, world, worldold | wa | There is 0 or more "rld" after "wo" |
earth|world | earth, world | sun | The string contains the "earth" or the "world" |
w.rld | world, wwrld | wrld | Any character in place of the dot. |
^.{5}$ | world, earth | sun | A string with exactly 5 characters |
[abc] | abc, bbaccc | sun | There is an "a" or "b" or "c" in the string |
[a-z] | world | WORLD | There are any lowercase letter in the string |
[a-zA-Z] | world, WORLD, Worl12 | 123 | There are any lower- or uppercase letter in the string |
[^wW] | earth | w, W | The actual character can not be a "w" or "W" |
On the next page you will find some more complex regular expression with detailed explanation.
Saturday, 5 November 2011
FCBKcomplete – jQuery Plugin
Fancy facebook-like dynamic inputs with auto complete & pre added values.
Usage:
json_url - url to fetch json object
cache - use cache
height - maximum number of element shown before scroll will apear
newel - show typed text like a element
addontab – add first show element on tab or enter hit
firstselected - automaticly select first element from dropdown
filter_case - case sensitive filter
filter_hide - show/hide filtered items
filter_selected - filter selected items from list
complete_text - text for complete page
maxshownitems - maximum numbers that will be shown at dropdown list (less better performance)
maxitems - maximum item that can be added to the list
onselect - fire event on item select
onremove - fire event on item remove
delay - delay between ajax request (bigger delay, lower server time request)
attachto – after this element fcbkcomplete insert own elements
bricket – use square bricket with select (needed for asp or php) enabled by default
Add item public method (how to use):
$(“elem”).trigger(“addItem”,[{"title": "test", "value": "test"}]);
Usage:
$("element").fcbkcomplete({
json_url: "fetched.txt",
cache: true,
filter_case: true,
filter_hide: true,
newel: true
});
json_url - url to fetch json object
cache - use cache
height - maximum number of element shown before scroll will apear
newel - show typed text like a element
addontab – add first show element on tab or enter hit
firstselected - automaticly select first element from dropdown
filter_case - case sensitive filter
filter_hide - show/hide filtered items
filter_selected - filter selected items from list
complete_text - text for complete page
maxshownitems - maximum numbers that will be shown at dropdown list (less better performance)
maxitems - maximum item that can be added to the list
onselect - fire event on item select
onremove - fire event on item remove
delay - delay between ajax request (bigger delay, lower server time request)
attachto – after this element fcbkcomplete insert own elements
bricket – use square bricket with select (needed for asp or php) enabled by default
Add item public method (how to use):
$(“elem”).trigger(“addItem”,[{"title": "test", "value": "test"}]);
Youtube Video Resize
<?
$rs= stripslashes($podval['url']); // Url Path
$pattern = "/height=\"[0-9]*\"/";
$string = preg_replace($pattern, "height='199'", $rs); // Set Height
$pattern = "/width=\"[0-9]*\"/";
$string = preg_replace($pattern, "width='331'", $string); // Set Width
echo $string;
?>
$rs= stripslashes($podval['url']); // Url Path
$pattern = "/height=\"[0-9]*\"/";
$string = preg_replace($pattern, "height='199'", $rs); // Set Height
$pattern = "/width=\"[0-9]*\"/";
$string = preg_replace($pattern, "width='331'", $string); // Set Width
echo $string;
?>
Paging in ZEND
Code for pagination.php
You Can Check the Paging Site : Amrit
<?php
$baseurl=Zend_Controller_Front::getInstance()->getBaseUrl();
$image=$baseurl."/public/";
//-------------------------For query string -----------------------------------------------
$start_array = array();
$next_array = array();
$previous_array = array();
$current_array = array();
$last_array = array();
//print_r('BatchID='.$this->sproject);
if($this->prop_type !='' || $this->choose_size1 !='' || $this->zonesize !=''){
$start_array['page'] = $this->first;
$next_array['page'] = $this->next;
$previous_array['page'] = $this->previous;
$last_array['page'] = $this->last;
if($this->sproject !=''){
$start_array['prop_type'] =$this->prop_type;
$next_array['prop_type'] = $this->prop_type;
$previous_array['prop_type'] = $this->prop_type;
$last_array['prop_type'] = $this->prop_type;
}
if($this->choose_size1 !=''){
$start_array['choose_size1'] =$this->choose_size1;
$next_array['choose_size1'] = $this->choose_size1;
$previous_array['choose_size1'] = $this->choose_size1;
$last_array['choose_size1'] = $this->choose_size1;
}
if($this->zonesize !=''){
$start_array['zonesize'] =$this->zonesize;
$next_array['zonesize'] = $this->zonesize;
$previous_array['zonesize'] = $this->zonesize;
$last_array['zonesize'] = $this->zonesize;
}
if($this->submit !=''){
$start_array['submit'] =$this->submit;
$next_array['submit'] = $this->submit;
$previous_array['submit'] = $this->submit;
$last_array['submit'] = $this->submit;
}
}
else{
$start_array['page']= $this->first;
$next_array['page'] = $this->next;
$previous_array['page'] = $this->previous;
$last_array['page'] = $this->last;
}
//-------------------------For query string -----------------------------------------------
?>
<style>
.paging
{
color:#0066FF;
text-align:center;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
.paging a
{
color:#000;
text-decoration:none;
padding:0 3px;
font-size:11px;
}
.paging a:hover{color:#0066ff; text-decoration:none;}
.paging a.active{color:#009900; text-decoration:none;}
</style>
<div class='paging' style='width:100%'>
<div style='float:left;width:28%'>
</div>
<div style='width:100%;'>
<!-- First page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url($start_array); ?>">Start</a> |
<?php else: ?>
<span class="disabled">Start</span> |
<?php endif; ?>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url($previous_array); ?>">< Previous</a> |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current):
$current_array['page'] = $page;
if(!empty($this->prop_type)){
$current_array['prop_type'] = $this->prop_type;
}
if(!empty($this->choose_size1)){
$current_array['choose_size1'] = $this->choose_size1;
}
if(!empty($this->zonesize)){
$current_array['zonesize'] = $this->zonesize;
}
if(!empty($this->submit)){
$current_array['submit'] = $this->submit;
}
//echo '<pre>';print_r($next_array);die;
?>
<a href="<?php echo $this->url($current_array); ?>"><?php echo $page; ?></a>
<?php else: ?>
<?php echo $page; ?>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
| <a href="<?php echo $this->url($next_array); ?>">Next ></a> |
<?php else: ?>
| <span class="disabled">Next ></span> |
<?php endif; ?>
<!-- Last page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url($last_array); ?>">End</a>
<?php else: ?>
<span class="disabled">End</span>
<?php endif; ?>
Page <?php echo $this->current; ?> of <?php echo $this->last; ?>
</div>
</div>
Code for View page such as abc.php
Paste the code if paging is start.
<? if(count($this->dirdata)>30){ ?>
<tr height="25">
<td colspan="13" align="center" bordercolor="#333333" bgcolor="#ffffff" class="error1" >
<?=$this->paginationControl($this->paginator, 'Sliding', 'pagination.php',$this->postArr);?></td>
</tr>
<? }?>
Code for Controller
/* Start code for Paging selected*/
$postArr = $_POST;//print_r($postArr);die;
if($this->getRequest()->isPost() || $this->_getParam('page') != '')
{ //echo '<pre>';print_r($_POST);die;
if($this->getRequest()->isPost())
{
$postArr = $_POST;//echo '<pre>';print_r($_POST);
}
else{
$postArr = array("prop_type"=>$this->_getParam('prop_type'),
"choose_size1"=>$this->_getParam('choose_size1'),
"zonesize"=>$this->_getParam('zonesize'),
"submit"=>$this->_getParam('submit'));//echo "<pre>";print_r($postArr);die;
}
}
/* End code for Paging selected*/
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($dirdata);
$pagelimit=$paginator->setItemCountPerPage(30);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
$this->view->pageID=$page;
$this->view->postArr = $postArr;
Facebook Profile : Rajneesh Gautam
PHP Interview Questions
What's PHP ?
The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.
What Is a Session?
A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.
There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved by the same script or another script when requested from the same visitor.
Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.
What is meant by PEAR in php?
A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.
There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved by the same script or another script when requested from the same visitor.
Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.
What is meant by PEAR in php?
Answer1:
PEAR is the next revolution in PHP. This repository is bringing higher level programming to PHP. PEAR is a framework and distribution system for reusable PHP components. It eases installation by bringing an automated wizard, and packing the strength and experience of PHP users into a nicely organised OOP library. PEAR also provides a command-line interface that can be used to automatically install "packages"
Answer2:
PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide:
A structured library of open-sourced code for PHP users
A system for code distribution and package maintenance
A standard style for code written in PHP
The PHP Foundation Classes (PFC),
The PHP Extension Community Library (PECL),
A web site, mailing lists and download mirrors to support the PHP/PEAR community
PEAR is a community-driven project with the PEAR Group as the governing body. The project has been founded by Stig S. Bakken in 1999 and quite a lot of people have joined the project since then.
How can we know the number of days between two given dates using PHP?
Simple arithmetic:
$date1 = date('Y-m-d');
$date2 = '2006-07-01';
$days = (strtotime() - strtotime()) / (60 * 60 * 24);
echo "Number of days since '2006-07-01': $days";
Simple arithmetic:
$date1 = date('Y-m-d');
$date2 = '2006-07-01';
$days = (strtotime() - strtotime()) / (60 * 60 * 24);
echo "Number of days since '2006-07-01': $days";
What is the difference between $message and $$message?
Anwser 1:
$message is a simple variable whereas $$message is a reference variable. Example:
$user = 'bob'
is equivalent to
$holder = 'user';
$$holder = 'bob';
Anwser 2:
They are both variables. But $message is a variable with a fixed name. $$message is a variable who's name is stored in $message. For example, if $message contains "var", $$message is the same as $var.
Anwser 1:
$message is a simple variable whereas $$message is a reference variable. Example:
$user = 'bob'
is equivalent to
$holder = 'user';
$$holder = 'bob';
Anwser 2:
They are both variables. But $message is a variable with a fixed name. $$message is a variable who's name is stored in $message. For example, if $message contains "var", $$message is the same as $var.
What Is a Persistent Cookie?
A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. By default, cookies are created as temporary cookies which stored only in the browser's memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
*Temporary cookies can not be used for tracking long-term information.
*Persistent cookies can be used for tracking long-term information.
*Temporary cookies are safer because no programs other than the browser can access them.
*Persistent cookies are less secure because users can open cookie files see the cookie values.
A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. By default, cookies are created as temporary cookies which stored only in the browser's memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
*Temporary cookies can not be used for tracking long-term information.
*Persistent cookies can be used for tracking long-term information.
*Temporary cookies are safer because no programs other than the browser can access them.
*Persistent cookies are less secure because users can open cookie files see the cookie values.
What does a special set of tags do in PHP?
The output is displayed directly to the browser.
The output is displayed directly to the browser.
How do you define a constant? Via define() directive, like define ("MYCONSTANT", 100);
What are the differences between require and include, include_once? Anwser 1:
require_once() and include_once() are both the functions to include and evaluate the specified file only once. If the specified file is included previous to the present call occurrence, it will not be done again.
But require() and include() will do it as many times they are asked to do.
Anwser 2:
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. The major difference between include() and require() is that in failure include() produces a warning
message whereas require() produces a fatal errors.
Anwser 3:
All three are used to an include file into the current page.
If the file is not present, require(), calls a fatal error, while in include() does not.
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. It des not call a fatal error if file not exists. require_once() does the same as include_once(), but it calls a fatal error if file not exists.
Anwser 4:
File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once(). This will prevent problems with function redefinitions, variable value reassignments, etc.
Anwser 3:
All three are used to an include file into the current page.
If the file is not present, require(), calls a fatal error, while in include() does not.
The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. It des not call a fatal error if file not exists. require_once() does the same as include_once(), but it calls a fatal error if file not exists.
Anwser 4:
File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once(). This will prevent problems with function redefinitions, variable value reassignments, etc.
What is the difference between mysql_fetch_object and mysql_fetch_array?
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array
How can I execute a PHP script using command line?
Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, "php myScript.php", assuming "php" is the command to invoke the CLI program.
Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.
Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, "php myScript.php", assuming "php" is the command to invoke the CLI program.
Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.
I am trying to assign a variable the value of 0123, but it keeps coming up with a different number, what?s the problem?
PHP Interpreter treats numbers beginning with 0 as octal. Look at the similar PHP interview questions for more numeric problems.
PHP Interpreter treats numbers beginning with 0 as octal. Look at the similar PHP interview questions for more numeric problems.
Would I use print "$a dollars" or "{$a} dollars" to print out the amount of dollars in this example?
In this example it wouldn?t matter, since the variable is all by itself, but if you were to print something like "{$a},000,000 mln dollars", then you definitely need to use the braces.
Facebook Profile : Rajneesh Gautam
Subscribe to:
Posts (Atom)