[ LiB ] |
Conditional content is page content that displays only if certain conditions are met. It can be dynamic or static content, but it must be in a dynamic page. In the various server-side scripting languages, it's usually created with some sort of if statement (if x is true, display this) or if-else statement (if x is true, display this; else display that). The Dreamweaver Show Region objects let you show or hide contents based on recordset conditions. Any other kind of conditional content (based on URL parameters, session variables , and so on) you can create yourself if you know the proper syntax for your server model.
The Show Region objects, shown in Figure 23.32, let you show or hide selected page content based on various recordset conditions: if the recordset is empty or not, or if recordset paging controls are displaying the first or last records of the set. This is very handy for creating special messages, such as "We're sorry; no records matched your search criteria." Show Region objects are also used in the Recordset Navigation Status object to hide or show the paging links for navigating through records.
To use a Show Region object, do the following:
There are all sorts of conditions besides these few recordset conditions that you might want to use to hide or show page content. How about hiding or showing a dynamic image depending on whether the current record has any data for a dynamic image? Hiding or showing top-secret information depending on whether a user is logged in? Hiding or showing titles or messages depending on the presence of URL parameters?
Conditional content makes it possible to use one page for multiple purposes instead of having to create separate pages.
Turning Dreamweaver page elements into conditional content isn't difficult if you don't mind a little bit of typing in Code view and if you know the syntax required by your server language. To create any conditional content, first create the content in Design view. Then surround it with the if statement required by your server language.
Some records in a database might contain empty fields. If so, you don't want to try displaying their data. You can create conditional content to be displayed only if the current record has data in that field, or only if the record has no data. You can also choose to display alternative sets of contentone if there's content and one if there's not. Tables 23.8 and 23.9 list the syntax requirements for the different server models covered here. For all examples, substitute the name of your recordset for Recordset1 and the name of your field for myField .
Syntax | |
---|---|
Show if field is not empty | <?php if ($row_Recordset1['myField'] != "") { ?> [ display this content if the field is empty ] <?php } ?> |
Show if field is empty | <?php if ($row_Recordset1['myField'] == "") { ?> [ display this content if the field is empty ] <?php } ?> |
Show alternate content | <?php if ($row_Recordset1['myField'] != "") { ?> [ display this content if the field is not empty ] <?php }else{ ?> [ display this content if the field is empty ] <?php } ?> |
Syntax | |
---|---|
Show if field is not empty | <cfif #Recordset.myField# NEQ ""> [ display this content if myField is not empty ] </cfif> |
Show if field is empty | <cfif #Recordset.myField# EQ ""> [ display this content if myField is empty ] </cfif> |
Show alternate content | <cfif #Recordset.myField# NEQ ""> [ display this content if the field is not empty ] <cfelse> [ display this content if the field is empty ] </cfif> |
You can repurpose and tweak pages to suit different visitors by passing different URL parameters in their addresses. For instance, the first time a user visits the login page, no extra content is displayed; if the login fails, the page includes an error message; if the user was redirected here after trying to visit a restricted page, a redirect message appears; and so forth. The conditional statement you write must test for the existence of a URL parameter and then must test for its value. Tables 23.10 and 23.11 show the required syntax for this kind of conditional content in the different server languages. For each example, substitute the name of your parameter for myParam and the value to test for for myValue . (Note that no recordset needs to be collected for this to work.)
Syntax | |
---|---|
Show if parameter exists and has a certain value | <?php if isset($_GET['myParam']) && $_GET['myParam'] == 'myValue') { ?> [ content to display ] <?php } ?> |
Show alternate content | <?php if isset($_GET['myParam']) && $_GET['myParam'] == 'myValue') { ?> [ content to display ] <?php }else{ ?> [ alternate content to display ] <?php } ?> |
Syntax | |
---|---|
Show if parameter exists and has a certain value | <cfif isdefined("URL.myParam") AND #URL.myParam# EQ "myValue"> [ content to display ] </cfif> |
Show alternate content | <cfif isdefined("URL.myParam") AND #URL.myParam# EQ "myValue"> [ content to display ] <cfelse> [ alternate content to display ] </cfif> |
If you've completed all the exercises in this chapter, you've sent your visitors to the login page from a variety of other pages. You can make your user interface more understandable by adding custom messages to the login page depending on why the user got sent here. Before doing this exercise, you need to have completed the login exercises earlier in this chapter.
login.php?msg=sorry
<?php if isset($_GET[msg]) && $_GET[msg] == 'sorry') { ?> <p class="sorry">Sorry, login was unsuccessful...</p> <?php } ?>
<cfif isdefined("URL.myParam") AND #URL.msg# EQ "sorry"> <p class="sorry">Sorry, login was unsuccessful...</p> </cfif>
login.cfm?msg=noaccess
login.php?msg=logout
[ LiB ] |