Tuesday, 29 January 2013

PHP-Removing Strings or non-integer values from an array?

I you want to remove some non-integer values from an array or string values fro an array, follow the following steps:
$array1= array( [0] => 1
         [1] => 2
         [2] => 3
         [3] => Some strings
        );
 
The above contains the array with some strings in it in the 4th index.
We remove it by 
 
$filtered_array = array_filter($array1, 'is_numeric');

Yes. We must use array_filter() in PHP to filter non-integer array values.

We can also use the below code to achieve same result.
$filtered_array = array_filter($array1, 'ctype_digit');
$filtered_array = array_filter($array1, 'is_int');

Now it'll print all array values expect the index which has 'Some strings'.

Monday, 10 December 2012

Youtube API tutorial PHP - Setup in xampp

In this blog We're gonna see how to set up youtube api environment in php xampp. Google clearly gives the documentation for this here. But still I'm stuck at the middle somewhere. Because I've never configured this before. So this is absolutely for beginners.

Download the Google Data Client Library files from the zend website, as we use Zend Gdata for Youtube API. We dont have to install zend framework for this to work. Just download Zend_Gdata from the above link which is located at the bottom of the Zend framework page.

Extract the folder and add the location of the library folder to your PHP path. We can set the path by 2 ways. One by using set_include_path() method and other by using setting path using php.ini file. For me it is located in C:\xampp\php. Open php.ini and search for include_path variable. I extracted Zend_Gdata in C:\xampp\ and rename the folder as ZendGdata.

Now edit the include_path variable by including this path C:\xampp\ZendGdata\library. Make sure there is a library folder in that path. Now restart your apache server and test whether the script works by going to this script. It is installation checker script for this. If all the scripts were passed then it'll show no errors found. I got one error when running this script. It is openssl error. We can solve it by uncommenting ;extension=php_openssl.dll to extension=php_openssl.dll in php.ini file and restarting the apache server. Then I got no errors.

Now run the demo files in the demo folder of Zend_Gdata folder(copy the demo folder to htdocs folder). Thats It! It'll work like charm!

 

Wednesday, 5 December 2012

How to use ajax using jQuery load method for appending data with fade in effect?

Usually we use $.ajax or $.post to use ajax in our script. But now we are gonna use load() to send ajax requests to server.

var offset = 0;   
$("#button").click(function(){      
        offset = offset+5;
    $("#morePosts").load("/post.php?offset="+offset, function(data) {   
    if($(data).is(':empty')){
            $(data).hide().appendTo('#morePosts').fadeIn("slow");           
    }else{
     $("#button").attr({"disabled":"disabled", "value":"No more posts to display"});   
    }
});
        return false;
});

I've used this in wordpress to load more posts in ajax. It'll display 5 posts each time when we click #button.

In the first parameter we are giving URL with offset as parameter to post.php(GET request).
Second parameter is the callback function used to get the data from the post.php after processing GET request.

 data is the returned data from post.php and we are checking whether the data is empty or not
using is(':empty') and appending that data to #morePosts div.

Then we are using appendTo() where we first hide the data and appending it to #morePosts div and then showing it by fadeIn()

If the data is empty we disable the button attribute using attr({"disabled":"disabled"}).

Thats it!

How to toggle with fade effect in jQuery?

Sometimes you need to toggle with fade in and fade out effects in jQuery. Toggle allows you to perform click and revert that click, or you can assign different functions to every click like this:

$('p').toggle(function(){
        alert("First click");
},function(){
       alert("Second Click");
},function(){
        alert("Third click");
});

In the above code we are using <p> tag as selector and if we click for the first time, it'll alert "First click" and if we click it for the second click, it'll alert "Second click" and so on.

fadeOut() will hide the content by fading it out and fadeIn() will show the hidden content. We are gonna use these effects to toggle content with fade effect.

$(':button').toggle(function(){
        $('img').fadeOut("slow");
},function(){
        $('img').fadeIn("slow");
});


The above code will hide and show images in your page when you clicked that button($(':button') ), with fade effect. Thats It!

Monday, 26 November 2012

How to install memcached on xampp - win 7

First download Win32 binary version of memcached here. Extract in any of ur folder outside xampp. I installed in c:/memcached/memcached.exe.

Then Go to your php.ini file usually located in C:/xampp/php/php.ini and find this line:

;extension=php_memcache.dll

then uncomment this line like this:

extension=php_memcache.dll

If you can't find this line add it at the end of the php.ini file:
Then add the following lines beneath this line:
[Memcache]  
memcache.allow_failover = 1  
memcache.max_failover_attempts=20  
memcache.chunk_size =8192  
memcache.default_port = 11211
 
Then download the memcached extension from here. For me php_memcache-5.4-vc9-x86 it works.
 Because I installed latest version of xampp. If the version id not right then it won't work. I've been searched for the correct version for whole day and finally found mine. 
 
Then extract that package in side c:/xampp/php/ext . Now restart ur Apache & MySql.
 
Then goto ur memcahed folder using command prompt and type like this:
c:\memcached\memcached.exe -d install  
If it say already installed or didn't show any errors then its been installed. Then start memcached using this command:
c:\memcached\memcached.exe -d start
 
Now create any test php file and test with memcache class like code given below:
 
<?php  
$memcache = new Memcache;  
$memcache->connect('localhost', 11211) or die ("Could not connect");  
  
$version = $memcache->getVersion();  
echo "Server's version: ".$version."<br/>\n";  
  
$tmp_object = new stdClass;  
$tmp_object->str_attr = 'test'; 
$tmp_object->int_attr = 123; 
 
$memcache->set('key', $tmp_object, false, 10) or  
die ("Failed to save data at the server"); 
echo "Store data in the cache (data will expire in 10 seconds) 
<br/>\n"; 
 
$get_result = $memcache->get('key');  
echo "Data from the cache:<br/>\n";  
  
var_dump($get_result);  
?> 

Thats It!

 
 

Friday, 23 November 2012

Why $smarty->is_cached() emits fatal error in smarty 3?

When we use the cache function is_cached() in smarty version 3 it'll emit fatal error as shown below:

Fatal error: Uncaught exception 'SmartyException' with message 'unknown method 'is_cached''

I've been searched all over the internet and couldn't find out the solution why it shows this error in latest version alone. But in documentation it is not deprecated. Then what the hell is going on with smarty 3.

Finally I found out that they changed the syntax of the function is_cached() in latest version.
According to latest version we have to declare it as isCached()

if(!$smarty->isCached("post.tpl",$postid)){
 $smarty->assign('image_url',$img_path);

$smarty->display('post.tpl',$postid);

We have to give cache id($post_id) for the function isCached() to make it work.Thats It!! I hope it works for u!!