From: Joe Z. <jz...@us...> - 2003-07-01 05:01:47
|
Update of /cvsroot/bobs/bobs/doc In directory sc8-pr-cvs1:/tmp/cvs-serv11259/doc Modified Files: list.php Log Message: Fix list class documentation and example to work with register_globals = on, the default since php 4.2.0. Index: list.php =================================================================== RCS file: /cvsroot/bobs/bobs/doc/list.php,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- list.php 1 Sep 2002 02:23:38 -0000 1.2 +++ list.php 1 Jul 2003 05:01:43 -0000 1.3 @@ -7,20 +7,16 @@ // the path to class_list.php. // ======================================================================= -if (!include("class_list.php")){ // the list class definition - $thisfile = getenv("DOCUMENT_ROOT") . "$PHP_SELF"; +$PHPSELF = $_SERVER['PHP_SELF']; + +if (!include("inc/class_list.php")){ // the list class definition exit("<hr>The file <b>class_list.php</b> was not found.<br> It is needed for this document to work properly.<br> - It should be in the same directory as this file: $thisfile<br>"); + It should be in the same directory as this file: $PHPSELF<br>"); } -// Uncomment the following line to turn on debug. -$debug = TRUE; - // Start the session -if(!session_is_registered("mylist")) { - session_register("mylist", "mylist2", "mylist3", "mylist4"); // Store $mylist variable in session -} +session_start(); ?> <html> @@ -70,42 +66,41 @@ </pre> <p> In order to reference the <em>selectlist</em> object between web pages, you must create a session. - The variables that will be stored in the session are parameters to the - <em>session_register()</em> function. Note: By default, php stores session variables in - files in the <em>/tmp</em> directory. + The session variables will be stored in a global <em>$_SESSION</em> array. + Note: By default, php stores session variables in files in the <em>/tmp</em> directory. </p> <pre> // Start the session - if(!session_is_registered("mylist")) { - session_register("mylist"); // Store $mylist variable in session - } + session_start(); </pre> <p> You can also end the session. </p> <pre> - session_unregister("mylist"); session_destroy(); </pre> <p> - Create the <em>selectlist</em> object and store it in a variable (<em>$mylist</em> in this case). - Note that the variable must be also specified in the <em>session_register()</em> function. + Create the <em>selectlist</em> object and store it in a session + variable (<em>mylist</em> in this case). </p> <pre> - $mylist = new selectlist("list1"); + $_SESSION['mylist'] = new selectlist("list1"); </pre> <p> - You can also delete the <em>selectlist</em> object by setting the object variable - to another value. + You can also delete the <em>selectlist</em> object by setting the variable + to another value or unsetting it. </p> <pre> - $mylist = ""; + $_SESSION['mylist'] = ""; + or + unset($_SESSION['mylist']); + </pre> -<form action="<?$PHP_SELF?>#section1" method="post"> +<form action="<?PHP $PHPSELF?>#section1" method="post"> <input type="submit" name="button" value="Create selectlist"> <input type="submit" name="button" value="Refresh"> <input type="submit" name="button" value="Delete selectlist"> @@ -113,31 +108,23 @@ <input type="hidden" name="section" value="1"> </form> -<? -if ($section == "1"){ - switch ($button){ +<?PHP +if ($_POST['section'] == "1"){ + switch ($_POST['button']){ case ("Create selectlist"): - $mylist = new selectlist("list1"); + $_SESSION['mylist'] = new selectlist("list1"); break; case ("Delete selectlist"): - $mylist = ""; + unset($_SESSION['mylist']); break; case ("Refresh"): break; case ("End session"): - session_unregister("mylist"); - session_unregister("mylist2"); - session_unregister("mylist3"); - session_unregister("mylist4"); - $mylist = ""; - $mylist2 = ""; - $mylist3 = ""; - unset($mylist4); session_destroy(); break; } } - +$mylist = $_SESSION['mylist']; if (get_class($mylist)){ echo "<pre>\n"; print_r($mylist); echo "</pre>\n"; } else { @@ -158,15 +145,15 @@ function. </p> <pre> - session_register("mylist"); // store list object in session - $mylist = new selectlist("list2"); // create the list object - $mylist->additem("one item"); // add 1 item + $_SESSION['mylist'] = new selectlist("list2"); // store list object in session + $mylist =& $_SESSION['mylist']; // create a convenient variable reference to the session variable + $mylist->additem("one item"); // add 1 item $items = array("banana", "apple", "peach"); - $mylist->additem($items); // add 3 items + $mylist->additem($items); // add 3 items </pre> -<form action="<?$PHP_SELF?>#section2" method="post"> +<form action="<?PHP $PHPSELF?>#section2" method="post"> <input type="submit" name="button" value="Add 1 item"> <input type="submit" name="button" value="Add 3 items"> <input type="submit" name="button" value="Add 5 items"> @@ -174,12 +161,13 @@ <input type="hidden" name="section" value="2"> </form> -<? -if ($section == "2"){ - if (!get_class($mylist2)) { // create list if not exist - $mylist2 = new selectlist("list2"); +<?PHP +if ($_POST['section'] == "2"){ + if (!get_class($_SESSION['mylist2'])) { // create list if not exist + $_SESSION['mylist2'] = new selectlist("list2"); } - switch ($button){ // Add items to the list + $mylist2 =& $_SESSION['mylist2']; // create convenient reference + switch ($_POST['button']){ // Add items to the list case ("Add 1 item"): $mylist2->additem("one item"); break; @@ -217,8 +205,9 @@ table within a form. Just echo the output of <em>gethtml()</em>. </p> <pre> - session_register("mylist"); // store list object in session - $mylist = new selectlist("list3"); // Create the selectlist object + $_SESSION['mylist'] = new selectlist("list"); + // Create and store list object in session + $mylist =& $_SESSION['mylist']; // create convenient reference $mylist->setadd(); // Show the "Add" button $mylist->setchange(); // Show the "Change" button $mylist->setdelete(); // Show the "Delete" button @@ -240,18 +229,19 @@ // 1 array of values $mylist->additem("Santana"); // A single value - $html = $mylist->gethtml($PHP_SELF); // Generate the html + $html = $mylist->gethtml($_SERVER['PHPSELF']); // Generate the html echo "$html"; // Output the html </pre> -<form action="<?$PHP_SELF?>#section3" method="post"> +<form action="<?PHP $PHPSELF?>#section3" method="post"> <input type="submit" name="button" value="Show html code"> <input type="hidden" name="section" value="3"> </form> -<? -if (!get_class($mylist3)) { // Create list if not exist - $mylist3 = new selectlist("list3"); +<?PHP +if (!get_class($_SESSION['mylist3'])) { // Create list if not exist + $_SESSION['mylist3'] = new selectlist("list3"); + $mylist3 =& $_SESSION['mylist3']; $mylist3->setadd(); // Show the "Add" button $mylist3->setchange(); // Show the "Change" button $mylist3->setdelete(); // Show the "Delete" button @@ -271,11 +261,12 @@ $mylist3->additem(array("Fontana", "nirvana")); $mylist3->additem("Santana"); } -$url = "$PHP_SELF" . "#section3"; +$url = "$PHPSELF" . "#section3"; +$mylist3 =& $_SESSION['mylist3']; $html = $mylist3->gethtml($url); // Generate the html -if ($section == "3"){ - switch ($button){ // "Show html code" button pressed +if ($_POST['section'] == "3"){ + switch ($_POST['button']){ // "Show html code" button pressed case ("Show html code"): $htmlcode = htmlspecialchars($html); echo "\n<pre>\n"; @@ -302,10 +293,10 @@ Functions are provided to process the information from the list form. Briefly, they are: <pre> - $selectlist['name'] Hidden form field containg the list name. - getname Get the name of the list. - getaction Get the value of the button pressed. - getvalue Get the values of the selected list items. + $_POST['selectlist']['name'] Hidden form field containg the list name. + getname Get the name of the list. + getaction Get the value of the button pressed. + getvalue Get the values of the selected list items. </pre> All the list information is contained in an array named <em>selectlist[].</em> by passing the <em>selectlist[]</em> array @@ -326,6 +317,8 @@ <pre> This is the code this example uses to add the items to the list when you click "Add". + $selectlist =& $_POST['selectlist']; // Create local references for convenience + $mylist =& $_SESSION['mylist']; if ($selectlist['name'] == 'list4'){ // if mylist4 object exists switch ($mylist->getaction($selectlist)){ // switch is "action" button case ("Add"): // If "Add" button pressed @@ -339,18 +332,20 @@ } </pre> -<? -if ($selectlist['name'] == 'list4'){ // if mylist4 is the active list - switch ($mylist4->getaction($selectlist)){ +<?PHP +if ($_POST['selectlist']['name'] == 'list4'){ // if mylist4 is the active list + $mylist4 =& $_SESSION['mylist4']; // Reference with local variable + switch ($mylist4->getaction($_POST['selectlist'])){ case ("Add"): - $value = $mylist4->getvalue($selectlist); + $value = $mylist4->getvalue($_POST['selectlist']); $mylist4->additem($value[0]); break; } } -if (!get_class($mylist4)) { - $mylist4 = new selectlist("list4"); // Create list if not exist +if (!get_class($_SESSION['mylist4'])) { + $_SESSION['mylist4'] = new selectlist("list4"); // Create list if not exist + $mylist4 =& $_SESSION['mylist4']; // Reference with local variable $mylist4->setadd(); // Show the "Add" button $mylist4->setchange(); // Show the "Change" button $mylist4->setdelete(); // Show the "Delete" button @@ -366,7 +361,9 @@ $mylist4->additem("Montana", "The state of Montana"); $mylist4->additem(array("red planet", "green martians", "blue sea", "outer space")); } -$url = "$PHP_SELF" . "#section4"; +$mylist4 =& $_SESSION['mylist4']; // Reference with local variable +$selectlist = $_POST['selectlist']; // Reference with local variable +$url = "$PHPSELF" . "#section4"; $html = $mylist4->gethtml($url); // Generate the html // table will contain list form and output of functions @@ -619,7 +616,7 @@ <hr> <pre> -string <b>gethtml</b> ( string $PHP_SELF ) +string <b>gethtml</b> ( string $_SERVER['PHPSELF'] ) <b>Description</b> @@ -627,9 +624,10 @@ <b>Parameters</b> - $PHP_SELF Required for the form "action". + $_SERVER['PHPSELF'] + Required for the form "action". You can substitute the web page url another way, - but using $PHP_SELF is easy. + but using $_SERVER['PHPSELF'] is easy. <b>Returns</b> @@ -697,4 +695,19 @@ </body> </html> + +<?php +//--------------------------------------------------------------------------- +// Functions +//--------------------------------------------------------------------------- + +//--------------------------------------------------------------------------- +// Print a variable for debug +function dump($var, $text = ""){ + echo "\n<pre>*********************************************\n"; + echo "$text \n"; + var_dump($var); + echo "\n</pre>*********************************************\n"; +} +?> |