max_execution_time ?
max_input_time ?
memory_limit ?M
post_max_size ?M
upload_max_filesize ?M
Friday, July 29, 2011
Thursday, July 28, 2011
A simple and useful function for escaping user inputs.
After some testing I have developed a function for escaping user inputs. the escaped values can be directly used for inserting to database. It is working fine for me for the last few years..
function safe_escape($string)
{
if(get_magic_quotes_gpc()){
$string = htmlentities($string);
}else{
$string = mysql_real_escape_string($string);
$string = htmlentities($string);
}
return $string;
}
function safe_escape($string)
{
if(get_magic_quotes_gpc()){
$string = htmlentities($string);
}else{
$string = mysql_real_escape_string($string);
$string = htmlentities($string);
}
return $string;
}
Monday, July 11, 2011
Sorting a multidimensional array in PHP - intelligent method
It is not at all easy to sort a multidimensional array.. After spending hours of time we have find out a solution for it.. check the following:
For eg: I need to sort this multidimensional array based on the message_name
in ascending order.
So I put
$sort = "message_name";
$order_by = "asc";
$asc = create_function ('$a,$b','return strcasecmp($a["'.$sort.'"], $b["'.$sort.'"]);');
$desc = create_function ('$a,$b','return strcasecmp($b["'.$sort.'"], $a["'.$sort.'"]);');
if($order_by=="asc")
usort($rows,$asc);
else
usort($rows,$desc);
print_r($rows)
Hope this will help some one.. :)
Array ( [0] => Array ( [message_email] => test3@test.com [message_subject] => p message2 [message_date] => 2011-06-02 00:00:00 [message_id] => 4 [message_name] => test name2 [user_id] => 26 [user_display] => Administrator ) [1] => Array ( [message_email] => test@test.com [message_subject] => test product sub1 [message_date] => 2011-06-01 00:00:00 [message_id] => 3 [message_name] => tet name1 [user_id] => 26 [user_display] => Administrator ) )
For eg: I need to sort this multidimensional array based on the message_name
in ascending order.
So I put
$sort = "message_name";
$order_by = "asc";
$asc = create_function ('$a,$b','return strcasecmp($a["'.$sort.'"], $b["'.$sort.'"]);');
$desc = create_function ('$a,$b','return strcasecmp($b["'.$sort.'"], $a["'.$sort.'"]);');
if($order_by=="asc")
usort($rows,$asc);
else
usort($rows,$desc);
print_r($rows)
Hope this will help some one.. :)
Tuesday, July 5, 2011
Finding root parent of a child category in php
Using the below recursive function we can able to find the root parent category of a child category.
function getRootParent($id){
$query = "select nav_parent_id from navigation_categories where nav_id='$id'";
$result = $this->db->query($query);
foreach($result->rows as $ids){
if($ids['nav_parent_id']==0){
return $id;
}else{
return $this->getRootParent($ids['nav_parent_id']);
}
}
return $id;
}
function getRootParent($id){
$query = "select nav_parent_id from navigation_categories where nav_id='$id'";
$result = $this->db->query($query);
foreach($result->rows as $ids){
if($ids['nav_parent_id']==0){
return $id;
}else{
return $this->getRootParent($ids['nav_parent_id']);
}
}
return $id;
}
Listing subcategories or chilld categories using php
We can use the following function for getting the subcategories up to any level. It is using recursive technique.
function getSubCats($catid){
$query= "select cat_id from category where cat_parent_id='$catid'";
$rst = $this->db->query($query);
if($rst->num_rows==0){
return null;
}else{
foreach($rst->rows as $row) {
$categories[] = $row['cat_id'];
$children = $this->getSubCats($row['cat_id']);
if($children!=null){
foreach($children as $child){
$categories[] = $child;
}
}
}
return $categories;
}
}
function getSubCats($catid){
$query= "select cat_id from category where cat_parent_id='$catid'";
$rst = $this->db->query($query);
if($rst->num_rows==0){
return null;
}else{
foreach($rst->rows as $row) {
$categories[] = $row['cat_id'];
$children = $this->getSubCats($row['cat_id']);
if($children!=null){
foreach($children as $child){
$categories[] = $child;
}
}
}
return $categories;
}
}
Subscribe to:
Posts (Atom)