Sunday, May 29, 2011

[PHP] What is the difference between sort & asort in php ?


Many times developers are not aware with the core functions of PHP. PHP have sort and asort built in array functions. You may or may not be aware with these functions. But when you will face a PHP interview than you may be asked for this question. What is the difference between sort & asort in php ? From the name itself, you can say it will sort an array elements but you may not be aware with the exact difference.
sort() function will sort an array by values and array keys will be automatically reset.
asort() function will sort an array by values and array keys will be the same as per original array.

<?php
$fruits = array(“lemon”, ”orange”, ”banana”, ”apple”);
sort($fruits);
foreach ($fruits as $key => $val) {
echo ”fruits[" . $key . "] = ” . $val . ”\n”;
}
?>
o/p:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
o/p:
c = apple
b = banana
d = lemon
a = orange

CakePHP PaginatorHelper-Sorting problem solved


Hello Friends,
CakePHP provide its own Pagination helper using which you can easily add paging and sorting functionality in your CakePHP application. Using this helper class for one model, all instruction is given in below link. If you works with more than one model. means using belongsTo or hasMany relation and getting records from more than one table than paging is as simple as one table. But for sorting, you might face some problem. This can be solved by adding Model name before field name. Look at below syntax.

$paginator->sort(‘Title’, ‘Category.title’); ?>
If you find any problem in this than let me know by comment.

How to use OR in find method – CakePHP


Hello Friends,
When any developer is working with any web application, he/she will constantly interact with the Select Query. As i am working with CakePHP, i also need to use Select Query often. For Select Query in CakePHP, you can use find method of Model which gives you the data as per conditions given in find method. If you write more than one conditions in conditions array passed in find method than by default it will take OR clause and make a query string with “AND“. If you want to do OR operation or any other operation than you need to mention that in find method. Look at below syntax.
 <?php
$this->Post->find(‘all’, array(‘conditions’ => $conditions));
$conditions = array(“id”=>”5″,”name”=>”abc”);
//Above condition will make a select query with AND.(WHERE id=5 AND name=’abc’)
$conditions = array(“OR”=>array(“id”=>”5″,”name”=>”abc”));
// If you define “OR” as per above statement than the query will be WHERE id=5 OR name=’abc’
?>
If you find any problem in this than let me know by comment.

Set Meta Tags (SEO Tags) in CakePHP


