Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

User registration form is a good example for this validator. Checking the username exists or not.

Db_NoRecordExists validator helps to achieve this

$username = new Zend_Form_Element_Text('username',
 array('label' => 'Username',
     'required' => true,
     'filters' => array('StripTags', 'StringTrim'),         
     'validators' => array('NotEmpty', array('Db_NoRecordExists',false,array(
  'users_table','username_column', 'messages' => array( Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND => '%value% already exists')
 )))));

Execute system commands from PHP

System commands can be executed from PHP using exec command.
eg:

$out = array();
$command = "memcat --server=10.10.10.10 ITEMS > /tmp/items.txt";
exec($command, $out);
The $out variable would hold the output of the command. If there is any issue with executing a command , have the command with path.
eg:
$command = "/usr/local/bin/memcat --server=10.10.10.10 ITEMS > /tmp/items.txt";
exec($command, $out);
for further info: http://www.php.net/manual/en/function.exec.php

Merge two arrays together using array_merge function



$result = array_merge(array(0=>"Hello"), array(3=>"World"));



The result will have an array with keys got re-numbered
therefore the key 3 will be changed to 1.

If you want to keep your array keys unchanged




$result = $arr1 + $arr2;


That will do it.

Debugging view of 2D array

If you are in a position to have a better view of 2 dimensional array then try this



$fruits["apple"] = array("Canada","US","Extra");


print_r($fruits) won't show a better view but the following way would show a clear view of arrays and values



echo "
";
print_r($fruits);
echo "
";



Enjoy.
cheers.

isset, empty functions of PHP

Always pay attention to these functions.


$a = 0;
// isset() - treats $a as not empty
if(isset($a))
echo "a has ".$a;

// empty() - treats $a as empty

having trouble with empty() with the editor

if(empty($a))
echo "a has ".$a;


both functions will display "a has 0".

Reading a file into a string in PHP

If you'd like to get the entire content of a file into a string then follow the way



// have your filename with path
$file_name = "yourfilename.ext";
$content_string = file_get_contents($file_name);
echo $content_string; // will display the content


want to display total chars in the file?


echo strlen($content_string);


To read file into array.


// have your filename with path
$file_name = "yourfilename.ext";
$content = file($filename);
foreach($content as $content_row)
echo $content_row;

Passing a PHP array to Javascript

PHP array:


$fruits = array("Apple","Mango","Banana");

convert $fruits to Javascript array


echo 'var fruits = new Array("',join($fruits,'","'),'");';


The above statement will convert PHP array to Javascript array. This saves a lot of time.

PHP Introduction

1.Hello World example

Use any of your favourite text editor and type your first php code and save it with “helloWorld.php”. Upload it to your web server and then execute it.


echo “Hello World”;


to execute : http://www.yourserver.com/helloWorld.php ( place it in the root directory )

semicolon means end of a line.


2.Conditional Check if..else

check for a condition to be true or false. Why do you need to check for conditions? Take a scenario like this. If you are checking the site in the morning I should say "Good morning", so how do we handle this now.


// if time is less than 12'o clock the site would display
// "Good Morning"
if($time < 12)
echo "Good Morning";


How do we say good evening now?

if($time < 12)
echo "Good Morning";
else // time is more than 12
echo "Good Evening";






Where are those curly braces? If you use one statement just after the if ..condition then you don't need it but if you have more than one line then you shuld open and close curly braces.


eg:
if($time < 12) {
echo "Good Morning";
$num_of_visits = $num_of_visits + 1;
}




3.Looping

Loop is used to repeat a job as many time you want

while loop


$i=1;
while($i<5) {
echo “Hello World”.$i;
}


The above code snippet would display
Hello World 1Hello World 2Hello World 3Hello World 4


for…loop


for($i=0; $i<5; $i++)
echo $i;



The loop will display 01234 as output.



do…while

some times you need your loop to execute at least once regardless of condition


$x = 5;
do {
echo “Hello World”;
}while($x<5);


though x=5 initially, the loop would execute once because the conditional statement is at the end. When it reaches the end the condition x<5 returns false the loop will not execute any more.



Some Links (off programming)