Results 1 to 4 of 4

Thread: PHP learning :Do it by yourself

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Location
    Kerala, India
    Posts
    17,476

    Default PHP learning :Do it by yourself

    My Experiments with PHP

    The following program defines and initializes an array to store data and the contents are displayed with a php program.

    Code:
    <?php
    // Defining photos as an array
    $photos = array();
    // Initialising the array with values
    $photos['0'] = "Cat";
    $photos['1'] = "Rat";
    $photos['2'] = "Tiger";
    $photos['3'] = "Lion";
    $photos['4'] = "Bear";
    $photos['5'] = "Buffalo";
    $photos['6'] = "Giraffe";
    //count the elements in an array and stores the value in variable number
    $number = count($photos);
    
    echo 'The number of animals in the array photos is '.$number.'<br><br>'; 
    // Displaying the elements of the array separated by a line break
    for ($i=0;$i<$number;$i++) {
        echo $photos[$i].'<br>';
    }
    -------> Tested with Firefox browser.

  2. #2
    Join Date
    Sep 2006
    Location
    Kerala, India
    Posts
    17,476

    Default Random password generator

    The program generates a random 12 character password.

    Code:
    <?php
    // creating a 12 character random password
    /* rand function generates a random integer
    The syntax is  : rand ( [int $min, int $max] )
    and it gives an integer output
    you will get an integer between minimum and maximum numbers specified 
    (both inclusive) as output. 
    The minimum value for $min is zero (0). */
    
    $r1 = rand(1,255);
    $r2 = rand(1,255);
    $r3 = rand(1,255);
    
    /* Here the three random numbers are concatenated using the '.' operator
    dechex function converts a dDecimal to hexadecimal
    <?php echo dechex(10); will convert decimal 10 to hexadecimal number a. */
    
    $number = dechex($r1) . dechex($r2) . dechex($r3);
    
    $rand = rand(1,9999);
    
    /*crc32 function calculates the crc32 polynomial of a string. 
    Syntax is  : crc32 (string $str)
    It generates checksum polynomial of 32-bit length and can be 
    negative integer too. 
    It is usually used to validate the integrity of data being transmitted. 
    To get an unsigned checksum use printf function : printf("%u\n", $checksum); */
    
    $number=dechex(crc32($number)).$rand;
    echo $number;

  3. #3
    Join Date
    Sep 2006
    Location
    Kerala, India
    Posts
    17,476

    Default STD Code Finder

    Program : STD Code Finder

    The program std.html displays a form with a drop down menu. When the user clicks the 'Get STD Code' button, std.php will be called and the data corresponding to the place selected is taken from the database and is displayed.

    std.html

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> Enter the place & get the STD code </TITLE>
     </HEAD>
    
     <BODY>
        <B>STD Code Finder</B><BR>&nbsp;
        <FORM METHOD=POST ACTION="std.php">
            
            <SELECT NAME="place" size=10>
                
                <OPTION VALUE="Abhohar">Abhohar
                <OPTION VALUE="Adampur">Adampur
                <OPTION VALUE="Adilabad">Adilabad
                <OPTION VALUE="Adipur">Adipur
                <OPTION VALUE="Adityapur">Adityapur
                <OPTION VALUE="Adoni">Adoni
                <OPTION VALUE="Adoor">Adoor
                <OPTION VALUE="Aduthurai">Aduthurai
                <OPTION VALUE="Agartala">Agartala
                <OPTION VALUE="Agra">Agra
                <OPTION VALUE="Ahmedabad">Ahmedabad
                <OPTION VALUE="Ahmednagar">Ahmednagar
                <OPTION VALUE="Ahwa">Ahwa
                <OPTION VALUE="Aizwal">Aizwal
        
                <INPUT TYPE="submit" value = " Get STD Code "><BR>
            </SELECT>
                        
        </FORM>
    
         </BODY>
    </HTML>

  4. #4
    Join Date
    Sep 2006
    Location
    Kerala, India
    Posts
    17,476

    Default STD Code finder

    Code:
    std.php
    
    <?php
    
    set_magic_quotes_runtime(0);
    // Connecting to database named yp_std using optional 
    // username & password (vshare)
    $db = mysql_connect('localhost','vshare','vshare');
    mysql_select_db('yp_std',$db);
    
    if (isset($_POST['place']) && $_POST['place'] != '') {
        $place = $_POST['place'];
        // trim whitespaces if any
        $place = trim($place);
        // searches the std table for the inputted place. 
       //MySql query defined.
        $query = "SELECT * FROM `std` WHERE 
                     `place`='" . mysql_clean($place) . "'";
    } else {
        $query = 'SELECT * FROM std';
    }
    // Fetches the record which has place matching with the inputted place.
    $result = mysql_query($query);
    
    echo "<B>STD Code Finder</B><BR><BR>";
    if ($result) {
        while ($record = mysql_fetch_assoc($result)) {
             echo 'STD Code for ' . $record['place'] . " is " . $record['std'];
        }
    }
    
    //MySql clean function for cleaning sql query
    function mysql_clean($query)
    {
        if (!is_numeric($query)) {
            return $query;
        } else {
            return mysql_real_escape_string($query);
        }
    }
    Before executing the program you have to create a database named 'yp_std' with a table 'std'. The table has the following fields : id (primary key, auto increment, numeric), place (varchar 255) and std (varchar 255).

    $db = mysql_connect('localhost','vshare','vshare');

    Here the second and third parameters (database username & password - vshare) are optional.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •