Thursday, 30 May 2013

PHP ARRAY

Like another programming language php also having array.An array is define as a series of elements of the same type placed in contiguous memory locations.

Type of array in PHP

  1. Numeric Array.
  2. Associative Array.
  3. Multidimensional Array.
Numeric Array: - 

  • In numeric array store each element with a numeric index.There are generally two methods to create a numeric array:
  1. $computer = array("Acer”,”Lenovo”,”deal”,”hp”);                                                                           In this the index value are automatically assigned to each array element starts from 0.
  2.                                                                                                                                              $computer[0] = “Acer”;                                                                                                 $computer[1] = “Lenovo”;                                                                                               $computer[2] = “deal”;                                                                                                  $computer[3] = “hp”;                                                                                                                      In this we assign the index value to each element in array.
Example:
                        <?php
                        $computer[0] = “Acer”;
                        $computer[1] = “Lenovo”;
                        $computer[2] = “ deal”;
                        $computer[3] = “hp”;
                        Echo $computer[0].”and”.$computer[1].”are cheap and best”;
                        ?>
Output: Acer and Lenovo are cheap and best
Associative Array:  In this each id is associative with a value.
Numeric array is not best in storing data about specific named so we can use associative array. With associative array we can use the value as key and assign to them.
Example :     <?php 
                      $employee = array (“Ram”=>23,”Sham”=>24,”Pankaj”=>23);  Or
                      $employee [‘Ram’] =”23”;
                      $employee [‘Sham’] = “24”;
                      $employee [‘Pankaj’] = “23”;
                      echo “Ram is”.$employee[‘Ram’].”Year old”; ?>
Output: Ram is 23 year old.
Multidimensional Array: In this an array element contain array or we can say that each element in the main array can also be an array. And each element in the sub-array can be an array.
Example:
Array
                        (
                        [kumar]=> array
                                    (
                                                [0] =>pankaj
                                                [1]=>amit
                                    )
                        [singh]=> array
                                    (
                                                [0]=> sumit
                                    )
                        ) 

Tuesday, 28 May 2013

Change the page title case in Wordpress

One can change the page title case according to page post id.
  <?php
     
       $page_title_case_change_arr = array(1,2,55);
       $page_id = $post->ID;
       $head_style = "";
       if(in_array($page_id,$page_title_case_change_arr)){
            $head_style = "style='text-transform:uppercase'";
       }
      echo "<h2".$head_style.">".$page_title."</h2>"; ?>
  ?>

In the above example first you have to declare the array of page where you want to change the case of header title in $page_title_case_change_arr variable. Then get page post ID by $page_id.

Then create a empty variable named as $head_style. After that check if a $page_id  value exists in an array if it's then add a inline css.

Monday, 27 May 2013

Array Key Case Change


Using array_change_key_case function we can easily change case of array key.
Syntax: array array_change_key_case (array, $input);

Example: 
        $array_exp = array("my"=>1, "country"=>2);
        $output = array_change_key_case($array_exp,CASE_UPPER);
        print_r($output);

       Output: Array ( [MY] => 1 [COUNTRY] => 2 )

case either CASE_LOWER  or UPPER_CASE

Thursday, 23 May 2013

Add Script in Wordpress


Add Script in Wordpress:  
         Syntax: wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer);
          
           $handler: Name use to handle the script.
           $scr :Is the URL of the script.
           $ver: define script version number.
          
Example:
         <?php

          function add_script(){
           wp_enqueue_script('Jquery', plugin_url('jquery-ui-1.10.1.custom.min'), '');
           }
          add_action('int',add_script);

         ?>
      
          
    

Monday, 20 May 2013

Add menu page with diffrent name from sub menu page in Wordpress

In wordpress if you are creating  a plugin the first sub menu page name is same as of plugin main menu page.
like if the name of plugin menu is Fund the by default the name of its first sub menu is also Fund.

To change this sub menu page you can make the slug for the submenu page equal that of the top level page.

add_action('admin_menu', 'my_plugin_menu');

