Conditional Content

[ 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

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.

Figure 23.32. The Show Region objects in the Insert bar.


To use a Show Region object, do the following:

  1. Create the content you want to hide or show based on the recordset.

  2. Select that content (make sure you get all of it, including opening and closing tags!), and choose one of the Show Region objects. The display in Design view shows your conditional content surrounded by a labeled box.

Other Kinds of Conditional Content

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.

Displaying Content Only If a Record Field Is Not Empty

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 .

Table 23.8. PHP Syntax for Displaying Content Based on Whether a Field Has Content
 

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 } ?> 


Table 23.9. ColdFusion Syntax for Displaying Content Based on Whether a Field Has Content
 

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> 


Displaying Content Based on a URL Parameter

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.)

Table 23.10. PHP Syntax for Displaying Content Based on a URL Parameter
 

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 } ?> 


Table 23.11. ColdFusion Syntax for Displaying Content Based on a URL Parameter
 

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> 


Exercise 23.7. Adding Conditional Content to the Antiques Barn Login Page (ColdFusion and PHP)

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.

  1. Start by opening the login page. This page redirects the user back to itself if the user's login is unsuccessful . You want it to pass a parameter as it's doing this.

    In the Server Behaviors panel, double-click the Log User In behavior to open it for editing. In the field that specifies where to redirect the user if the login fails, change the entry by adding an msg parameter set to sorry . You can type this parameter in by hand or use the Browse and Parameter buttons . The code in the input field should look like this:

     login.php?msg=sorry 

    (The filename extension, of course, depends on your server model.) When you're done, click OK to close the dialog box.

  2. Add a paragraph of text immediately preceding the form that says something like Sorry, username or password is incorrect; try again as shown in Figure 23.33. Using the Property Inspector or Tag selector, apply the sorry class to the paragraph.

    Figure 23.33. Adding a Sorry! message to the login page for unsuccessful logins.


  3. Switch to Code view, and find your sorry paragraph. You need to surround it with conditional statements, to display only if the msg parameter is present and is set to sorry . The exact code you'll enter depends on the server language you're using:

    In PHP:

     <?php  if isset($_GET[msg]) && $_GET[msg] == 'sorry') { ?>  <p class="sorry">Sorry, login was unsuccessful...</p> <?php } ?> 

    In ColdFusion:

     <cfif  isdefined("URL.myParam") AND #URL.msg# EQ "sorry">  <p class="sorry">Sorry, login was unsuccessful...</p> </cfif> 

  4. Switch back to Design view. Your conditional content appears. If you're using PHP (and if invisible elements are showing), it is surrounded by gold server-side script icons, as shown in Figure 23.34. If you're using ColdFusion, a labeled box surrounds your content, as if you had applied the Show If server behavior.

    Figure 23.34. Conditional content in the Antiques Barn login page, as seen in Design view.


  5. Try things out in a browser. If you preview the login page, the conditional content doesn't show. If you manually add the ?msg=sorry parameter to the URL field, the sorry message shows. If you try to log in, and you fail, the URL parameter and the sorry message appear.

  6. Open the catalog page. This page has restricted access. Users without access are redirected to the login page.

    In the Server Behaviors panel, double-click the Restrict Access to Page behavior to open it for editing. If access is denied , send the user to the login page with the msg parameter set to a different value:

     login.cfm?msg=noaccess 

    Click OK to close the dialog box.

    Double-click the Log Out User behavior to open it. When the logout is complete, send the user to the login page with another msg :

     login.php?msg=logout 

    Click OK to close the dialog box, and save and close the file.

  7. Go back to the login page. You need two more sets of conditional content. Select your existing conditional content, including the gold server-code icons, and select Edit > Copy. Place the insertion point after the closing icon, and select Edit > Paste twice. Change the second conditional paragraph to say something like You need to log in to view this page . Change the third to say something like You have been logged out . Your page should now look like Figure 23.35.

    Figure 23.35. The Antiques Barn login page with three sets of conditional content.


  8. You need to change the conditional scripting for the new conditional regions . You could go to Code view, but you can also use the Property Inspector. Select the opening tag for the second set of content. (For PHP, click the gold icon; for ColdFusion, use the Tag selector to select the cfif tag.) In the Property Inspector, change the msg value from sorry to noaccess . Select the opening tag for the third set of content, and in the Property Inspector, change the msg value from sorry to logout . These values match those you entered in the other pages.

  9. Try your pages in a browser. Log yourself in and out, successfully and unsuccessfully, and see how the login page customizes itself.

[ LiB ]


Macromedia Dreamweaver MX 2004 Demystified
Macromedia Dreamweaver MX 2004 Demystified
ISBN: 0735713847
EAN: 2147483647
Year: 2002
Pages: 188
Authors: Laura Gutman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net