⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 documentation-4.html

📁 PHPLOB注释详细版 使用模板技术的好帮手 PHP最有用的东东了
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>PHP Base Library Documentation, Release phplib_7_2: Extended functionality</TITLE> <LINK HREF="documentation-5.html" REL=next> <LINK HREF="documentation-3.html" REL=previous> <LINK HREF="documentation.html#toc4" REL=contents></HEAD><BODY><A HREF="documentation-5.html">Next</A><A HREF="documentation-3.html">Previous</A><A HREF="documentation.html#toc4">Contents</A><HR><H2><A NAME="s4">4. Extended functionality</A></H2><P><P>The section on extended functionality covers non-GUI classesthat provide often needed application functions without a userinterface. Some extended classes depend on core functionality,some contain independent classes.<P>Extended classes are treated differently from core classes inthat their code is not automatically included by<CODE>prepend.php3</CODE>. You have to include the class definitionmanually where needed or you modify <CODE>prepend.php3</CODE>.<P><H2><A NAME="ss4.1">4.1 Cart</A></H2><P><P>The Cart class is programmatically independent, but makes senseonly if its instances are made persistent in some way. The Cartclass automatically registers itself as a session variable inits <CODE>start()</CODE> function.<P>Cart implements a shopping cart. At the moment, items within theshopping cart are independent of each other; the cart can onlyhold simple things. Support for compound articles that requireother articles to function and provide a base for dependentarticles is to be added at a future time.<P>An example of a simple article is any article with no options,for example an apple or a book. Common examples for compoundarticles are a pizza (which requires a foundation in eitherAmerican or Italian style, a selection of toppings, and cheese,to function correctly) and a computer system (which requires ahousing, a motherboard, RAM, a video card, etc to functioncorrectly).<P><EM>Note:</EM> <CODE>Cart</CODE> was a core class up to <EM>release-5</EM>. Ifyour applications uses the <CODE>Cart</CODE> class, you <EM>must</EM>manually add the statement <CODE>include("cart.inc")</CODE> to your<CODE>prepend.php3</CODE> file where indicated in that file.<P><EM>Note:</EM> The page management functions do no longer supportthe feature <CODE>cart</CODE> to set up and start the cart class. It isrecommended that you use <CODE>Session</CODE>'s <CODE>auto_init</CODE> featureinstead to start your cart automatically or that you manuallyset up your cart.<P><H3>Instance variables</H3><P><P><CENTER><TABLE BORDER><TR><TD><BR>classname</TD><TD>Serialization helper: The name of this class.</TD></TR><TR><TD>persistent_slots</TD><TD>Serialization helper: The names of all persistent slots.</TD></TR><TR><TD>item</TD><TD>Multidimensional array of items in the cart.</TD></TR><TR><TD>currentItem</TD><TD>A counter for item positions.</TD></TR><TR><TD><CAPTION>Accessible instance variables.</CAPTION></TD></TR></TABLE></CENTER><P><H3>Instance methods</H3><P><P><H3>Accessible instance methods</H3><P><P><DL><P><DT><B>check($art)</B><DD><P> Checks that an item with the given article number <CODE>$art</CODE> isin the cart. Returns an array of a boolean value and an integernumber. If the boolean is true, there are number many articlesof that article number in the cart.<P><DT><B>reset()</B><DD><P> Deletes all items in current cart, resetting $this->currentItem to1. Always returns true.<P><DT><B>num_items()</B><DD><P>Returns the number of articles in the current shopping cart, orfalse if cart is empty. For compatibility reasons, this functionis available as <CODE>tot_arts</CODE> as well (but will printa warning if used by this name).<P><DT><B>add_item($art, $num)</B><DD><P>Add <CODE>$num</CODE> many articles of article number <CODE>$art</CODE> to the currentshopping cart. Returns the position number of <CODE>$art</CODE> in theshopping cart.<P><DT><B>remove_item</B><DD><P>Remove <CODE>$num</CODE> many articles of article number <CODE>$art</CODE> from theshopping cart, if there are at least that many articles in thecart. Returns the position number of <CODE>$art</CODE> in the shopping cartor false, if there weren't enough <CODE>$art</CODE> to remove them from thecart. If the function does return false, the cart has not beenmodified.<P><DT><B>set_item</B><DD><P>Set the quantity of article number <CODE>$art</CODE> in the shopping cart to exactly <CODE>$num</CODE>. If <CODE>$num</CODE> is set to zero, article is removed fromcart. Returns the position number of <CODE>$art</CODE> in the shopping cart.<P><DT><B>show_all()</B><DD><P>If the shopping cart is empty, it will call<CODE>show_empty_cart()</CODE> once and then return.<P>Calls <CODE>show_item_open()</CODE> once at the beginning of a shoppingcart listing. Then calls <CODE>show_item()</CODE> once for each item inthe shopping cart. Calls <CODE>show_item_close()</CODE> once at the endof a shopping cart listing.<P><DT><B>show_item($art, $num)</B><DD><P>This function should be provided by the user. It renders theHTML to display a single item from the cart. <CODE>$art</CODE> is thearticle number of the item and there are <CODE>$num</CODE> of these inthe cart.<P><DT><B>show_cart_open()</B><DD><P>This function should be provided by the user. It renders theprologue HTML to display a shopping cart listing.<P><DT><B>show_cart_close()</B><DD><P>This function should be provided by the user. It renders theepilogue HTML to display a shopping cart listing.<P><DT><B>show_empty_cart</B><DD><P>This function should be provided by the user. It should renderan appropriate message to symolize an empty cart.</DL><P><H3>Example</H3><P><P>Use a subclass of <CODE>Cart</CODE> to provide an implementation of<CODE>show_item()</CODE>.<P><BLOCKQUOTE><CODE><HR><PRE>class My_Cart extends Cart {  var $classname = "My_Cart";  // Look up article numbers...  var $database_class = "DB_Article";  var $database_table = "articles";  var $db;    var $sum = 0;  function show_cart_open() {    printf("&lt;table class=cart_table&gt;\n");    $this-&gt;sum = 0;  }    function show_cart_close() {    printf("&lt;/table&gt;\n");    printf("That's a total of %s.\n", $this-&gt;sum);  }  function show_item($art, $num) {    if (!is_object($this-&gt;db)) {      $class    = $this-&gt;database_class;      $this-&gt;db = new $class;    }        $query = sprintf("select * from %s where artid = '%s'",      $this-&gt;database_table,      $art);    $this-&gt;db-&gt;query($query);    while($this-&gt;db-&gt;next_record()) {      printf(" &lt;tr class=cart_row&gt;\n  &lt;td class=cart_cell&gt;%s&lt;/td&gt;\n",        $this-&gt;db-&gt;Record["name"]);      printf("  &lt;td class=cart_cell&gt;%s&lt;/td&gt;\n",         $this-&gt;db-&gt;Record["price"]);      printf("  &lt;td class=cart_cell&gt;%s&lt;/td&gt;\n",        $num);      $rowsum = $num * $this-&gt;db-&gt;Record["price"];      $this-&gt;sum += $rowsum;      printf("  &lt;td class=cart_cell&gt;%s&lt;/td&gt;\n",        $rowsum);      printf(" &lt;/tr&gt;\n");    }  }}</PRE><HR></CODE></BLOCKQUOTE><P>To use a cart, create an instance of your <CODE>Cart</CODE> subclass andcall <CODE>start()</CODE>. This will automatically register <CODE>cart</CODE>.<P>It is recommended that you set in your <CODE>Session</CODE> subclassin <CODE>local.inc</CODE> the slot <CODE>$auto_init</CODE> to the value<CODE>setup.inc</CODE> and create an include file of that name whichcontains the following code:<P><BLOCKQUOTE><CODE><HR><PRE>  global $cart;               ## $cart is a global variable.  $cart = new My_Cart; ## Make a My_Cart instance named $cart  $cart-&gt;start();          ## and have it register itself.</PRE><HR></CODE></BLOCKQUOTE><P>Use <CODE>add_item()</CODE> and <CODE>remove_item</CODE> to work with your Cart:<P><BLOCKQUOTE><CODE><HR><PRE>  $cart-&gt;add_item("101", 2);    ## Add two pieces of "101"  $cart-&gt;remove_item("101", 1); ## Drop one piece of "101"</PRE><HR></CODE></BLOCKQUOTE><P>Use <CODE>show_all()</CODE> to display the contents of your cart.<P><BLOCKQUOTE><CODE><HR><PRE>  $cart-&gt;show_all();    ## What's in a cart, anyway?</PRE><HR></CODE></BLOCKQUOTE><P><H3>On using Cart</H3><P>To make use of the Cart class, you need to define a new tablein your database that lists all articles you shop should sell.With PHPLIB and MySQL we recommend that you create a newinstance of PHPLIB for each virtual web server and a new databasefor each customer. This database should hold theactive_sessions and auth_user tables as well as allapplication specific tables like for example the articlelist. In other words, with MySQL we strongly discouragethat you use PHPLIB and the MySQL directive <CODE>use</CODE><EM>database_name</EM> together. There is no supportif you do (there is no support if you do as we say, too,because PHPLIB is an open source product you are using on yourown risk, but ...).<P>So let us assume you define a very simple new table <CODE>articles</CODE>with a structure like this:<P><BLOCKQUOTE><CODE><HR><PRE>## Table structure for table 'articles'#CREATE TABLE articles (  name text,  price float(8,2),  artid int(11) DEFAULT '0' NOT NULL auto_increment,  PRIMARY KEY (artid));</PRE><HR></CODE></BLOCKQUOTE><P>This table has an article number called <CODE>artid</CODE>, and for each<CODE>artid</CODE>  there is an article description <CODE>name</CODE> and a <CODE>price</CODE>. Youmay extend this minimal definition for your purposes by addingarticle groups, BLOBs with article images and more, but this willsuffice for our example purposes.<P>Populate this table with some products that suit your taste.<P>The next step is to teach PHPLIB about the cart class. Threesteps are necessary to do so:<P><UL><LI>the <CODE>Cart</CODE> class has to be included on every page. Even onthat pages that do not make use of the <CODE>Cart</CODE> class.On that pages that use <CODE>Cart</CODE>, a cart subclass is instantiated andsaved. On all subsequent pages, that <CODE>Cart</CODE> object is recreated andto be able to recreate the <CODE>Cart</CODE> object, PHP must know what a <CODE>Cart</CODE>object is. Since you cannot know which pages a user loads afterhe has put the first item into the <CODE>Cart</CODE>, we need to have adefinition for <CODE>Cart</CODE> on <EM>all</EM> pages.The proper place to include the <CODE>Cart</CODE> definition from <CODE>cart.inc</CODE> isconsequently <CODE>prepend.php3</CODE>. Edit <CODE>prepend.php3</CODE> and<CODE>require("cart.inc")</CODE> as indicated by the comments in that file.</LI><LI>a subclass of <CODE>Cart</CODE> has to be created to suit your tastes.Your subclass of <CODE>Cart</CODE> will be called <CODE>Example_Cart</CODE> in this example.You may actually name it as you like, but you have to beconsistent.The definition of <CODE>Example_Cart</CODE> goes into <CODE>local.inc</CODE> anywhere belowyour definition for <CODE>Example_Session</CODE>. It looks like this<BLOCKQUOTE><CODE><HR><PRE>class Example_Cart extends Cart {  var $classname = "Example_Cart";}</PRE><HR></CODE></BLOCKQUOTE>and we will add additional code later in this example. Thatadditional code will teach your shopping cart about the databasetable that holds your articles and so on.</LI><LI>finally, you need to create an instance of your shopping cartclass so that you have an object that actually holds the articlesselected by the user.We will use a very nifty feature of PHPLIB to create that objectinstance: If you set up PHPLIB properly, it is able to load andexecute an include file every time a session is being created. Wecall this feature <CODE>auto_init</CODE>, after the instance variable ofSession that controls it.Go into <CODE>local.inc</CODE> and edit your subclass of <CODE>Session</CODE>. You willhave some code like<BLOCKQUOTE><CODE><HR><PRE>class Example_Session extends Session {  var $classname = "Example_Session";...}</PRE><HR></CODE></BLOCKQUOTE>in your <CODE>local.inc</CODE>. Adda line like<BLOCKQUOTE><CODE><HR><PRE>  var $auto_init = "setup.inc",</PRE><HR></CODE></BLOCKQUOTE>to your definition of <CODE>Example_Session</CODE> and create a file<CODE>setup.inc</CODE> in the same directory that holds your local.inc.Whatever code is in this file will be executed every time wecreate a new session. The code is being executed after your<CODE>$sess</CODE>, <CODE>$auth</CODE> and <CODE>$perm</CODE> objects are loaded andinitialized, but does run from within a function context. Youhave to <CODE>global</CODE> everything you define to export it from thatfunction context.In <CODE>setup.inc</CODE>, create a global instance of<CODE>Example_Cart</CODE> named <CODE>$cart</CODE> and register that variablewith PHPLIB:<BLOCKQUOTE><CODE><HR><PRE>&lt;?php  global $cart;  $cart = new Example_Cart;  // $sess is already global  $sess->register("cart"); ?&gt;</PRE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -