Pages

Wednesday, November 28, 2012

Configure Eclipse for Android Dev/Tools not found Error

Just started up with my first development with Android here is the link to how to setup Android ADT Package in Eclipse...
 
http://developer.android.com/sdk/eclipse-adt.html#installing
 
Here is the link for complete guide.
 
http://developer.android.com/sdk/installing.html#Installing
 
And if you are still getting an error like tools not found in android sdk in Eclipse
  
Run Android SDK Manager from start menu 
 
On the top you will see SDK Path

copy that path(you won't be able to copy it practically.I meant just take a note of that path)

run eclipse 
 
it will show an error that tools not found in a given directory

click on window menu select preferences

select Android from the list on left hand side 
 
on the right hand side you will find "SDK Location". Copy that path next to that and Bingo issue resolved 

Indicating a value as decimal literal in C Sharp

A value such as 14.5 is taken as a double by default by C Sharp and which cannot be automatically typecasted in decimal.

So indicate its data type as decimal we append M at its back

for example

decimal variable= 14.5M

Sunday, November 25, 2012

PHP Fatal error: Class 'CTestCase' not found Yii

PHP Fatal error:  Class 'CTestCase' not found

Running the command from the folder where bootstarp.php for testcases resolves this issue

Try running the tests from \protected\tests

Installing PHP Unit and Selenium and configuring it on Netbeans IDE

Follow the following links and tutorials to install and configure both(wanted it to be a one stop tutorial).

Follow this to install PEAR
http://itfeast.blogspot.in/2012/11/installing-pear.html

Installing PHPUnit

http://pear.phpunit.de/

To  Install Selenium 

Make sure you have Pear and phpunit installed

Then to install Selenium
Use this command
pear install Testing_Selenium-beta

To configure all this on Net-beans use this link
 http://netbeans.org/kb/docs/php/phpunit.html

Sunday, November 11, 2012

Adding php and phpunit to Windows default path

Adding php and phpunit to Windows default path

Open System in control panel=>Advance System Setting=>Envirnoment Variables=>

You will find a variable path in

Path

append to it your php directory..that's it

 Here is an image of the same...



Installing PHP Unit/Nothing to Upgrade error message

First of all we need to install PEAR as stated in my post

http://itfeast.blogspot.in/2012/11/installing-pear.html

Now that we have installed PEAR if success we will notice a PEAR folder in our php directory

also in our PHP directory will find a PEAR command line interface file

We need to place this a windows root path so that we can execute or files from everywhere for this refer

http://itfeast.blogspot.in/2012/11/adding-php-and-phpunit-to-windows.html


Now...

Installing PHP Unit

Fire the following command
pear channel-discover pear.phpunit.de
 
followed by
pear install phpunit/PHPUnit
 
wait for a few time depending on your internet speed phpunit will get downloaded and installed
 
If you get something similar to this you are done with phpunit installation 
 
C:\wamp\bin\php\php5.4.3>pear channel-discover pear.phpunit.de
Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded

C:\wamp\bin\php\php5.4.3>pear install phpunit/PHPUnit
Unknown remote channel: pear.symfony.com
Did not download optional dependencies: phpunit/PHP_Invoker, use --alldeps to
wnload automatically
phpunit/PHPUnit requires package "channel://pear.symfony.com/Yaml" (version >=
.1.0)
phpunit/PHPUnit can optionally use package "phpunit/PHP_Invoker" (version >= 1
.0)
phpunit/PHPUnit_MockObject can optionally use PHP extension "soap"
downloading File_Iterator-1.3.3.tgz ...
Starting to download File_Iterator-1.3.3.tgz (5,152 bytes)
.....done: 5,152 bytes
downloading Text_Template-1.1.4.tgz ...
Starting to download Text_Template-1.1.4.tgz (3,701 bytes)
...done: 3,701 bytes
downloading PHP_CodeCoverage-1.2.6.tgz ...
Starting to download PHP_CodeCoverage-1.2.6.tgz (155,960 bytes)
...done: 155,960 bytes
downloading PHP_Timer-1.0.4.tgz ...
Starting to download PHP_Timer-1.0.4.tgz (3,694 bytes)
...done: 3,694 bytes
downloading PHPUnit_MockObject-1.2.2.tgz ...
Starting to download PHPUnit_MockObject-1.2.2.tgz (20,347 bytes)
...done: 20,347 bytes
downloading PHP_TokenStream-1.1.5.tgz ...
Starting to download PHP_TokenStream-1.1.5.tgz (9,859 bytes)
...done: 9,859 bytes
install ok: channel://pear.phpunit.de/File_Iterator-1.3.3
install ok: channel://pear.phpunit.de/Text_Template-1.1.4
install ok: channel://pear.phpunit.de/PHP_Timer-1.0.4
install ok: channel://pear.phpunit.de/PHP_TokenStream-1.1.5
install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.2.6
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.2.2

C:\wamp\bin\php\php5.4.3> 
 

Now there are times we get an error while updating phpunit "Unabable to update PHP UNIT"

Message comes "Nothing to upgrade"

Follow following steps to upgrade phpunit in such cases

pear clear -cache

pear install -a-- phpunit/phpunit

and phpunit will upgrade successfully

Selecting random row from mysql database

Here is a simple query to select random row from a phpmysql database

Select * from `users` ORDER BY rand() limit 0,1

where users is the name of my table

Installing PEAR

PEAR is a very good package which contains pre defined built classes for various functionality which prevents one to re invent the wheel

I use mostly for TDD Test driven development which needs phpUnit which requires PEAR

Installing PEAR is very simple all you have to do is install php or a vanilla package like wamp, mamp, xampp, lamp now

download the file

http://pear.php.net/go-pear.phar

and place it under your php folder I use Wamp vanilla package so I will place under C:\wamp\bin\php\php.version

Its a windows machine

Now open your command line interface(windows) or shell(linux) and navigate to directory of your php folder and execute

php go-pear.phar

Under windows Vista,7 or in some versions of linux you might get a permission denied message in such  cases you need to run command prompt under admin mode.

To do so in windows under start->all programs->accessories->(right click on command prompt and select run as administrator to get the things work out for you)

Thursday, November 8, 2012

Wordpress plugins posting form or data to another Page in plugin

Wordpress plugins posting form or data to another Page in plugin

 <form id="mikex-settings" action="<?php echo plugins_url('update.php', __FILE__) ?>">  
   <label>Search Text:</label> <input name="search" value="<?php echo $options['search'] ?>" type="text" /><br />  
   <label>Replace Text:</label> <input name="replace" value="<?php echo $options['replace'] ?>" type="text" /><br />  
   <label>Replace Color</label> <input name="replace-color" value="<?php echo $options['replace-color'] ?>" type="text" /><br />  
   <label>Toggle Color:</label> <input name="toggle" value="<?php echo $options['toggle'] ?>" type="text" /><br />  
   <input type="submit" value="Update" /><span class="update-status"></span>  
 </form>  


In this way we an call another page to show up completely of a plugin in case of searchboxes etc

Add Metaboxes and Metafields to your wordpress posts

Add Metaboxes and metafields to your wordpress posts


 add_action('add_meta_boxes','mypost_add_custom_box');  
 add_action('save_post','myplugin_save_postdata');  
 function mypost_add_custom_box() {  
   add_meta_box(  
     'myplugin_sectionid',  
     __('Post Type Selection','myplugin_textdomain'),  
     'myplugin_inner_custom_box',  
     'post'  
   );  
 }  
 function myplugin_inner_custom_box( $post ) {  
  wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );  
  // The actual fields for data entry  
  $scheckval=esc_attr(get_post_meta($post->ID,'slider_post',true));  
  $pcheckval=esc_attr(get_post_meta($post->ID,'popular_post',true));  
  if($scheckval=='on')  
  echo '<input type="checkbox" id="myplugin_slider_field" name="myplugin_slider_field" checked="checked"/>';  
  else  
  echo '<input type="checkbox" id="myplugin_slider_field" name="myplugin_slider_field"/>';  
  echo '<label for="myplugin_slider_field">';  
     _e("Slider Post", 'myplugin_sdomain' );  
  echo '</label> ';  
  if($pcheckval=='on')  
  echo '<input type="checkbox" id="myplugin_popular_field" name="myplugin_popular_field" checked="checked"/>';  
  else  
  echo '<input type="checkbox" id="myplugin_popular_field" name="myplugin_popular_field"/>';  
  echo '<label for="myplugin_popular_field">';  
     _e("Popular Post", 'myplugin_pdomain' );  
  echo '</label>';  
 }  
 /* When the post is saved, saves our custom data */  
 function myplugin_save_postdata( $post_id ) {  
  // verify if this is an auto save routine.  
  // If it is our form has not been submitted, so we dont want to do anything  
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )  
    return;  
  // verify this came from the our screen and with proper authorization,  
  // because save_post can be triggered at other times  
  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )  
    return;  
  // Check permissions  
  if ( 'page' == $_POST['post_type'] )  
  {  
   if ( !current_user_can( 'edit_page', $post_id ) )  
     return;  
  }  
  else  
  {  
   if ( !current_user_can( 'edit_post', $post_id ) )  
     return;  
  }  
  // OK, we're authenticated: we need to find and save the data  
  if($_POST['myplugin_slider_field']=='on')  
  $smydata=$_POST['myplugin_slider_field'];  
  else  
  $smydata='off';  
  if($_POST['myplugin_popular_field']=='on')  
  $pmydata=$_POST['myplugin_popular_field'];  
  else  
  $pmydata='off';  
  update_post_meta($post_id,'slider_post',$smydata);  
  update_post_meta($post_id,'popular_post',$pmydata);  
 }  

