Total Pageviews

Wednesday, April 20, 2011

ITC 280 - Forms Get, Post / Arrays & Superglobals


ITC 280 – Class notes 4-20-11

Programs:

·        EditPad Pro (www.jgsoft.com)
o   AceText (holds the stuff from your clipboard, in sync with EditPad Pro)
·         www.portableapps.com  (portable version of firefox & all the open office suite & a pdf viewer)

Web Applications review:

Postback string does NOT show up on the page
Web applications can transmit two ways:
Get:       default method, not as secure
                good for a loaded query page
                data being sent is visible
                Will go back to default if developer accidentally
 
Post:      sends data in a non-visible way
                better at forms

Forms: (floating in memory)

“name” attribute is how to find/get access to the form
“postback” goes back to the same page
To send data back to the machine back to server
Form elements: see http://saranewman.com/web110/web110form-ppt/web110form-ppt.html
see also:
    Constants: accessible everywhere (global)        
Superglobal:    all piece of the data that was sent from the user is now called a “superglobal”
         “$_...” – indicator of a superglobal

          Definition: a variable that is available to you that is a result of a “post”
                      
superglobals are arrays!
·         $GLOBALS
·         $_SERVER
·         $_GET
·         $_POST
·         $_FILES
·         $_COOKIE
·         $_SESSION
·         $_REQUEST
·         $_ENV
$_POST['varName']      //name attribute MUST match

NOTE: $_GET and $_POST are NOT the same
            web server – controls the address …
if(isset($_POST['FirstName'])  // to see if there is data
{
     $myVar = $_POST['FirstName'];
}
action:”blah.php”
Assignment 4: contact form with Recaptcha (ferrets out web robots)
NOTES:
web – by default is a stateless environment
Cookies: string of data that uniquely identifies the person (part of http & html)
            they are for your security
            also are known as superglobals
            Session” – piece on the server that matches up to the person globally
Server Log: tracks the number of users, number of unique visitors, browser that is used, search engine, search term, website that they used to get to your site
   Hosting company: Urchin (owned by google, that is on the server)
       > Usually only available are for a week (so they get deleted after)
             > Find out how much traffic they have, take a snapshot of the data before it disappears

Arrays:

·        All important data comes from this format, an alternative way to store a set of variables
   constants – can only store one piece of data at a time
·        Like a file folder (groups the data) and can grow to virtually any size!
Type of data storage: flexible, allows the following to be stored
            strings
            numbers
            Boolean
            arrays             (within another set)
              > array (within another array)

Practice:

array_practice1.php // database stores & gives us the array
indicator for an array (suggested by Bill)
                “$a…” = array( );
ex:
<?php
//array_practice1.php
$aFruit = array("bananas",56,"oranges");
print_r($aFruit); // print_r is the command to print the array
?>
Displays:
          Array ( [0] => banana [1] => 56 [2] => oranges )
               // c style code and starts in 0, not at 1
                (built by AT&T, language C)
Offset: how far off it is from the beginning (zero is the start)
index: to what it means to humans  (what position it is actually in, “0” means 1st element in array)
key: unique identify      data/value: on the right side of the equal sign
<?php
//array_practice1.php
$aFruit = array("bananas",56,"oranges");  
echo '<pre>';       
//formats the array like how it should look, open <pre> tag
print_r($aFruit);    // print_r is the command to print the array
echo '</pre>';            // must end pre tag,
php is ignorant of <html> but can use it
?>
Line break:              <br /> for <html>
carriage returns:  don’t exist in <html>

Ways to print an array:

print_r: way to look at an array
var_dump: look at anything
Backtrace: like a var_dump for the entire data, dump of the entire server at that moment

var_dump result: 

array(3) 

{
  [0]=>
  string(7) "bananas"
  [1]=>
  int(56)
  [2]=>
  string(7) "oranges"
}
Cheese array exercise:
$aCheese = array();
$aCheese[0] = "cheddar";
$aCheese[1] = "brie";
$aCheese[2] = "brie";
$aCheese[] = "mozerella";
$aCheese[] = "brie"; //fastest way to add a new variable to an array
$brieCounter = 0; // neutral value, empty number for an int
foreach($aCheese as $slice)
{
     echo $slice . "<br />";   
     // how many times does 'brie' string shows up in the array
     // if it does equal, increment the brie counter
     if($slice == "brie")
     {   
// if the current array value = 'brie', add 1 to the counter
        $brieCounter = $brieCounter + 1; // increments the number of bries
        $brieCounter += 1;     
// another way of writing the incrementation     
   $brieCounter++;     // shortest version if you are only adding by 1
}
}
Most efficient way to access an array is with a “for” loop:
for($x=0;$x<count($aFruit);$x++)
 {  
     echo $aFruit[$x];   
 }
1.      Start at the beginning
2.      Until you get to the end, continue until you get to the end
a.      “count” is a function (like getlength)
b.      Loops through the array called “aFruit”, $ is the indicator of the array variable
3.      Increment by 1, go one at a time
Another way:
$aCheese = array();
$aCheese[0] = "cheddar";
$aCheese[1] = "brie";
$aCheese[2] = "swiss";

 for($x=0;$x<count($aCheese);$x++)
{
   echo $aCheese[$x] . '<br />';   
}

Alternative way to have a for loop:
foreach($aCheese as $slice)
{
    echo $slice . 'br />';  
}

No comments:

Post a Comment