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!!

Wednesday, 21 November 2012

How to strip Specific tag from HTML using smarty?

Usually in PHP and smarty there is a function called strip_tags to strip all the tags from HTML. But in if you want to strip <img> tag alone from the HTML or any other tag from HTML content we have to use some regular expressions.

If $content is the smarty variable which holds the HTML content, then use the below regex to parse it and strip <img> tag:

{$content | regex_replace:"/(<img>|<img [^>]*>|<\\/>)/":""}

We can use this to strip any html tags. If you wanna strip <p> tag:


{$content | regex_replace:"/(<p>|<p[^>]*>|<\\/p>)/":""}

This will replace <p>, </p> and all <p many attributes> strings with an empty string.

Thats It!!

Monday, 19 November 2012

How to use print_r() in smarty?

We use print_r() in php to print the arrays recursively. If you're using smarty templating engine we can't use the same to print the arrays recursively. We have to assign the array value in a smarty variable in the corresponding php like this:

$smarty->assign('contacts', array('fax' => '555-222-9876', 'cell' => '555-111-1234'));
$smarty->display('index.tpl');

Then open index.tpl and use the variable contacts like this:
{$contacts|@debug_print_var}

Thats it! it'll print the arrays recursively same as print_r() in PHP !!

Friday, 16 November 2012

How to get specific inline styles from Javascript?

At times we need to get specific inline styles from an HTML element. We can use jQuery to get that easily by specifying like $('#id').css('marginLeft');

But it will return the value of the margin-left inline style only in pixels(px), even if u specified in percentage(%) originally. To overcome that I've come across a solution using javascript using
document.getElementById('myDiv').style.cssText;
 
Code:
 
<script type="text/javascript">
function showStyles() {
    var s = document.getElementById('myDiv').style.cssText;
    var c = s.split(";");
    for ( var i = 0; i < c.length; i++ ) {
        var p = c[i].split(":");
        alert( p[0] + " is " + p[1] );
    }
}
</script> 

<div id="myDiv" style="height: 200px; width: 150px; margin-left: -10%;">
</div>
 
The above script will alert all the inline styles one by one exactly specified. If we
Want margin-left alone then use c[3] to output that. p[0] and p[1] specifies style 
attribute and value for the same.

Saturday, 3 November 2012

how to create mobile version of wordpress website?

There are lot of ways to do this. But this is simplest of all but not simpler than responsive site. This method is used to redirect mobile users to another sub-domain. So that you can reduce the load of the server from the same domain. Here I used another sub-domain for mobile version.

First you have to add redirect function to your sub-domain (http://m.example.com). So open the functions.php from your theme folder. Add the redirect function as given below:


function check_is_mobile(){
$cur_url = esc_url("http://".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$mob_url= 'http://m.example.com/';
    if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android'))
            {
                $ex = explode("/",$cur_url);
                $postt_id= $ex[4];
                if(!empty($postt_id)){
                wp_redirect($mob_url.'post.php?id='.$postt_id);
                exit;
                }
                else{
                wp_redirect($mob_url);
                exit;
                }
            }
    }

add_action('wp_head','check_is_mobile',1);

 Then create an index.php file in your sub-domain folder and include the wp-blog-header.php. Then display list of post with the below code.
<?php require($_SERVER["DOCUMENT_ROOT"].'/blog/wp-blog-header.php');?>
  <div id="content">
<?php
     $args = array( 'numberposts' => 10, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
    $postslist = get_posts($args);
   
    foreach ($postslist as $post) : setup_postdata($post); ?>

    <?php add_filter('excerpt_more', 'new_excerpt_more');?>
       <div class="inner_box">        
           <p class="art_head"><a href="<?php echo $_SERVER["REQUEST_URI"]; the_ID(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
           <div class="description">
           <?php the_excerpt();?>
           </div>
      </div>
      <?php endforeach; ?>
    </div>

 <?php
function new_excerpt_more( $more ) {
    return '[.....]';
}
?>


 

How to extract image name from the image url in php?

Sometimes we need to extract image name from the url of the image. For that use the below preg_match script:

<?php
if (preg_match('".*?/?([^/]+?(\.gif|\.png|\.jpg))"', $url, $regs)) {
    echo $image = $regs[1];
} else {
    $image = "";
}

?>
$url is the url of the image and $regs[1] gives the name of the image.Thats it!