ch.7a array and time

Upload: wfscmgbh

Post on 30-May-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Ch.7a Array and Time

    1/30

    Hong Kong Baptist University

    Week 7

    Array and Time

    01/03/2010

    Dr. Tony Chun-Kuen WONG

    Coordinator, BBA(Hons) Information Systems Management Major

    Coordinator, BBA(Hons Information S stems and e-Business Mana ement

    Concentration

    Department of Finance and Decision Sciences

    School of Business

  • 8/9/2019 Ch.7a Array and Time

    2/30

    Hong Kong Baptist University

    Arrays

    n arrays s a s o var a es

    The index number start with 0 rrays n exe y str ngs are nown as assoc at ve arrays

    Arrays that contain arrays as their elements are known as

  • 8/9/2019 Ch.7a Array and Time

    3/30

    Hong Kong Baptist University

    Example (using number as index)

    $users = array (Ludwig, Tony,Billy);

    // or

    $users[ ] = Ludwig;

    =

    $users[ ] = Billy;

    print $users[1]; //return Tony

  • 8/9/2019 Ch.7a Array and Time

    4/30

    Hong Kong Baptist University

    Example (using string as index)

    $character = array (name=>Ludwig,occupat on=> teac er ,

    age=>30,

    special knowledge=>SMALL expert);

    // or$character[name] = Ludwig;

    $character[occupation] = teacher;

    $character[age] = 30;

    $character[special knowledge] = SMALL expert;

  • 8/9/2019 Ch.7a Array and Time

    5/30

    Hong Kong Baptist University

    Multidimensional Array

    amp e rogramme

    http://isem03.hkbu.edu.hk/~isemuser20/array/Array01.php

    previous two examples.

    Assessing by $user[numerical index][string index]

  • 8/9/2019 Ch.7a Array and Time

    6/30

    Hong Kong Baptist University

    Accessing arrays

    The index of the last element is count($users)1

    The Count ( ) will return the size of the array

    Looping through an array (Sample Programme 2 and 3) http://isem03.hkbu.edu.hk/~isemuser20/array/Array02.php

    http://isem03.hkbu.edu.hk/~isemuser20/array/Array03.php

    foreach($array as $temp)

    {

    //

    }

    //or

    foreach($array as $key=>$value)

    //}

  • 8/9/2019 Ch.7a Array and Time

    7/30

    Hong Kong Baptist University

    an pu a ng rrays

    Joining two arrays with array_merge()

    Adding multiple variables to an array with array_push() Removing the first element of an array with array_shift()

    Slicing arrays with array_slice()

    Sorting an associative array by value with asort()

    Sorting an associative array by key with ksort()

  • 8/9/2019 Ch.7a Array and Time

    8/30

    Hong Kong Baptist University

    Arrays more.

    rsort() opposite of sort()

    krsort() opposite of ksort()

    select the next data in the array and its value

    end() point to the end of the array

    pos(), prev(), shuffle()

  • 8/9/2019 Ch.7a Array and Time

    9/30

    Hong Kong Baptist University

    Forms Revisited

    testform.html

    http://isem03.hkbu.edu.hk/~isemuser20/form2/testform.html

    testform01.html

    ~

  • 8/9/2019 Ch.7a Array and Time

    10/30

    Hong Kong Baptist University

    Form

    orm ac on = a ress

    method=transmission method>t e content o t e orm

  • 8/9/2019 Ch.7a Array and Time

    11/30

    Hong Kong Baptist University

    GLOBALS

    ou can assess a unc ons y us ng e u - n

    associative array call

    .

    p: sem . u.e u. ~ semuser orm g o a .p p

  • 8/9/2019 Ch.7a Array and Time

    12/30

    Hong Kong Baptist University

    Globals.php

    Global demonstration

  • 8/9/2019 Ch.7a Array and Time

    13/30

    Hong Kong Baptist University

    ome re e ne var a es

    $HTTP_USER_AGENT The name and version of the client

    _ e a ress o e c en

    $REQUEST_METHOD Whether the request was GET or POST

    $QUERY_STRING For GET requests, the encoded data senda ended to the URL

    $REQUEST_URI The full address of the request including query

    $HTTP_REFERER The address of the page from which the requestwas made

  • 8/9/2019 Ch.7a Array and Time

    14/30

    Hong Kong Baptist University

    Associative Array s ng or oop to pr nt t e ata out

    The variable is either _ or

    $_POST

  • 8/9/2019 Ch.7a Array and Time

    15/30

    Hong Kong Baptist University

    .

    foreach ($ POST as $ke id => $var)_

    {

    if (gettype($var) == "array")

    print "$keyid ==EQUAL TO==

    \n";

    foreach ($var as $two_dim_value)

    rint ".............. $two dim value
    ";_ _

    }else

    print "$keyid =equal to= $var

    \n";

    }

    }Calling program : formdemo01.html

    http://isem03.hkbu.edu.hk/~isemuser20/form2/formdemo01.html

  • 8/9/2019 Ch.7a Array and Time

    16/30

    Hong Kong Baptist University

    More flexible

    Extracting Parameters from either a GET or POST

    Extract parameter between get and post by using isset

    Syntax

    _

    ?$_POST:$_GET

    Use $methodused in the for loop

  • 8/9/2019 Ch.7a Array and Time

    17/30

    Hong Kong Baptist University

    Combining HTML and PHP

    yn ax

  • 8/9/2019 Ch.7a Array and Time

    18/30

    Hong Kong Baptist University

    An HTML From Calls Itself

    HTML form that calls

  • 8/9/2019 Ch.7a Array and Time

    19/30

    Hong Kong Baptist University

    Example

    ony uess ng s ame orm .p p

    http://isem03.hkbu.edu.hk/~isemuser20/form2/form201.php

    Notes

    How to count the number of trial

    How to generate the number to guess

  • 8/9/2019 Ch.7a Array and Time

    20/30

    Hong Kong Baptist University

    Question

    Table define the format

    Form to collect data

    Php to process the data captured and present the information

    (Need looping skill with array)

    But how to store date, we need a database

    y s a ree a a ase a we can use o s ore e a a

  • 8/9/2019 Ch.7a Array and Time

    21/30

    Hong Kong Baptist University

  • 8/9/2019 Ch.7a Array and Time

    22/30

    Hong Kong Baptist University

    First Program : time01.phphttp://isem03.hkbu.edu.hk/~isemuser20/time/time01.php

  • 8/9/2019 Ch.7a Array and Time

    23/30

    Hong Kong Baptist University

    Meaning of the output time ( )

    e num er o secon s e apse s nce m n g on

    January 1, 1970

    We will use getdate ( ) to convert it to the normal Time

    getdate ( ) optionally accepts a time stamp and returns anassociative array containing the information about the date

  • 8/9/2019 Ch.7a Array and Time

    24/30

    Hong Kong Baptist University

    The associative array returned by getdate ( )

    Key Description Exampleseco n s econ s pas e m nu es -

    minutes Minutes past the hour (0 59) 7

    mday Day of the month (1 31) 20

    m on Month of the year (1 12) 1

    ear Year 4 di its 2000

    yday Day of year (0 365) 19

    weekday Day of the week(name) Thursday

    month Month of the year (name) January0 Timestamp 948370048

  • 8/9/2019 Ch.7a Array and Time

    25/30

    Hong Kong Baptist University

    .

    http://isem03.hkbu.edu.hk/~isemuser20/time/time02.php

    Part of the program

    date_default_timezone_set('Asia/Hong_Kong');

    emo_ a e_array = ge a e ;

    //no argument passed so today's date will be used

    =_ _

    { print "$key = $val
    "; }

    print "Today's date :$demo_date_array[mday]/$demo_date_array[mon]/$d

    < >"_ _

  • 8/9/2019 Ch.7a Array and Time

    26/30

    Hong Kong Baptist University

    Converting a Time Stamp with date ( )

    e a e unc on re urns a orma e s r ng

    representing a date

    Formatting

    . . . .

  • 8/9/2019 Ch.7a Array and Time

    27/30

    Hong Kong Baptist University

    .

    http://isem03.hkbu.edu.hk/~isemuser20/time/time03.php

    Portion of the code

    print date("m/d/y G.i:s
    ", time());

    Hour.minute:second

    print "Today is ";

    " ", . ,// Today is Day ofMonth Year, at hour.minutes am/pm

    lowercase

    H K B i U i i

  • 8/9/2019 Ch.7a Array and Time

    28/30

    Hong Kong Baptist University

    Creating Time Stamps with mktime ( )

    ,

    yeat work with arbitrary dates. mktime ( ) returns a time stamp that

    you can then use with date ( ) or getdate ( ).

    mktime ( ) accepts up to six integer arguments in the following order :

    minute second

    month

    day of month

    ear

    H K B ti t U i it

  • 8/9/2019 Ch.7a Array and Time

    29/30

    Hong Kong Baptist University

    http://isem03.hkbu.edu.hk/~isemuser20/time/time04.php

    Portion of the code

    // make a timestamp for 4/18/98 at 8.00 am

    $ts = mktime(8, 0, 0, 4, 18, 1998);

    print date("m/d/y G.i:s
    ", $ts);

    print "The date is ";

    pr nt ate o , a t g. a , ts ;

    H K B ti t U i it

  • 8/9/2019 Ch.7a Array and Time

    30/30

    Hong Kong Baptist University

    Checking for a date with checkdate ( )

    input before you work with this date, or store it in a

    database. checkdate ( ) accepts three integers : month, day, and

    year. checkdate ( ) return true if

    Month is between 1 and 12, The day is acceptable for the given month and year (accounting

    ,

    .http://isem03.hkbu.edu.hk/~isemuser20/time/time05.php