SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict ozmart2004's Avatar
    Join Date
    Feb 2004
    Location
    Albury, NSW, Australia
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP script for Flash app

    I need to write a php script to move data between a Flash app and a MySQL database. See the attached flowchart image for a more detailed explanation. I would appreciate some ideas and perhaps some code example about how to approach this. Thks.
    Attached Images Attached Images

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sure there are a lot of docs, samples and tutorials on this. Generally, you need to print your data as url-encoded string or xml, and use either LoadVars or XML object in flash.

  3. #3
    SitePoint Addict ozmart2004's Avatar
    Join Date
    Feb 2004
    Location
    Albury, NSW, Australia
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the script I've worked up so far (with thanks to http://www.flash-db.com/Tutorials/lo...ata.php?page=5):

    Code:
    <?php 
    /* get start and end date values - var contains name/value pairs passed to this script from query string attached to URL */
    if (isset($_GET['startDate'])) {
    $startDate = $_GET['startDate'];
    }
    if (isset($_GET['endDate'])) {
    $endDate = $_GET['endDate'];
    }
    echo ("Start date is $startDate");
    echo ("<br />");
    echo ("End date is $endDate");
    /* include db connection info */
     include("dbconfig.php"); 
     /* Connect to db */
    if (!$connect = mysql_connect($dbhost, $dbuser, $dbpass)){
    	die('Could not connect to server');
    	}
    
    /* select db */
    if (!mysql_select_db($dbname, $connect)){
    	die ('Could not select database');
    	}
    	echo ('Connection Successful!');
    /* SQL query */
    $sql = "SELECT DISTINCT FROM `mod_calendar_events` WHERE `startDate` = $startDate AND `endDate` = $endDate ORDER BY 'startDate'");
    $result = mysql_query($sql, $connect);
    /* loop through the results */
    $cant = 0;
    while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
    	echo "title$cant=$row[title]&description$cant[description]&startTime$cant[starttime]&endTime$cant[endtime]&startDate$cant[startdate]&endDate$cant[enddate]";
    	$cant++;
    }
    echo "cant=$cant";
    
    /* Close the connection */
    mysql_close($connection);
    ?>
    I'm not sure about the SQL in particular!

    One question I have is with the dates. Is it better to have Flash format the dates so that MySQL likes them or convert the format in the SQL? If the latter, a code exapmle would be appreciated :-)

    If someone would mind very much looking over my code I would appreciate it!!

    Thks
    Martin

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some comments to your code

    PHP Code:

    // if there are no start and endDate in GET they are set to empty string
    // initialize them with default values before this block

    if (isset($_GET['startDate'])) {
        
    $startDate $_GET['startDate'];
    }
    if (isset(
    $_GET['endDate'])) {
        
    $endDate $_GET['endDate'];
    }


    echo (
    "Start date is $startDate");
    echo (
    "<br />");
    echo (
    "End date is $endDate");

    /* include db connection info */
     
    include("dbconfig.php"); 

     
    /* Connect to db */
    if (!$connect mysql_connect($dbhost$dbuser$dbpass)){

    // die(message) shouldn't be used in flash backend
    // you must provide error informaition so that flash understands it
    // ie. die("&error=Could not connect to server'");

        
    die('Could not connect to server');
        }

    /* select db */
    if (!mysql_select_db($dbname$connect)){
        die (
    'Could not select database');
        }
        echo (
    'Connection Successful!');
    /* SQL query */

    // note: backticks `` are not quotes ' ' 
    // should be ORDER BY `startDate` or just ORDER BY startDate

    $sql "SELECT DISTINCT FROM `mod_calendar_events` WHERE `startDate` = $startDate AND `endDate` = $endDate ORDER BY 'startDate'");
    $result mysql_query($sql$connect);
    /* loop through the results */
    $cant 0;
    while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC)){

    // you have to url-encode your data
    // (assuming you don't use international characters)
    // and 
    // you should quote array indices and enclose array elements in {}
    // for proper interpolation:
    // title$cant={$row['title']}&description$cant={$row['description']} etc
    // or use loop like this:

     
    foreach($row as $fieldName => $value) {
         echo  
    '&' $fieldName $cant '=' urlencode($value);
      }


        echo 
    "title$cant=$row[title]&description$cant[description]&startTime$cant[starttime]&endTime$cant[endtime]&startDate$cant[startdate]&endDate$cant[enddate]";
        
    $cant++;
    }
    echo 
    "cant=$cant";


    /* Close the connection */
    mysql_close($connection); 
    As to sql, it depends on what your tables look like. Can you post your create table statement?

  5. #5
    SitePoint Addict ozmart2004's Avatar
    Join Date
    Feb 2004
    Location
    Albury, NSW, Australia
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks very much......I took your advice as much as I could, although I was not sure about how/where to implement the URL encode:

    Code:
    <?php 
    /* initialize variables */
    $today = date(Y m d);
    $_POST['startDate'] = $today;
    $_POST['endDate'] = $today;
    /* get start and end date values - var contains name/value pairs passed to this script from query string attached to URL */
    if (isset($_POST['startDate'])) {
    $startDate = $_POST['startDate'];
    }
    if (isset($_POST['endDate'])) {
    $endDate = $_POST['endDate'];
    }
    
    echo ("Start date is $startDate");
    echo ("<br />");
    echo ("End date is $endDate");
    
    /* include db connection info */
     include("dbconfig.php"); 
     
     /* Connect to db */
    if (!$connect = mysql_connect($dbhost, $dbuser, $dbpass)){
    	die("&error=Could not connect to database server");
    	}
    
    /* select db */
    if (!mysql_select_db($dbname, $connect)){
    	die ("&error=Could not select database");
    	}
    	echo ('Connection Successful!');
    	
    /* SQL query */
    $sql = "SELECT DISTINCT FROM mod_calendar_events WHERE startDate = $startDate AND endDate = $endDate ORDER BY startDate");
    $result = mysql_query($sql, $connect);
    /* loop through the results */
    $cant = 0;
    while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
    	echo "title$cant={$row[title]}&description$cant={$row[description]}&startTime$cant={$row[starttime]}&endTime$cant={$row[endtime]}&startDate$cant={$row[startdate]}&endDate$cant={$row[enddate]}&";
    	$cant++;
    }
    echo "cant=$cant";
    
    /* Close the connection */
    mysql_close($connection);
    ?>

    If the data is just displaying on the page for the user to copy and paste to word, is it really neccessary? Otherwise, I'm not sure which bit to use it on?

    I've uploaded the table as a word file if that helps.

    Cheers
    Martin
    Attached Files Attached Files

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please test your code before posting. And use [ php ] tags, not [ code ].

    Urlencode is used to escape special characters in data. For example, what happens in your snippet if $row['title'] contains '&' ?

  7. #7
    SitePoint Addict ozmart2004's Avatar
    Join Date
    Feb 2004
    Location
    Albury, NSW, Australia
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thks. I will be able to test it today once I get the Flash app.

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Boy sounds to me like you would benefit from passing XML back to the flash movie, for ease of parsing and scalability.

    You are using php, so have you looked at remoting, specifically amfphp?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •