Monday, April 19, 2010

Workshop 3 : STRINGS, DATES, FORMS and SESSIONS

Working with Strings




1. Up to now, we've only been working on the echo statement. The printf() statement does the same thing except it can accept arguments passed onto the function.



<?php



// This outputs the integer as a decimal number instead of part of a string

printf("This is my number : "%d", 60) ;



?>





2. You can also specify the start position of your string displayed



<?php



echo "<pre">;

// This will leave 15 spaces before printing

printf ("%15s\n", " Name");

printf ("%15s\n", " Address");

printf ("%15s\n", " Mobile Number");

printf ("%15s\n", " Email");

echo "<pre">;

?>



3. You can look up more string functions on www.php.net

• strlen() , to determine the length of a string

• strstr (), to determine the existence of a string within a string

• strpos(), to find the position of a substring





Working with Time and Date



1. PHP stores time as number of seconds that have elapsed since GMT midnight 1 January 1970. This is the time stamp



<?php



// This is the timestamp, It is the number of seconds since Unix Epoch (Midnight GMT 1 Jan 1970)



echo "The number of seconds that have passed since Midninght GMT 1 January 1970 is ";

echo time();

echo "<br>";



?>



2. With the timestamp you can convert it to date using getdate(). This is a an array that stores the vales of date.



<?php

$date_array = getdate(); // no argument passed so today's date will be used

foreach ($date_array as $key => $val) {

echo "$key = $val<br>";

}

?>

<hr>

<?

echo "Today's date: ".$date_array['mday']."/".$date_array['mon']."/".

$date_array['year']."<p>";

?>



Creating Forms



1. Use the following files for testing.

1.1 Listing 9.1.html

<html>

<head>

<title>Listing 9.1 A simple HTML form</title>

</head>

<body>

<form action="listing9.2.php" method="POST">

<p><strong>Name:</strong><br>

<input type="text" name="user">

<p><strong>Address:</strong><br>

<textarea name="address" rows="5" cols="40"></textarea>

<P><input type="submit" value="send"></p>

</form>

</body>

</html>



1.2 Listing 9.2.php

<html>

<head>

<title>Listing 9.2 Reading input from a form </title>

</head>

<body>

<?php

echo "<p>Welcome <b>$_POST[user]</b></p>";

echo "<p>Your address is:<br><b>$_POST[address]</b></p>";

?>

</body>

</html>



There are two input boxes available from listing9.2.html called text and address. When the user submits the form it will invoke the script called listing9.2.php. The $_POST command will grab the value values corresponding to the text boxes value in listing9.1.html.





2. Getting data from a multi valued text box

2.1 Listing9.3.html

<html>

<head>

<title>Listing 9.3 An HTML form including a SELECT element</title>

</head>

<body>

<form action="listing9.4.php" method="POST">

<p><strong>Name:</strong><br>

<input type="text" name="user">



<p><strong>Address:</strong><br>

<textarea name="address" rows="5" cols="40"></textarea>



<p><strong>Select Some Products:</strong> <br>

<select name="products[]" multiple>

<option value="Sonic Screwdriver">Sonic Screwdriver</option>

<option value="Tricoder">Tricorder</option>

<option value="ORAC AI">ORAC AI</option>

<option value="HAL 2000">HAL 2000</option>

</select>



<p><input type="submit" value="send"></p>

</form>

</body>

</html>



2.2 Listing9.4.php

<html>

<head>

<title>Listing 9.4 Reading input from the form in Listing 9.3</title>

</head>

<body>

<?php

echo "<p>Welcome <b>$_POST[user]</b></p>";

echo "<p>Your address is:<br><b>$_POST[address]</b></p>";

echo "<p>Your product choices are:<br>";

if (!empty($_POST[products])) {

echo "<ul>";

foreach ($_POST[products] as $value) {

echo "<li>$value";

}

echo "</ul>";

}

?>

</body>

</html>



3. Sending out emails



3.1 Listing9.10.html

3.2 Listing.9.11.php

Sessions



Sessions are unique identifier of a user, which can be used to store and retrieve information attached to the user. Therefore enabling the website to know what the user have seen, browse through etc. The user would need to have cookies enabled for session to work.



1. Initializing a session



<?php

session_start();



echo "<p>Your session ID is ".session_id()."</p>";

?>



Your session id stays the same all the time.



2. Storing and accessing session variables

We'll store the variables first in a separate file



<?php

session_start();



$_SESSION[product1] = "Sonic Screwdriver";

$_SESSION[product2] = "HAL 2000";

echo "The products have been registered.";



?>





Once you have executed this script, run the next script. The above script will attached the data onto session variables.



<?php

session_start();



echo "Your chosen products are:";

echo "<ul><li>$_SESSION[product1] <li>$_SESSION[product2]\n</ul>\n";

?>





3. Destroying sessions and variables. Once executed it would remove the session and the corresponding variables



<?php

session_start();

session_destroy();

unset($session[product1]); // Remove product 1

unset($session[product2]); // Remove product 2



// This should print nothing

echo "Your chosen products are:";

echo "<ul><li>$_SESSION[product1] <li>$_SESSION[product2]\n</ul>\n";

?>

No comments:

Post a Comment