Easily get posts with a specific custom field/value on your WordPress blog

Easily get posts with a specific custom field/value on your WordPress blog

 <?php query_posts('meta_key=review_type&meta_value=movie'); ?>  
 <?php if (have_posts()) : ?>  
 <?php while (have_posts()) : the_post(); ?>  

WordPress $post Variable Keys

WordPress $post Variable Keys
Below are the methods of retrieving the information that you are most likely to need.
  • $post–>ID – ID of the current post.
  • $post–>post_category - Retrieves the ID of the post category.
  • $post–>post_parent - ID of the page parent. Useful for creating custom navigational elements.
  • $post–>post_title – Post Title
  • $post–>post_excerpt - Post excerpt
  • $post–>post_content – Retrieves all of the post content, along with any markup.
  • $post–>post_name – Retrieving the slug of a post.
  • $post–>guid – Post Url
  • $post–>post_author – ID of post author post_parent
  • $post–>post_type - Returns the type, page or post.
  • $post–>menu_order - Returns the menu order as set in the post/page editing window. Often menu items are sorted via this value.
  • $post–>post_date - Retrieves the integer timestamp for when the post was published. The output can be customized. See the php.net date manual.
  • $post–>post_modified - Retrieves the integer timestamp for when the post was last modified.
  • $post–>post_status - Retrieves one of five possible posts statuses: publish, private, draft, pending, future.
  • $post–>comment_count - Returns the number of comments, pings, and trackbacks for a given post.
 And here is how to use it

 <?php if (have_posts()) : while (have_posts()) : the_post();  
 //somewhere in the loop  
 $yourvariable = $post->post_content;  
 //do something  
 endwhile; endif; ?>  

