Flash
  Home arrow Flash arrow Page 3 - Building the Back End of a Content Managem...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Building the Back End of a Content Management System for Flash
By: Jennifer Sullivan Cassidy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2006-08-02

    Table of Contents:
  • Building the Back End of a Content Management System for Flash
  • The Back End Elements
  • 2. The PHP Code
  • Gluing it All Together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building the Back End of a Content Management System for Flash - 2. The PHP Code


    (Page 3 of 4 )

    Now that we've created our basic form, we should write the PHP to process the information. For now, we'll call the script "backend.php," which we referred to in our HTML form action, using the post method. 

    First, after we encapsulate our PHP with the <?php and ?> tags, we will need to write the data.

    if (isset($_POST['submit'])) {
    $content1_txt = $_POST['content1_txt'];

    Upon hitting submit our form action is POST; we need to post the contents of the variable, "content1_txt", giving its contents over to the new posted variable, "content1_txt".

    Now, we have to open the text file to which we will be posting the data. If we cannot, we need to say so.

    $fp = fopen("data.txt","w+");
    if(!$fp) {
        echo 'Error, the file could not be opened or there was an error creating it.';
        exit;
    }else {     echo 'Your content has been saved.'."n";
    }

    We first define the $fp variable as set to open the data file which we want for reading and writing, or "w+," as defining it first provides a shortcut of sorts for the next line.  The first line tells the server to open the file we want, which is our data.txt file, by using the "fopen()" function.  The fopen() function is used to open files in PHP. The w+ mode allows for read and/or write. This mode opens and clears the contents of the file, or creates a new file if it doesn't exist. If we only wanted to read the file, then it would be "r."  But because we want to write data to this file, we need to tell the server to do so.

    The next section of code means that, if the file is not able to be opened, it will return the error message, and exit; otherwise, return a statement that the file has been opened and the content can be saved. Remember our defined variable from the first line? In PHP, the exclamation mark means "not." So we are saying, in short, if our variable is NOT as we defined it earlier, then do this, which is, in this case, provide the error message and exit. But if it does match, then the server disregards this line and moves on to the next.

    Next, we need to put the data entered into the form into the file we just opened or created, then actually write the data.

    fwrite ($fp, "content1_txt=");
    fwrite ($fp, $content1_txt."n");

    So we use the fwrite() function.  We also need to tell the server WHAT to write to the file, and in this case we need the contents of our variable written, so we include that between the parentheses of our function.

    Wait a second -- it looks like we've attempted to write the variable twice! Look closer.  We only need one variable's contents written to the file. However, in order for Flash to know what to send to the dynamic area of our movie, we need to make sure that the actual variable name is written in the file so that Flash can identify it. (We will show this specifically when we look at the data file in the next section).  So the first line is basically writing the variable name used by Flash. The second line is writing the data from the form. 

    Now, I apologize if I've made this a bit confusing.  For simplicity's sake, I gave the variable from the HTML form the same name as the Flash dynamic variable.  But we could have easily named them something different. So if I'd called the form variable "text" and the dynamic variable "flash," then our HTML form code would have looked like this:

    <textarea name="text" cols="40" rows="10">
    </textarea>

    And the PHP would look like more this:

    fwrite ($fp, "flash=");
    fwrite ($fp, $text."n");

    The great thing about variables is that you can name them anything you like, as long as you remember that you have to call it whatever you named it.

    NOTE: Odds are that you are going to have more than one dynamic variable in your Flash.  Flash requires that you prefix your variables with the ampersand sign (&) in any external source so that Flash can distinguish what is a variable and what isn't when there are multiple variables. So in ActionScript code, I should use &content1_txt instead of content1_txt; however since I am only using one variable in our demonstration, we are okay without it.  Moving on...

          echo 'This is what you submitted:'."n";
          readfile("data.txt");

    This next part of the code is really not necessary for the script to work.  But if you want to view what you have just entered without having to download or view your data.txt file, then you may want to leave this part in. The next part of the code instructs the server to read the contents you've just written and echo that back to the browser. You could use a few different commands or functions to read the file contents and echo them back, as the "readfile" command doesn't use any formatting at all. But because that is not the focus of this tutorial, we don't need any formatting of the read data for now.

    Finally, we must close the file.

    fclose($fp);
    }

    In summary, here's what your PHP code in backend.php might look like all together:

    <?php

    //write the data
    if (isset($_POST['submit'])) {
    $content1_txt = $_POST['content1_txt'];

    //open the text file
    $fp = fopen("data.txt","w+");
    if(!$fp) {
        echo 'Error, the file could not be opened or there was an error creating it.';
        exit;
    }else {     echo 'Your content has been saved.'."n";
    }

    //put data from the form into the text file
    fwrite ($fp, "content1_txt=");
    fwrite ($fp, $content1_txt."n");

    //read back the text file data
          echo 'This is what you submitted:'."n";
          readfile("data.txt");

    //close the file
    fclose($fp);
    }
    ?>

    3.  The Flat File Database

    As I've shown in the previous element, we need to store the data that we asked PHP to post from our form.  Remember the ActionScript we wrote in the first tutorial?  We referred to "data.txt" as our source from which to load our variables into Flash, as we also did in backend.php.  This is our flat file database.  It is nothing more than a simple text file.

    Notice that we have our variable "content1_txt" in there, as we asked PHP to echo for us, so that Flash can identify the variable, and know it's the correct one to place in our dynamic area. You need to make sure that there are no spaces between the variable, the "=" sign and the content you wish to plug into Flash.  This is important.  (As mentioned above in my update, the data.txt variables, when there are multiple variables, should always begin with the ampersand sign (&)).

    More Flash Articles
    More By Jennifer Sullivan Cassidy


       · Did you find this article helpful or useful? I certainly hope that you are able to...
       · This article was exactly what I was looking for! However, I think that a third...
       · This was a great tutorial for the basics, however it did not work for me. I got a...
       · This was exactly what i needed for a site i was working on!!!
     

    FLASH ARTICLES

    - Decorator Pattern
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT