-->

Sunday, February 2, 2014

Types of Array in PHP Programming


Types of array: - In real world we have two types of array. These are following :-

Single dimensional array: -
                                             In this array data items stored in only in one dimension.
Like $word [1] =”hello” which means the array $word stores the items in one dimension such as $word [1] $word [2] $word [3] $word [5] etc. The arrays which we have discussed in previous topics all are single dimensional array.

Multidimensional Arrays: -
                                             If an array is able to store the items at more than one dimensional is called multidimensional array. Multidimensional array comes in picture where we need to store different association with single variable. For example if we want to make a matrix of size 2x2 the array representation will be.

$mat[ 0] [0]=2
$mat[ 0] [1]=4
$mat[ 1] [0]=7
$mat[ 1] [1]=6

Here the array $mat use two direction first is for row and second for column. So this is called two dimensional array. We can create any dimensional array in PHP . Such that

$threeDarray[ ][ ][ ]
$fourDarray[ ][ ][ ][ ]
$fifthDarray[ ][ ][ ][ ][ ]
$nDarray [ ][ ][ ][ ][ ][ ][ ]……..[ ][ ]

Concept of multidimensional array can be understood with the help of example:-
$shop= array(‘fruit’ =>
array(‘red’ => ‘apple’,
‘orange’ => ‘orange’,
‘yellow’ => ‘banana’,
‘green’ => ‘pear’),
‘flower’ =>
array(‘red’ => ‘rose’,
‘yellow’ => ‘sunflower’,
‘purple’ => ‘iris’));

It is simply an array with two values stored in association with keys. Each of these values is
an array itself. After we have made the array, we can reference it like this:-
if we want to check which  fruit of color red then we use

$shop [fruit][red]; this statement return the value “apple”.

How to use Substitution Control in ASP.NET

The substitution control is used to specify a section on an output-cached Web page where you want to substitute the dynamic content for the control. This control provides a simplified solution to partial page caching for pages where the majority of the content is cached. You can output  cache the entire page, and then use the Substitution control to specify the sections of the page that are exempted from caching.

Public Properties of the Substitution class

MethodName : Callback method to call up when the Substitution control executes.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="substitution.aspx.cs" Inherits="substitution" %>
<%@ OutputCache Duration="300" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script runat="server">  
    static string dotprogramming(HttpContext context) {

        string name = DateTime.Now.ToString();
        return name;
          
    }  
</script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Page Cached DateTime<br />
&nbsp; <%= DateTime.Now.ToString() %> 
        <br />
        Current Time without cache<br />
&nbsp;<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        <asp:Substitution ID="Substitution1" runat="server" 
            MethodName="dotprogramming" />
    </div>
    </form>
</body>
</html>

In this example page cached for 300 seconds using OutputCache directive , But you can refresh content using Substitution control.
Code Generate the following Output
How to use Substitution Control in ASP.NET

How to use Substitution Control in ASP.NET

How to use HiddenField Control in ASP.NET

The HiddenField control is used to store a value that needs to persist across posts to the server. Normally, view-state, session-state, and cookies are used to maintain the state of the Web form page. In case, if these methods are disabled or are not available, you can use the HiddenField  control to store state values. Here is the class hierarchy for the HiddenField class :

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.HiddenField

Public Properties of the HiddenField Class

EnableTheming : Check, Whether theme is apply or not onto this control.
SkinID : Retrieve specific style from skin file and apply to the control.
Value : value of the HiddenField

Lets take an simple example 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Hidden Field Contro Example</h2>
        <p>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                Text="Click Me!" />
        </p>
    </div>
    <asp:HiddenField ID="HiddenField1" runat="server" />
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

// Code Behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField1.Value = "Welcome to dotprogramming";

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = HiddenField1.Value;

    }
}
Code Generate the following output
How to use HiddenField Control in ASP.NET

How to use PlaceHolder Control in ASP.NET

The PlaceHolder Control
The PlaceHolder control is used as a container to store server controls that are added to the Web page at runtime. The PlaceHolder control does not produce any visible output and is used only as a container for other controls on the Web page.

Example of PlaceHolder control

 
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox text = new TextBox();
        text.Text = "This is the runtime textbox";
        text.Style["width"] = "300px";
        PlaceHolder1.Controls.Add(text);       
           
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Click to add TextBox" OnClick="Button1_Click" />
        <br />
        <br />
        <br/>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>
Output
Add Textbox on runtime

Saturday, February 1, 2014

Dynamic and Static Memory Allocation in C Programming