Wordpress get posts on basis of name,category,tag

Since we have post name involved here long with category and tag we need to use custom query here which will provide us to search specific posts which contain special text in name and will belong to a particular category and will contain a tag as per requirements

Here is the custom query

I used a get in my search form with normal request parameters

 if (!(empty($_GET['section'])) || !(empty($_GET['article']))) {  
       global $wpdb, $post, $paged, $max_num_pages, $current_date;  
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  
       $post_per_page = intval(get_query_var('posts_per_page'));  
       $offset = ($paged - 1) * $post_per_page;  
       $val = $_GET['s'];  
       $cat = $_GET['category'];  
       $tag = $_GET['tag'];  
 $querystr = "  
   SELECT DISTINCT $wpdb->posts.*  
   FROM $wpdb->posts  
   LEFT JOIN $wpdb->postmeta wpostmeta ON ($wpdb->posts.ID = wpostmeta.post_id)  
   LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)  
   LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)  
     WHERE  
     $wpdb->posts.post_type = 'post'  
     AND $wpdb->posts.post_status='publish'";  
 //Tags and categories were like dropdowns so each time either "0" for no select or tag_id or //category_id or both were sent via GET as url params  
       if($tag > 0 && $cat > 0)  
       {  
         $querystr = $querystr . " AND ($wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'post_tag'  
       AND $wpdb->term_taxonomy.term_id IN($tag))) OR ($wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'category'  
       AND $wpdb->term_taxonomy.term_id IN($cat)))";  
       }  
       elseif ($tag > 0) {  
         $querystr = $querystr ." AND $wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'post_tag'  
       AND $wpdb->term_taxonomy.term_id IN($tag))";  
       }  
       elseif ($cat > 0) {  
               $querystr = $querystr . " AND $wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'category'  
         AND $wpdb->term_taxonomy.term_id IN($cat))";  
          }  
       $pageposts = $wpdb->get_results($querystr, OBJECT);  
       $querystr = $querystr . " LIMIT " . $offset . ", " . $post_per_page . "; ";  
       $sql_posts_total = $wpdb->get_var("SELECT FOUND_ROWS();");  
       $max_num_pages = ceil($sql_posts_total / $post_per_page);  

 //And can use pagination as
  <?php if ($wp_query->max_num_pages > 1) : ?>  
           <nav id="<?php echo $nav_id; ?>">  
             <div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">&larr;</span> Older posts', 'old')); ?></div>  
             <div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">&rarr;</span>', 'next')); ?></div>  
           </nav>  
   <?php endif; ?>   