Hello Friends,
In my previous post, i show you the way how to set Page Title Tag in CakePHP. But setting Meta keywords and Meta Description is not the same way as Meta Title. .Also Meta Keyword and Meta Description is important part of Search Engine Optimization. Refer to below syntax to set Meta tags in CakePHP.
<?php
echo $html->meta(‘keywords’,'enter any meta keyword here’);
?>
//Output <meta name=”keywords” content=”enter any meta keyword here”/>
<?php
echo $html->meta(‘description’,'enter any meta description here’);
?>
//Output <meta name=”description” content=”enter any meta description here”/>
If you find any problem in this than let me know by comment.


Set Meta Title (Page Title) in CakePHP 1.2/1.3


Hello Friends,

As i mention in my previous posts, CakePHP 1.3 come up with lots of new syntax changes. I was looking for a Meta Title SEO tag for each page. I need to set this Meta Title (Page Title) for each page. You must be aware that Page Title can be set in method which we write in respective controller. Look at below syntax changes in CakePHP 1.2 and CakePHP 1.3 for setting Page Title.
CakePHP 1.2
$this->pageTitle = “Page Title Value”;
CakePHP 1.3
$this->set(“title_for_layout”,”Page Title Value”);
If you find any problem in this than let me know by comment.


Zip Component in CakePHP-Add files in Zip


Hello Friends,
Today i have implemented a Zip functionality in CakePHP. If you want to create an archive file consists of some files to give a download Zip fileoption to user than i might help you to show the steps. Below are the steps how to zip all files and save it to some folder.
$this->Zip->addFile($src_file,$file_name_inzip);
$this->Zip->saveZip(“myzipfile.zip”);
First you can download the file given in link and place above file in components directory in your app/controller folder. Now you can call the function addFile for adding files in zip. Once you will call this function it will read all data from src_file. Now you can save those data to Zip file by calling saveZip function.
If you find any problem in this than let me know by comment.





Problem in Mozilla Firefox-Easy AJAX Pagination Using JQuery CakePHP


Hello Friends,
I was working with CakePHP’s Easy AJAX Pagination Using JQuery. I found this as a great code for sorting and paging with jQuery inCakePHP. But i faced a problem in Mozilla Firefox. It is not working in Mozilla Firefox. While in IE and Crome it works fine for me. For Mozilla Firefox when i debug it, it shows an error like “501 Method Not Implemented”. I searched for this error and Google gives the solution likeClearing Cache and Cookie. But neither works for me. Finally i got the issue and the problem is in jQuery load function.



 /**
* Loads in a URL into a specified divName, and applies the function to
* all the links inside the pagination div of that page (to preserve the ajax-request)
* @param string href The URL of the page to load
* @param string divName The name of the DOM-element to load the data into
* @return boolean False To prevent the links from doing anything on their own.
*/
function loadPiece(href,divName) {
$(divName).load(href, {}, function(){
var divPaginationLinks = divName+” #pagination a”;
$(divPaginationLinks).click(function() {
var thisHref = $(this).attr(“href”);
loadPiece(thisHref,divName);
return false;
});
});
}
 If you open the above link and look at jQuery load function. They have passed the second parameter as {} for data. So that was the problem in Mozilla Firefox. You can remove the {} from jQuery load function.
If you find any problem in this than let me know by comment.

How to restrict users from accessing files & directory level(Protect directory) using .htaccess


Hello Friends,
Today i faced a situation where i have to restrict or deny users from accessing files and folders of my web application directly by entering URL in browser which contain very important contents likeimages,pdfs,docs,etc. For this what i need to do is to protect the files and folders(directory) which contains images,pdfs and docs. We can restrict users from accessing files,folders and subfolders by using.htaccess(Hyper Text Files).
Using .htaccess, you can restrict or deny access to individual files and folders. Below is the example how to restrict or deny users from accessingimages folder. Make a .htaccess files and upload it in images folder. So this will be effective only for images folder(not for whole site).
# This will restrict all users from accessing images folder directly from web.
deny from all
If you would like to ban only one IP or range of IP than look at the below rules.







# Restrict only 192.168.0.1 IP user.
order allow,deny
deny from 192.168.0.1
allow from all
# Restrict an IP range from 192.168.0.0 to 192.168.0.10
order deny,allow
deny from 192.168.0.0/10
allow from all
To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database,php info,php editor,programming php,Open-source,php help and php script , subscribe to our feed by entering email address below. You will get updates via email about every tutorial posted on this site . It will not take more than a sec.

301 Redirect old dynamic URLs to new static URLs using .htaccess




Hello Friends,
Today i faced a situation where i need to redirect dynamic URL of my shopping cart to static url. I have changed the Dynamic URL(with query string , ? and &) to static one in my sunshop shopping cart. But still if i will open dynamic url with query string than it shows the content. This is a big issue with search engine as they will consider this dynamic page and static page as two different page and duplicate content issue araise.
Redirect 301 rule in .htaccess (which is below) is common rule everybody knows.
Using .htaccess
redirect 301 http://www.domain.com/about-us.html http://www.domain.com/about.html
Using PHP
What i want is to redirect dynamic urls(with query string , ? and &) to static page. For e.g. If i want to redirect http://www.domain.com/content.php?a=1&b=2 to http://www.domain.com/content.html than look at the below htaccess rules.
RewriteCond %{HTTP_HOST} ^domain.com
RewriteCond %{QUERY_STRING} ^a=1&b=2$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/content.html [R=301,L]
Add the above three lines and change the domain name and query string as per your need. Now whenever you will enter http://www.domain.com/content.php?a=1&b=2 , it will redirect to http://www.domain.com/content.html. So now Search engine will consider both as one page and duplicate content issue will not araise.
To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database,php info,php editor,programming php,Open-source,php help and php script , subscribe to our feed by entering email address below. You will get updates via email about every tutorial posted on this site . It will not take more than a sec.