Display comma-separated values in a different format

This part of the tutorial will cover a second situation that can appear when using comma separated values: both students and the foreign language courses are stored in a single table. For each student, the languages_std field stores the list of languages as plain text instead of foreign keys.

As this second approach uses a different model you will have to empty the existing database, and create a new page in the Dreamweaver site - languages.

In the new database structure only the student table is needed:

 

 

You have to add new entries in the table that add in the languages_std field the actual language names instead of the ID's. You should do so before proceeding, in order to better view the results.

A page that inserts information in such a table can be created s shown here. The only difference lies in the fact that now, the Comma-Separated Checkboxes widget will have to insert as values the language names. When configuring the user interface, make sure you select the same column - name_lan for both the value and label.

Retrieve and display information in the database

In this section you will learn how to retrieve information form the single table model and how to display the values it contains. Instead of displaying the languages separated by commas, a pipe will be used.

To achieve this, you must:

  1. Open the languages page in Dreamweaver.

  2. Create a new recordset that retrieves all informations from the student_std table. Go to the Bindings tab of the Application panel > Plus (+) > Recordset (Query). Configure the dialog box options for the rsStudents recordset as shown in the image below:


     

  3. From the Bindings tab expand the rsStudents recordset by clicking on the Plus (+) icon next to its name. Then drag and drop the name_std field on the page. Type :  and then drag and drop the languages_std field onto the page. Press SHIFT+ENTER to create a line break on the page.


     

  4. Select the entire page content. Go to the Application tab of the Insert bar and click on the Repeat Region icon. Configure it to use the rsStudents recordset and display all records:


     

  5. To test the page so far, save it and press F12 to load it into a browser. The student names and languages will be displayed:


     

  6. To change the way languages are displayed you have to switch to Code view. Locate the code that displays the languages, which should look like:

  7. To replace the commas with pipe signs (|) you will use a function which will replace all occurrence of commas with pipes. This function will be applied on the recordset field itself. The code section will become:

    1. For PHP:

      <?php echo str_replace(","," | ",$row_rsStudents['languages_std']); ?>

       

    2. For ASP_VBScript

      <%=replace(rsStudents.Fields.Item("languages_std").Value,","," | ")%>

       

    3. For ColdFusion

      <cfoutput query="rsStudents"> #rsStudents.name_std# : #Replace(rsStudents.languages_std,","," | ","all")#
      <br />
      </cfoutput>
       

  8. To test the page after changing it, save and press F12:


     

Simply replacing a sign with another is not the only possibility. You can replace the commas that are added by default with HTML mark-up, thus creating rows or columns into which to display the information.