This is one way out...

Other way out is to put this filter in your functions.php

 add_filter('posts_where','wpse_posts_where', 10, 2 );  
 function wpse_posts_where( $where, &$wp_query )  
 {  
   global $wpdb;  
   if ( $wpse_title = $wp_query->get( 'wpse_title' ) ) {  
     $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $wpse_title ) ) . '%\'';  
   }  
   return $where;  
 }  


and then make a call to

 <?php if(!(empty($_GET['category'])) || !(empty($_GET['tag'])))  
 {  
   $val=$_GET['s'];  
   $cat=$_GET['category'];  
   $tag=$_GET['tag'];  
      global $query_string;  
      $qstr= $query_string."wpse_title=$val";  
      $args['wpse_title']=$val;  
      if($cat>0)  
      {  
           $args['category__and']=$cat;  
           $qstr=$qstr."&cat=$cat";  
      }  
      if ($tag>0)  
   {  
           $args['tag__in'] = $tag;  
           $qstr=$qstr."&tag=$tag";  
   }  
      query_posts($args);  
      query_posts($qstr);  
 ?>  


They both work how ever in later case youcan either make via name and category or name and tag

If you try to search via name category and tag you will end up getting a response which contains all the posts of that category..so I preffered using a custom query as it suited me

Tuesday, November 6, 2012

.htacess rewrite rule translation example

RewriteRule  ^builder/([0-9]*)/([0-9]*)/([a-z]*)/([a-z]*)$   wp_content/plugins/career/mkr.php?page_id=$1&r_id=$2&user=$3&rtype=$4 [L]


This rule means that is we encounter a url which starts with builder followed by an integer again followed by an integer then an alphabet then an alphabet and then end convert it to the url after space for example

A Url

domain.com/builder/3/6/alpha/beta will be converted to

domain.com/wp_content/plugins/career/mkr.php?page_id=3&r_id=6&user=alpha&rtype=beta

That's how translation occurs

Unable to upload files from form

enctype=multipart/form-data

Always when adding input type="file" in your form add this parameter to your form tag as a property.....



now what does this mean

When submitting a form, you're trying to say your browser to send via the HTTP protocol a message on the network properly enveloped in a TCP/IP protocol message structure. When sending data, you can use POST or GET modes to send data using HTTP protocol. POST tells your browser to build an HTTP message and put all content in the header of the message ( a very useful way of doing things, more safe and also flexible). GET has some constraints about data representation and length.
When sending a file, it is necessary to tell HTTP protocol that you are sending a file having several characteristics and information inside it. In this way it is possible to consistently send data to receiver and let him open the file with the current format and so on... This is a requirement from the HTTP protocol as shown here:http://www.w3.org/TR/html401/interact/forms.html
You cannot send files using default send enctype parameters because your receiver might encounter problems reading it (consider that a file is a descriptor for some data for a specific operating system, if you see things this way, maybe you'll understand why it is so important to specify a different enctype for files).
This way of doing things also ensures that some security algorithms work on your messages. This information is also used by application-level routers in order to act as good firewalls for external data.

Session data lost in chrome and Internet Explorer

In your header file or which ever file that loads first add the following piece of code

if(session_id()=='')
session_start();

This is a very simple way to start of session.Mostly now a days frameworks have this set already in their bootstrap files but still if you are loosing session you can start off session with this.

wordpress media upload cheating ' uh issue

Its and issue with media_upload.php and comes with some plugins on updating Wordpress

The solution is simple

if using SMOF
 
add_action('init','optionframework_mlu_init');
'posttype'=>'options'

If not using SMOF

tb_show('','media_upload.php?post_id='+jquery('#post_ID').attr('name')+'....)

find this line and change  

'post_id=0'

that's it

I know it's changing the core but if plugin does not releases its update that's how we need to manage.

Difference in == & === with example

I had a scenario in WordPress where to update and insert a record I used

$wpdb->insert and
$wpdb->update

now they both on success return "no of rows" and "1" on failure they return "0" and "0"(of type boolean which is false)

I was using same variable To check for their result as such on their failure I had to use I had to use

if($res= = =false)
{
}.
elseif($res= =0)
{
}

Redirection facebook authentication "An Error occured please try lator"

Check that app host address is same as in redirect url parameter of your PHP SDK

$params = array(
  'scope' => 'read_stream, friends_likes',
  'redirect_uri' => 'https://www.myapp.com/post_login_page'
);

$loginUrl = $facebook->getLoginUrl($params);