Search

Alves Singh

Category

Function Reference

The function reference description provides the following information:

A brief description of the purpose and operation of the function
The function parameters and values
A detailed description of the function to include, where applicable, how associated fax features apply to the function

Sort Multi-dimensional Array by Value

function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}

aasort($your_array,”order”);

Friend functions

function CheckfriendRequest($user_id, $friend_id){
global $db;
$sql1 = “SELECT * FROM user_friend WHERE (user_id = ‘”.$user_id.”‘ AND friend_id = ‘”.$friend_id.”‘) OR user_id = ‘”.$friend_id.”‘ AND friend_id = ‘”.$user_id.”‘ “;
$result1 =$db->get_row($sql1);
if($result1){
return $result1->user_friend_status;
}
}

function GetAllFriendRequest($user_id){
global $db;
$sql = “SELECT * FROM user AS u
JOIN user_profile AS up ON up.user_id = u.user_id
JOIN user_extended_profile AS uep ON uep.user_id = u.user_id
JOIN user_friend AS uf ON uf.user_id = u.user_id
WHERE uf.friend_id = ‘”.$user_id.”‘ AND uf.user_friend_status = ‘pending’ “;
$results =$db->get_results($sql);
if($results){
return $results;
}
}

function GetAllFriends($user_id=0){
global $db;
if(!$user_id)
$user_id = @$_SESSION[‘userId’];
$sql = “SELECT * FROM user AS u
JOIN user_profile AS up ON up.user_id = u.user_id
JOIN user_extended_profile AS uep ON uep.user_id = u.user_id
JOIN user_friend AS uf ON uf.user_id = u.user_id AND
u.user_id IN (select user_id from user_friend
where friend_id = ‘$user_id’ AND user_friend_status = ‘accept’
Union All
select friend_id from user_friend
where user_id = ‘$user_id’ AND user_friend_status = ‘accept’)”;
$results = $db->get_results($sql);
if($results){
return $results;
}
}

Image resizing and cropping

/*
* $file = tmp_name of the image  ($_FILES[‘image’][‘tmp_name’];)
* $path = absolute path of the image (ABSPATH.’image/’;)
* $image_name = image name ($_FILES[‘image’][‘name’];)
* $size like Small< Large, Medium
* $file_type = file extension (
$type = $_FILES[‘image’][‘type’];
$image_type = explode(‘/’,$type);
)

How to use this function

$file         = $_FILES[‘profile_image’][‘tmp_name’];
$path         = IMAGE_DIR.’gallery/’;
$image_name = $general->uniqueName($_FILES[‘profile_image’][‘name’]);
$type         = $_FILES[‘profile_image’][‘type’];
$image_type    = explode(‘/’,$type);
$file_type  = $image_type[1];

$image = resize_crop_image($file,$path,$image_name,SMALL_IMAGE_WIDTH,SMALL_IMAGE_HEIGHT,’small’,$file_type);
*/
function resize_crop_image($file,$path,$image_name,$newwidth,$newheight,$prefix,$file_type=’jpeg’){
$uploadedfile     = $file;
$image_name     = $prefix.’_’.$image_name;
$src = @imagecreatefromjpeg($uploadedfile);
if($file_type==’jpeg’ OR $file_type==’jpg’){
$src = @imagecreatefromjpeg($uploadedfile);
}
if($file_type==’gif’){
$src = @imageCreateFromGif($uploadedfile);
}
if($file_type==’png’){
$src = @imageCreateFromPng($uploadedfile);
}
if($file_type==’bmp’){
$src = @imageCreateFromBmp($uploadedfile);
}
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

$tmp     = @imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
@imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = $path. $image_name;
@imagejpeg($tmp,$filename,100);

@imagedestroy($src);
@imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.
return $image_name;
}

Generate MySQL data dump in SQL from PHP

backup_tables(‘localhost’,’root’,”,’like_minder’);

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = ‘*’)
{

$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//get all of the tables
if($tables == ‘*’)
{
$tables = array();
$result = mysql_query(‘SHOW TABLES’);
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(‘,’,$tables);
}
//echo “<pre>”; print_r($tables); echo “</pre>”;

//cycle through
$return = ”;
foreach($tables as $table)
{
$result = mysql_query(‘SELECT * FROM ‘.$table);
$num_fields = mysql_num_fields($result);

//$return = ‘DROP TABLE ‘.$table.’;’;
$row2 = mysql_fetch_row(mysql_query(‘SHOW CREATE TABLE ‘.$table));
$return.= “\n\n”.$row2[1].”;\n\n”;

for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= ‘INSERT INTO ‘.$table.’ VALUES(‘;
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = @ereg_replace(“\n”,”\\n”,$row[$j]);
if (isset($row[$j])) { $return.= ‘”‘.$row[$j].'”‘ ; } else { $return.= ‘””‘; }
if ($j<($num_fields-1)) { $return.= ‘,’; }
}
$return.= “);\n”;
}
}
$return.=”\n\n\n”;
}

//save file
$dbname = $name;
$handle = fopen($dbname.’_’.date(“Y-m-d_H-i-s”).’.sql’,’w+’);
fwrite($handle,$return);
fclose($handle);
}

=========================================================

/* backup the db OR just a table into csv formate */
function backup_tables_csv($tables = ‘*’)
{
if($tables == ‘*’)
{
$tables = array();
$result = mysql_query(‘SHOW TABLES’);
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables     = is_array($tables) ? $tables : explode(‘,’,$tables);
}
$date         = date(“Y-m-d_H-i-s”);
$dbname       = DB_NAME;
$folder       = ABS_PATH.’db/’.$dbname.’_’.$date;
$directory    = mkdir($folder, 777);
$csv_output   = ”;
foreach($tables as $table)
{
$filename   = $folder.’/’.$table.’.csv’;
$fp         = fopen($filename, “w”);
$result     = mysql_query(“SHOW COLUMNS FROM “.$table.””);

$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row[‘Field’].”,”;
$i++;
}
}
$csv_output .= “\n”;
$values = mysql_query(“SELECT * FROM “.$table.””);

while ($rowr = mysql_fetch_row($values)) {
for ($j=0; $j<$i; $j++) {
$csv_output .= $rowr[$j].”, “;
}
$csv_output .= “\n”;

}
fputs($fp, $csv_output);
fclose($fp);
$csv_output = ”;
}

//echo “<pre>”; print_r($values); echo “</pre>”;
}

Dymanic year select box

$months = array (1 => ‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’);
echo ‘<select name=”month”>’;
foreach ($months as $key => $value) {
echo “<option value=\”$key\”> $value</option>\n”;
}
echo ‘</select>’;
echo ‘<select name=”day”>’;
for ($day = 1; $day <= 31; $day++) {
echo “<option value=\”$day\”> $day</option>\n”;
}
echo ‘</select>’;
echo ‘<select name=”year”>’;
$year = date(‘Y’);
for ($year; $year >= 1960; $year–) {
echo “<option value=\”$year\”> $year</option>\n”;
}
echo ‘</select>’;

Blog at WordPress.com.

Up ↑