function my_plugin_menu() {  
    add_menu_page('Page Title', 'Menu Title', 'manage_options', 'mymenu', 'display_menu);
    add_submenu_page('mymenu', 'sub_menu1 ', 'Sub menu name', 'manage_options','mymenu');
   add_submenu_page('mymenu', 'sub_menu2 ', 'Sub menu name2', 'manage_options','mymenu2');


   

Error Reporting in PHP


In this article we are discussing error reporting in php and we also discuss why  error reporting is important during development and why it is important to turn it off.

for example:
<?php
echo $var = "pan "
$var2 = "pan2";

?>

In this example i missing the line terminator or $var which is a semi colon. At that moment my error reporting is off. So if i run this code nothing has to be return  no error and no output. This is because of error reporting off.

So we have to go to the php.ini file located in php folder and  find error_reporting and modified its value by 0 to E_ALL and then save the file and restart Apache server. And then if we refresh our output page the it will show an parse error.

Wednesday, 15 May 2013

Class change using Javascript.


<Html>
<head>
<style>
.intro
{
color:green;
background-color:blue;

}
</style>
</head>
<body>

 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
alert("hello")
});

function fun1()
{

$('p').addClass('intro');

}

function fun2()
{

$('p').removeClass('intro');

}

function fun3()
{

$('p').toggleClass('intro');

}
 </script>
<p>Hello</p>
<input type="button" onclick="fun1()" value="pune1">
<input type="button" onclick="fun2()" value="pune2">
<input type="button" onclick="fun3()" value="pune3">
</body>
</html>

Tuesday, 14 May 2013

Hide On Click


<html>
<head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<div id="d1">
Test Content
</div>
<span id="s1">click me to hide-></span>
<script>
$("#s1").click(function(){
$("#d1").hide();
})
</script>
</body>
</html>

Saturday, 11 May 2013

PHP ARRAY


Like another programming language php also having array. Array is a special variable which can used to store multiple values of same data type.
Array is used to hold all values under one single name and we used that value by referring that name of array. Array can be accessed by its index value.

Types of Array:

  1. Numeric Array
  2. Associative Array
  3. Multidimensional Array.
  • Numeric Array:  In numeric array store each element with a numeric index. There are generally two methods to create a numeric array:
                  a)      $computer = array("Acer”,”Lenovo”,”deal”,”hp”);
     In this the index value are automatically assigned to each array element starts from 0.
b)      $computer[0] = “Acer”;
c)      $computer[1] = “Lenovo”;
d)     $computer[2] = “deal”;
e)      $computer[3] = “hp”;
                     In this we assign the index value to each element in array.
Example:
<?php
                        $computer[0] =  “Acer”;
                        $computer[1] = “Lenovo”;
                        $computer[2] = “ deal”;
                        $computer[3] = “hp”;
                        Echo $computer[0].”and”.$computer[1].”are cheap and best”;
 ?>

Output: Acer and Lenovo are cheap and best.

  • Associative Array: In this each id is associative with a value.
Numeric array is not best in storing data about specific named so we can use associative array. With associative array we can use the value as key and assign to them.

Example :
$employee = array (“Ram”=>23,”Sham”=>24,”Pankaj”=>23);
                                                      Or
                  $employee [‘Ram’] =”23”;
                  $employee [‘Sham’] = “24”;
                  $employee [‘Pankaj’] = “23”;
Example :
<?php
                  $employee [‘Ram’] =”23”;
                  $employee [‘Sham’] = “24”;
                  $employee [‘Pankaj’] = “23”;
                  echo “Ram is”.$employee[‘Ram’].”Year old”;
 ?>
Output: Ram is 23 year old.

  • Multidimensional Array.: In this an array element contain array or we can say that each element in the main array can also be an array. And each element in the sub-array can be an array.
Example:
Array
                        (
                        [kumar]=> array
                                    (
                                                [0] =>pankaj
                                                [1]=>amit
                                    )
                        [singh]=> array
                                    (
                                                [0]=> sumit
                                    )
                        )




Friday, 10 May 2013

Variables in PHP

Variables are used to store information or a value to its. Like other programming language php have its own variable .but in php there is no need to define its data type it will be automatically regret its vale that it is of integer or character type.

In php variable define with a sigh of ‘$’ i.e.
$a, $abc etc.
The first character after $ must be character or underscore. The remaining character in name may be letter , number or underscore.
Syntax :
$variable_name = value;
Example:
$a
$a=10;
$b=20;
$c= $a+$b;

Open a Connection to the MySQL Server in PHP

 Basic syntex:

mysql_connect("hostname","usename","password","dbname")

Example:
<?php
     $con= mysql_connect("xyz.com","pan","pan123","my_db");
     if(!con){
          echo "Could not connect to database".mysql_connect_error();
         
      }
?>