Author Archives: om

How to merge two mysql query results into one array?

You can use UNION operator to merge the result-set of two or more SELECT statements into one array.
The default behavior for UNION is that duplicate rows are removed from the result. UNION ALL does not remove duplicate.

There are few things need to be kept in mind.

A. SELECT statement within the UNION must have the same number of columns.
B. The field should have similar data types.
C. Fields in each SELECT statement must be in the same order if not make them.
D. If column have diffrent name make column another name by using an alias with “AS” kewords as given in the example

SELECT column1 AS alias_name FROM table

Here is the full example of UNION.


	  $select_btemp_1 = "SELECT * FROM table1 WHERE enable='1' AND aid='1'";

	  $select_btemp_2 = "SELECT * FROM table1 WHERE enable='1' AND user_id='$user_id' ORDER BY  aid ASC";

	  $select_btemp =  $select_btemp_1." ".UNION." ".$select_btemp_2;

	  $btemp_query = mysql_query($select_btemp);
	  $btemp_count = mysql_num_rows($btemp_query);

	  if($btemp_count > 0)
	  {
		while($row_images = mysql_fetch_array($btemp_query))
		{ 	
			echo $row_images[temp_desc];	
		}
	  }

	
Share

How to select all check boxes with one click ?

Selecting multiple items from a list to process them or delete them is just one like of jquery program.



Check all
Checkbox 1
Checkbox 2
Checkbox 3

Here is another version which does not use fieldset

    
Check all
Checkbox 1
Checkbox 2
Checkbox 3
Share

How to pass select box drop down value to multiple text boxes?

This code allow you to select dropdown value to multiple text boxes which have same name or class.
Very useful if you are creating price table and want to pass data to next page.


    

 



You can pass select drop down value to one text box without using jQuery.

      
    

 
What would you like to select
Share

Mac: How to copy files from hard disk to USB drive through command line ?

In order to copy really big files or full directory coping through command line is good idea.
It will be fast and easy. In your mac you can find source directory easily but how to find destination directory?

Go to root and check the volume directory. /Volumes
Now its time to check where is your destination directory and where you want to keep your files.
In my case /Volumes/Seagate Backup Plus Drive/my_download

cp -R 1_j_download/ /Volumes/"Seagate Backup Plus Drive"/my_download 

There are 2 points
A. cp has -R which is used to copy entire directory.
B. “Seagate Backup Plus Drive” directory in my case has quote because of space. If you don’t use quote probably it will not work! However you can name your drive without space.

It will take few minutes to copy really big chunks of data… Still it depends on size.

Share

PHP: How to dynamically resize image in your page?

Every php programmer/developer have to resize images in web pages.
There are a lot of ways available through JavaScript, jQuery and PHP.
Here is very simple way to resize image temporarily but proportionally.
You can still keep big image file in you in your folder but when you are displaying you can make is smaller.

Jquery and java script some times does not work. So PHP way is very useful.


 ($inputwidth/$inputheight)) {
            $outputwidth = $inputwidth;
            $outputheight = ($inputwidth * $height)/ $width;
        }

        elseif (($width/$height) < ($inputwidth/$inputheight)) {
            $outputwidth = ($inputheight * $width)/ $height;
            $outputheight = $inputheight;
        }

        elseif (($width/$height) == ($inputwidth/$inputheight)) {
            $outputwidth = $inputwidth;
            $outputheight = $inputheight;
            }

echo '';

?>
Share

How to save remote image in server or save image from a url?

Saving images from any live url is very easy with php.
Here are three ways to do this.
1. Using Curl
2. Using File functions
3. Using GD library functions.

Condition is in your server php.ini should have “allow_url_fopen” On. check you php setting using phpinfo() function.

Create a folder name “images_saved” to save your images.

Using Curl

Image ' . basename($i) . ' Downloaded Successfully';
    }else{
        echo '

Image ' . basename($i) . ' Download Failed

'; } } function image_save_from_url($my_img,$fullpath){ if($fullpath!="" && $fullpath){ $fullpath = $fullpath."/".basename($my_img); } $ch = curl_init ($my_img); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); $rawdata=curl_exec($ch); curl_close ($ch); if(file_exists($fullpath)){ unlink($fullpath); } $fp = fopen($fullpath,'x'); fwrite($fp, $rawdata); fclose($fp); } ?>

Using File functions


Using GD library functions.


Share

PHP – Add a custom application tab to facebook fan page ?

I spend a lot of time fingering out how to add “Custom Tab” in face-book fan page.
In fact this is very easy through PhpFacebook API.

You need to have “manage_pages” permission during user authorization.

This code will allow you to get control of all fab pages. This code is good for someone who is willing to add his tab/app to fab page. This is not good for normal user.

$logoutUrl = $facebook->getLogoutUrl();

	$params = array(
		 'scope' => 'offline_access, email, publish_stream, manage_pages',
		'redirect_uri' => 'https://apps.facebook.com/your-app-name/'
	); 

If you logged in successfully this code will show you all fan pages with Id and names etc.

$pageIds=$facebook->api('/me/accounts');

print_r($pageIds); // this will show you array data. This is key part and show you access_token which is very imp.


         [0] => Array
                (
                    [name] => My App Tester
                    [access_token] => AAAD3pm1y4sQBADkZA4mT1UNFzUyQZBw9g1aH2CCriZCAHyqmP6rvHurcZC0qShvPFhPB0R4CWP7TMxVzHp2ktepcrM1XZBheZCoZD
                    [category] => Computers/internet
                    [id] => 56789349409295678
                    [perms] => Array
                        (
                            [0] => ADMINISTER
                            [1] => EDIT_PROFILE
                            [2] => CREATE_CONTENT
                            [3] => MODERATE_CONTENT
                            [4] => CREATE_ADS
                            [5] => BASIC_ADMIN
                        )

                )

$pageAccessToken=$pageIds["data"][1]["access_token"];

Now it is time to post you can use Ajax or any way you like to post. PAGE_ID is your fan page ID/56789349409295678.

$facebook->api("/PAGE_ID/tabs","post", array("access_token" => $pageAccessToken,  "app_id" => $appId));

Now you can store PAGE_ID in database and when you will run this code then one tab will be added in your fan page.

Now your user can access whatever you add in your app.

use this code in your app page to correlate custom app for different user since you already have PAGE_ID stored in db

";

print_r(parse_signed_request($signed_request, $secret));
?>

Using this method you can create multiuser fab page tab application and make some money by adding extra features!

Share

How to filter bad words?

You can use this simple function to filter bad words.

Please see attachment for mysql table of frequently used 458 bad words. http://www.phpmind.com/codedemo/php-bad-word-filter/bad_word.txt.zip
You can add more word easily as you know more!!


  function bad_wordcensor($txt)
  {
    $q = mysql_query("SELECT bad_word, replacement FROM bad_words");
    while ($row_bad = mysql_fetch_array($q))
    {
      $txt = str_ireplace($row_bad['bad_word'], $row_bad['replacement'], $txt);
    }

  return $txt;
  }

echo bad_wordcensor($_POST["comments"]);
Share