<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title> Practical PostgreSQL</title> <meta name="keywords" content="postgresql, support, recovery, design, software, hosting, expterise, programming, postgres, pgsql, replication, mysql, firebird, database, sql, free, open source, software" /> <meta name="description" content="Command Prompt, Inc. is the largest and oldest US based commercial PostgreSQL support provider. We provide the only commercially viable integrated PostgreSQL replication solution, but also custom programming, and support. We authored the book Practical PostgreSQL, the procedural language plPHP, and adding trigger capability to plPerl. " /> <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" /> <link rel="stylesheet" type="text/css" href="../css/layout.css" /> <link rel="stylesheet" type="text/css" href="../css/cmd4.css" /> <script language="javascript" type="text/javascript"><!-- greymenuhomewideLit = new Image(); greymenuhomewideLit.src = "/images/greymenu_homewide_lit.gif"; greymenuaboutLit = new Image(); greymenuaboutLit.src = "/images/greymenu_about_lit.gif"; greymenuproductsLit = new Image(); greymenuproductsLit.src = "/images/greymenu_products_lit.gif"; greymenuservicesLit = new Image(); greymenuservicesLit.src = "/images/greymenu_services_lit.gif"; greymenusupportLit = new Image(); greymenusupportLit.src = "/images/greymenu_support_lit.gif"; greymenucommunityLit = new Image(); greymenucommunityLit.src = "/images/greymenu_community_lit.gif"; //--></script> </head> <body> <div align="center"> <div id="widecontainer"> <div id="widetop"> <div class="throwleft" style="width: 300px; text-align: left;"><a href="http://www.commandprompt.com/"><img src="../images/cmd4logo_wide.gif" border="0" alt="Command Prompt, Inc." /></a></div> <div class="throwright" style="width: 400px; text-align: right;"> <div id="topright"> <form action="index.html" name="cmdform" method="post"> <ul> <li><input type="text" name="search" value="" /></li> <li><input type="submit" name="action" value="Search Book" /></li> </ul> </form> </div> </div> </div> <ul id="greymenu_wide"> <li><a href="http://www.commandprompt.com/home/"><img onmouseover="this.src = greymenuhomewideLit.src;" onmouseout="this.src = '/images/greymenu_homewide.gif';" src="../images/greymenu_homewide.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/about/"><img onmouseover="this.src = greymenuaboutLit.src;" onmouseout="this.src = '/images/greymenu_about.gif';" src="../images/greymenu_about.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/products/"><img onmouseover="this.src = greymenuproductsLit.src;" onmouseout="this.src = '/images/greymenu_products.gif';" src="../images/greymenu_products.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/services/"><img onmouseover="this.src = greymenuservicesLit.src;" onmouseout="this.src = '/images/greymenu_services.gif';" src="../images/greymenu_services.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/support/"><img onmouseover="this.src = greymenusupportLit.src;" onmouseout="this.src = '/images/greymenu_support.gif';" src="../images/greymenu_support.gif" border="0" height="22" title="" alt="" /></a></li><li><img src="../images/greymenu_divider.gif" border="0" width="2" height="22" alt="|" /></li><li><a href="http://www.commandprompt.com/community/"><img onmouseover="this.src = greymenucommunityLit.src;" onmouseout="this.src = '/images/greymenu_community.gif';" src="../images/greymenu_community.gif" border="0" height="22" title="" alt="" /></a></li> </ul> <div style="padding-left: 16px; width: 96%;"> <HTML ><HEAD ><TITLE >Branching Logic</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.72 "><LINK REL="HOME" TITLE="Practical PostgreSQL" href="book1"><LINK REL="UP" TITLE="LXP" href="lxp"><LINK REL="PREVIOUS" TITLE="Tag Parsing" href="x21718"><LINK REL="NEXT" TITLE="Loop Iteration" href="x22084"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Practical PostgreSQL</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A href="x21718" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 13. LXP</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A href="x22084" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="USINGLOGIC" >Branching Logic</A ></H1 ><P >A simple method of conditionally rendering content lies in LXP's native support for a small set of branching logic tags. These allow you to either display or hide regions of markup by performing equivalence checks on variables or cookies. LXP's basic branching logic tags include:</P ><P > <P ></P ><UL ><LI ><P ><TT CLASS="LITERAL" ><if></TT ></P ></LI ><LI ><P ><TT CLASS="LITERAL" ><ifnot></TT ></P ></LI ><LI ><P ><TT CLASS="LITERAL" ><ifcookie></TT ></P ></LI ><LI ><P ><TT CLASS="LITERAL" ><ifnotcookie></TT ></P ></LI ><LI ><P ><TT CLASS="LITERAL" ><else></TT ></P ></LI ><LI ><P ><TT CLASS="LITERAL" ><elseif></TT ></P ></LI ><LI ><P ><TT CLASS="LITERAL" ><elseifnot></TT ></P ></LI ></UL > </P ><P >The <TT CLASS="LITERAL" ><if></TT > and <TT CLASS="LITERAL" ><ifnot></TT > tags operate on LXP variables (or object variable values), whereas the <ifcookie> and <ifnotcookie> tags operate on stored cookies for the current domain. In other words, the logical functions of <TT CLASS="LITERAL" ><if></TT > and <TT CLASS="LITERAL" ><ifcookie></TT > are the same; only the sources for logical evaluation differ.</P ><P >The <TT CLASS="LITERAL" ><else></TT > tag is more generalized, and implements subsequent, inverted logic evaluations on any of the previously mentioned tags. The <TT CLASS="LITERAL" ><elseif></TT > and <TT CLASS="LITERAL" ><elseifnot></TT > tags are actually just shortcut tags with the same result as nesting an <TT CLASS="LITERAL" ><if></TT > or <TT CLASS="LITERAL" ><ifnot></TT > tag within an <TT CLASS="LITERAL" ><else></TT > region.</P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN21921" >The <if> and <ifnot> Tags</A ></H2 ><P >When used without any accompanying attributes, the <TT CLASS="LITERAL" ><if></TT > and <TT CLASS="LITERAL" ><ifnot></TT > tags perform no useful function. However, with meaningful attributes, these tags can be used to quickly and simply flag regions of mark-up for display under specific circumstances.</P ><DIV CLASS="SECT3" ><H3 CLASS="SECT3" ><A NAME="AEN21926" >Using <if></A ></H3 ><P >The <TT CLASS="LITERAL" ><if></TT > tag examines its defined attributes through equivalence comparisons to variables whose names match the attribute names. If the specified attribute's value matches the variable's value exactly, the region of mark-up between that <TT CLASS="LITERAL" ><if></TT > and its associated <TT CLASS="LITERAL" ></if></TT > closing tag will be processed by LXP. Otherwise, that region (between <TT CLASS="LITERAL" ><if></TT > and <TT CLASS="LITERAL" ></if></TT >) will be completely ignored (including any LXP mark-up) up to its closing tag.</P ><P >You may include in the <TT CLASS="LITERAL" ><if></TT > tag either an attribute name, a complete attribute pair, or a series of attribute pairs, depending on the intended logical assessment you wish to make.</P ><P >Providing only an attribute name (e.g., <TT CLASS="LITERAL" ><if test></TT >) causes LXP to check only for the existence of <SPAN ><I CLASS="EMPHASIS" >any</I ></SPAN > characters assigned to the variable value with that name. In this case, if the variable is set to an empty value (or not set at all), the <TT CLASS="LITERAL" ><if></TT > match fails, and its defined region is <SPAN ><I CLASS="EMPHASIS" >muted</I ></SPAN > (not displayed). Otherwise, if a value is found, the region is processed as it would be normally.</P ><P >Providing one or more attribute pairs results in each attribute value being compared to the variable with the specified attribute name. When more than one attribute is specified in the tag, <SPAN ><I CLASS="EMPHASIS" >each</I ></SPAN > condition must match exactly for the <TT CLASS="LITERAL" ><if></TT > conditions to be considered a match as a whole, and for the region to be processed.</P ><P ><A href="x21880#USINGTHEIFTAG" >Example 13-13</A > uses the <TT CLASS="LITERAL" ><if></TT > tag to check for the existence of any variable value named <TT CLASS="LITERAL" >name</TT >, and compares the variable named <TT CLASS="LITERAL" >access</TT > to the value of 1.</P ><DIV CLASS="EXAMPLE" ><A NAME="USINGTHEIFTAG" ></A ><P ><B >Example 13-13. Using the <if> tag</B ></P ><PRE CLASS="SCREEN" ><lxp> <if name access="1"> <strong>Success!</strong><br /> A <em>name</em> is set, and <em>access</em> is set to 1.<br /> </if> </lxp></PRE ></DIV ></DIV ><DIV CLASS="SECT3" ><H3 CLASS="SECT3" ><A NAME="AEN21956" >Using <ifnot></A ></H3 ><P >The <TT CLASS="LITERAL" ><ifnot></TT > tag logically performs the opposite of the <TT CLASS="LITERAL" ><if></TT > tag in every respect. For example, when multiple attributes are passed, each equivalence comparison must <SPAN ><I CLASS="EMPHASIS" >fail</I ></SPAN > for the <TT CLASS="LITERAL" ><ifnot></TT > region to be processed.</P ><P ><A href="x21880#USINGTHEIFNOTTAG" >Example 13-14</A > uses the <TT CLASS="LITERAL" ><ifnot></TT > tag to test for the lack of a variable called <TT CLASS="LITERAL" >error</TT >, as well as to check that a variable named <TT CLASS="LITERAL" >access</TT > is not set to the value of <TT CLASS="LITERAL" >0</TT >.</P ><DIV CLASS="EXAMPLE" ><A NAME="USINGTHEIFNOTTAG" ></A ><P ><B >Example 13-14. Using the <ifnot> tag</B ></P ><PRE CLASS="SCREEN" ><lxp> <ifnot error access="0"> <strong>Success!</strong><br /> An <em>error</em> is not set, and <em>access</em> is not set to 0.<br /> </ifnot> </lxp></PRE ></DIV ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B >You may not define two attributes with the same name in a single LXP tag (e.g., <TT CLASS="LITERAL" ><ifnot access="0" access="2"></TT > is not valid). Therefore, two logical assessments on one variable requires the use of two logic tags.</P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="SECT3" ><H3 CLASS="SECT3" ><A NAME="AEN21979" >Nesting logic</A ></H3 ><P >The term <SPAN ><I CLASS="EMPHASIS" >nesting</I ></SPAN > refers to placing tags within regions marked-up by other tags. You may safely nest logical tags as much as you like, provided you carefully keep track of where they open and close.</P ><P >In some cases, you may have to nest logic tags in order to perform multiple checks on a single variable. This is because you can only place a variable's name inside of a logic tag once.</P ><P ><A href="x21880#USINGNESTEDLOGIC" >Example 13-15</A > nests several logic tags within one top-level <TT CLASS="LITERAL" ><if></TT > tag.</P ><DIV CLASS="EXAMPLE" ><A NAME="USINGNESTEDLOGIC" ></A ><P ><B >Example 13-15. Using nested logic</B ></P ><PRE CLASS="SCREEN" ><lxp> <if answer> <strong>You have supplied an answer!</strong><br /> <if answer="12"> Your answer is correct!<br /> </if> <ifnot answer="12"> Your answer of <putvar name="answer">, though, is incorrect.<br /> </ifnot> <if answer="12" cheatcode> You appear to be cheating, however. </if> </if> </lxp></PRE ></DIV ><P >In <A href="x21880#USINGNESTEDLOGIC" >Example 13-15</A >, the first <TT CLASS="LITERAL" ><if></TT > tag checks to see if an argument titled <TT CLASS="LITERAL" >answer</TT > is set at all. If it is not, the entire region it encapsulates is muted. </P ><P >The second <TT CLASS="LITERAL" ><if></TT > tag evaluates the passed <TT CLASS="LITERAL" >answer</TT > argument to see if it is equal to 12. If it is, that <TT CLASS="LITERAL" ><if></TT > tag's region is processed. Otherwise, that region will be muted.</P ><P >The <TT CLASS="LITERAL" ><ifnot></TT > tag then checks to see if the passed argument named <TT CLASS="LITERAL" >answer</TT > is <SPAN ><I CLASS="EMPHASIS" >not</I ></SPAN > equal to 12. If it is not, the region that the <TT CLASS="LITERAL" ><ifnot></TT > encapsulates will be processed.</P ><P >Lastly, the final <TT CLASS="LITERAL" ><if></TT > tag in <A href="x21880#USINGNESTEDLOGIC" >Example 13-15</A > checks to see if the passed value for <TT CLASS="LITERAL" >answer</TT > is equal to 12, and for the existence of a passed argument called <TT CLASS="LITERAL" >cheatcode</TT >. If the variable <TT CLASS="LITERAL" >answer</TT > is found to equal 12, and the variable <TT CLASS="LITERAL" >cheatcode</TT > is found at all, the region encapsulated by the last <TT CLASS="LITERAL" ><if></TT > tag will be processed (meaning, in this case, that it is merely displayed).</P ></DIV ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN22011" >Using <ifcookie> and <ifnotcookie></A ></H2 ><P >The <TT CLASS="LITERAL" ><ifcookie></TT > and <TT CLASS="LITERAL" ><ifnotcookie></TT > tags behave identically to the <TT CLASS="LITERAL" ><if></TT > and <TT CLASS="LITERAL" ><ifnot></TT > tags, with the notable exception being that they derive the source of their logical evaluations from the cookies stored in the browser for the domain being accessed by the web browser, rather than from stored variables.</P ><P ><A href="x21880#USINGIFCOOKIEANDIFNOTCOOKIE" >Example 13-16</A > welcomes a user with a personalized message if they have a cookie stored in their browser named <TT CLASS="LITERAL" >username</TT >.</P ><DIV CLASS="EXAMPLE" ><A NAME="USINGIFCOOKIEANDIFNOTCOOKIE" ></A ><P ><B >Example 13-16. Using ifcookie and ifnotcookie</B ></P ><PRE CLASS="SCREEN" ><lxp> <ifcookie username> Welcome back, <putcookie name="username">.<br /> </ifcookie> <ifnotcookie username> <include src="login.php" /> </ifnotcookie> </lxp></PRE ></DIV ><P >In <A href="x21880#USINGIFCOOKIEANDIFNOTCOOKIE" >Example 13-16</A >, if the <TT CLASS="LITERAL" >username</TT > cookie doesn't exist, the user will see a login screen provided by a PHP document. This document is rendered through an Apache sub-request inclusion (see <A href="x22143#INCLUDINGNONLXP" >the Section called <I >Including External Content Types</I ></A >").</P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN22036" >The <else>, <elseif>, and <elseifnot> Tags</A ></H2 ><P >The <TT CLASS="LITERAL" ><else></TT >, <TT CLASS="LITERAL" ><elseif></TT >, and <TT CLASS="LITERAL" ><elseifnot></TT > tags aid in the creation of more involved conditional logic than a single <TT CLASS="LITERAL" ><if></TT > or <TT CLASS="LITERAL" ><ifnot></TT > statement.</P ><P >The <TT CLASS="LITERAL" ><else></TT > tag marks a region to be displayed only if the last logical evaluation (at the same logical <SPAN ><I CLASS="EMPHASIS" >depth</I ></SPAN >, if working with nested logic tags) was <TT CLASS="LITERAL" >false</TT >. If the last logical evaluation was <TT CLASS="LITERAL" >true</TT >, the <TT CLASS="LITERAL" ><else></TT > region will be muted.</P ><P ><A href="x21880#USINGTHEELSETAG" >Example 13-17</A > creates a simple <TT CLASS="LITERAL" ><if></TT > condition to check for the existence of a variable called <TT CLASS="LITERAL" >answer</TT >. If it is not found, the region marked up by the <TT CLASS="LITERAL" ><else></TT > and <TT CLASS="LITERAL" ></else></TT > tags will be displayed; otherwise, that region will be muted.</P ><DIV CLASS="EXAMPLE" ><A NAME="USINGTHEELSETAG" ></A ><P ><B >Example 13-17. Using the <else> tag</B ></P ><PRE CLASS="SCREEN" ><lxp> <if answer> Thank you for supplying an answer. </if> <else> You have not yet supplied an answer.<br /> <include src="forms/question.lxp" /> </else> </lxp></PRE ></DIV ><P >As mentioned earlier in this section, the <TT CLASS="LITERAL" ><elseif></TT > and <TT CLASS="LITERAL" ><elseifnot></TT > tags are just shortcuts. They behave exactly as the <TT CLASS="LITERAL" ><if></TT > and <TT CLASS="LITERAL" ><ifnot></TT > tags do, respectively, if they were nested within an <TT CLASS="LITERAL" ><else></TT > region. For example, the following two blocks of markup are functionally identical:</P ><PRE CLASS="SCREEN" > <if condition1="true"> Condition 1 is True. </if> <else> <if condition2="true"> Condition 2 is true. </if> </else> ... <if condition1="true"> Condition 1 is True. </if> <elseif condition2="true"> Condition 2 is true. </elseif></PRE ><P >Using <TT CLASS="LITERAL" ><else></TT > tags streamlines both the maintainability and efficiency of the conditional logic. By using <TT CLASS="LITERAL" ><else></TT >, you can rely on LXP to keep track of whether or not the last condition was or was not met, and not have to re-evaluate the same conditions with the opposite logic tag.</P ><P ><A href="x21880#USINGNESTEDLOGICWITHELSETAGS" >Example 13-18</A > re-implements the same logic that was used in <A href="x21880#USINGNESTEDLOGIC" >Example 13-15</A > earlier in this section, but improves it with the use of the <TT CLASS="LITERAL" ><else></TT > tag.</P ><DIV CLASS="EXAMPLE" ><A NAME="USINGNESTEDLOGICWITHELSETAGS" ></A ><P ><B >Example 13-18. Using nested logic with <else> tags</B ></P ><PRE CLASS="SCREEN" ><lxp> <if answer> <strong>You have supplied an answer!</strong><br /> <if answer="12"> Your answer is correct!<br /> <if cheatcode> You appear to be cheating, however. </if> <else> Congratulations for not cheating! </else> </if> <else> Your answer of <putvar name="answer">, though, is incorrect.<br /> </else> </if> <else> You have not yet supplied an answer.<br /> <include src="forms/question.lxp" /> </else> </lxp></PRE ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A href="x21718" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A href="book1" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A href="x22084" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Tag Parsing</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A href="lxp" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Loop Iteration</TD ></TR ></TABLE ></DIV ></BODY ></HTML > </div> <br /> <div id="thefineprint"> <a href="http://www.commandprompt.com/products/mammothpostgresql/"><img src="../images/powered_by_mammoth.gif" alt="Powered by Mammoth PostgreSQL" title="Powered by Mammoth PostgreSQL" border="0" /></a> <br /> Copyright © 2000-2007 Command Prompt, Inc. All Rights Reserved. All trademarks property of their respective owners. </div> </div> </div> </body> </html>