Memory is allocated to the respective data variables (of any type of data structure) in two manners that are dynamic and static.
The memory if it allocated to the variables declared, during the compilation process is called as a static memory allocation whereas the allocated to the variables, during the execution of the program is called as dynamic memory allocation.
Once the memory is allocated statically it cannot be altered during the execution of the program. So the programmer may need the memory that is to be allocated as per the request during the program execution. So there comes dynamic memory allocation. Dynamic memory allocation is possible only when pointers are used. A pointer variable is used to store the base address of dynamically allocated memory.

      void main()
          {
            int array[20];
            . . . . .
         }

In this case when the program is compiled, main memory is allocated to the array. So it is static memory allocation. Once the memory is allocated to “array”, it cannot be altered during execution. Here if the programmer wishes to store more numbers of elements in the array it is not possible. He can change the size of array, only before compilation. Now if he is decides the size of array as 50 and if he is stored only few elements then there is wastage of main memory. Usually if the elements that are to be stored in an array is not decided before compilation and it is decided during the execution of the program then the static memory allocation has a major drawback.


In the order to overcome the above-mentioned problem now programmer can request the required memory during the execution of program using pointer variable. It is dynamic memory allocation.
For example, consider the following program segment:-
      
void main()
          {
            int *arr, size;
            /* user can get size of the array during execution */
           arr= (int *) malloc (size of (int) * size);
            . . . .
         }

In this case the dynamic memory is allocated by the malloc function during the execution of program and the base is returned is stored in a pointer variable arr. With the help of arr now it is possible to refer as many elements as user wishes during the execution of the program. That is the major concept of dynamic memory allocation.

Concept of Memory Allocation in C Programming


As the stored program concept specifies, the data and instructions that are to be processed should be stored in main memory of the computer, the memory allocation comes into picture. The data that is to be processed must be stored in main memory so that is readily available to the processor and can fetched and stores in internal registers for processing. Reserving space in main memory for the data variables and used for storing data in it later through assignment or input is called as memory allocation. So once the data structure is decided and variables are declared in the declaration part of the program (in case of C language) memory is allocated to the variables of the respective data structured by the compiler. This is called as memory allocation. For example, consider the following C program:-
           void main()
          {
            int a, b, c;
            . . . . .
         }
When the above  program is compiled memory of two bytes each is allocated to the variables a, b and c respectively. These memory locations are used to store two bytes signed integers (data). In the program the data are referred through variables names (identifiers). Consider another example,
         void main()
          {
            int arr[20];
            . . . . .
         }

When this program is compiled 40 bytes of consecutive memory is allocated to store 20 integers and all the integers are referred by a single variable “arr” with the proper index.
         void main()
          {
            int *arr; int size=20;
            arr= (int *) malloc (2 * size);
         }
When the above program is executed 40 bytes of memory is allocated during execution and the base address is stored in a pointer variable arr. With the help of arr now it is possible to refer 20 integers.
           The above mentioned all examples explain basic concept of memory allocation.



Delete Element from Array in PHP Programming


Delete  element in PHP array:-  If we wish to delete an item or element from PHP array then we have three choices first is remove the element  from starting position ,second is remove the elements  from last position and third is remove the element at any desire position of an array. Here following code is showing that how to implement above three operations.

Remove the element from starting position: - This can be possible to use  array_shift() PHP function. Syntax of this method is follows:-

mixed array_shift(array array);
this can be understood with the help of following code.
Let we  have an array 
$name =array(“weilems”, “jon”,”jacob”);

If we want  to remove the first name from this array then we can use array_shift() function such that
 Array_shift($name);
After execute this statement the start index value of this array is removed and the index of next elements is decremented by 1 this can be use for associative array.

Remove the element from ending position: - This can be possible to use array_pop() PHP function. Syntax of this method is follows:-

mixed array_pop(array target_array) 
this can be understood with the help of following code.
Let we  have an array 
$name =array(“jack”,“jon”,”jackren”);

If we want  to remove the last name from this array then we can use array_pop() function such that
 Array_pop($name);
After execute this statement the last index value of this array is removed. And the array becomes $name =array(“jack”,“jon”);

Remove the element at any desire position:- This is possible by using PHP unset() method. The syntax of this method is follows:-

unset(mixed vriable);
for example let we have an array such that
$branch=array(“CS”,”IT”,EE”);

And we want to remeove IT branch from this array then we can use unset() function in following way.
Unset ($branch [1]);
After execute this statement value stored at first index of array $branch will be unset and removed.
© Copyright 2013 Computer Programming | All Right Reserved