Pages

Friday, 13 February 2015

Integrating PHP with Embedded System : Chapter -4

Associative array in PHP 

  • Associative array is an array where each ID key is associated with a value.

    Associative array example: 

    <html> 

       <body>

        <?php 

/* Here rose, daisy and orchid indicates ID key and 5.00, 4.00, 2.00 indicates their values respectively*/

             $flower_shop = array ( 

                        "rose" => "5.00", 

                        "daisy" => "4.00", 

                        "orchid" => "2.00"

             ); 

// Display the array values

             echo "rose costs

                        .$flower_shop['rose'].",daisy costs

                        ".$flower_shop['daisy'].",and orchid

                        costs ".$flower_shop['orchid']."";

        ?>

   </body>

</html> 

OUTPUT of the above given Example is as follows:
rose costs 5.00,daisy costs 4.00,and orchid costs

Loop through an Associative Array

<html>
   <body>
        <?php
             $flower_shop=array("rose"=>"5.00",
                   "daisy"=>"4.00","orchid"=>"2.00");

             /* for each loop works only on arrays, and is used
             to loop through each key/value pair in an array */
             foreach($flower_shop as $x=>$x_value) {
                   echo "Flower=" . $x .
                        ", Value=" . $x_value;
                   echo "<br>";
             }
        ?>
        </body>
</html>
OUTPUT of the above given Example is as follows:

Flower=rose, Value=5.00
Flower=daisy, Value=4.00
Flower=orchid, Value=2.00


Multidimensional array in PHP        
§  Multidimensional array is an array containing one or more arrays.

Multidimensional array Example

<html>

<body>

<?php 

   /* Here $flower_shop is an array, where rose, daisy and orchid
   are the ID key which indicates rows and points to array which
   have column values. */
$flower_shop = array( 
"rose" => array( "5.00", "7 items", "red" ), 
"daisy" => array( "4.00", "3 items", "blue" ), 
"orchid" => array( "2.00", "1 item", "white" ), 
); 
/* in the array $flower_shop['rose'][0], ‘rose’ indicates row and ‘0’ indicates column */
echo "rose costs ".$flower_shop['rose'][0].
", and you get ".$flower_shop['rose'][1].".<br>";  

echo "daisy costs ".$flower_shop['daisy'][0].
   ", and you get ".$flower_shop['daisy'][1].".<br>"; 

echo "orchid costs ".$flower_shop['orchid'][0].
   ", and you get".$flower_shop['orchid'][1].".<br>";
?>
</body>
</html>


OUTPUT of the above given Example is as follows:
rose costs 5.00, and you get 7 items.
daisy costs 4.00, and you get 3 items.
orchid costs 2.00, and you get 1 item



PHP Forms  
  • Scripts will interact with their clients using one of the two HTTP methods. The methods are GET and POST
  • When a form is submitted using the GET method, its values are encoded directly in the query string portion of the URL
  • When a form is submitted using the POST method, its values will not be displayed the query string portion of the URL.

The $_GET Function 

  • The built-in $_GET function is used to collect values from a form sent with method="get"
  •  Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's URL) and has limits on the amount of information to send (max. 100 characters)
  • This method should not be used when sending passwords or other sensitive information. However, because the variables are displayed in the URL, it is possible to bookmark the page. 
  • The get method is not suitable for large variable values; the value cannot exceed 100 characters.

The $_POST Function

  • The built-in $_POST function is used to collect values from a form sent with method="post".
  • Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
  • However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).

The $_GET Function Example

Form1.html
<html>
    <body>
        /* form submitted using ‘get’ method, action specifies
        next page which is to be loaded when button is clicked*/

  <form action="welcome.php" method="get">
                // textbox is to take user input
            Name: <input type="text" name="fname" />
            Age: <input type="text" name="age" />
            // Submit button is to submit the value
            <input type="submit" />
        </form>
    </body>
</html>

welcome.php
<html>
   <body>
             // $_GET to receive the data sent from Form1.html
        Welcome <?php echo $_GET["fname"]; ?>.<br />
        You are <?php echo $_GET["age"]; ?> years old!
   </body>
</html>

 OUTPUT of the above given Example is as follows:


In this example, when you click on the “submit” button, the values entered in the text box are encoded directly in the query string portion of the URL.