7 Replies Latest reply on Nov 7, 2007 11:01 PM by xparrot_dude

    Pull images from SQL/PHP into Flash

    xparrot_dude
      I have a website that displays images that are stored in a SQL database using PHP as a method for pulling and displaying the images. These images are stored in the database as text (ex: image1), then the PHP looks for the image like this:
      <img src="images/Photograph/<?php echo $row_rs_Photos['Image']; ?>.jpg" alt="" width="<?php echo $row_rs_Photos['Thumb_Width']; ?>" height="<?php echo $row_rs_Photos['Thumb_Height']; ?>" border="0" title="" />

      I want to pull these images into Flash. I've done a bunch of ActionScripting, but am no expert. Really don't have the foggiest idea where to start.

      -kirk
        • 1. Re: Pull images from SQL/PHP into Flash
          Greg Dove Level 4
          A couple of questions....
          Do you want to access data for a single image when flash calls your php script (like in your example) or do you want the php script to return a list (array) of images and associated data (width and height)?
          And do you have a preferred format for sending the data from php to flash (urlencoded name=value&name=value etc or xml)?
          (Formats other than urlencoded (flash LoadVars if its in as2) and XML are possible but you would need a non-native parser.)
          • 2. Re: Pull images from SQL/PHP into Flash
            The Feldkircher Level 2
            Hi

            Take a look at THIS it does exactly what you want with minimal scripting. You can download the source .fla at the same location.

            Hope it helps
            • 3. Re: Pull images from SQL/PHP into Flash
              xparrot_dude Level 1
              Thank you very much. Did a partial tutorial and was able to pull in a table from SQL. Awesome. Thanks.

              -Kirk
              • 4. Re: Pull images from SQL/PHP into Flash
                xparrot_dude Level 1
                I'm still having trouble with this. I was able to get a list of text into flash from a PHP file. I have since tried various ways with minimal success.

                I know just enough PHP to get myself in trouble. Dreamweaver writes almost all the PHP I've done.

                I have about 200 photographs on my website stored in a SQL database, I successfully show them on my site using PHP. I would like to make the way they are displayed a little fancier.

                But I'm having a hell of a time getting them into Flash.

                I've tried tutorials for passing variables, but they haven't worked. I managed to get a listing of all the names of the images into Flash, but they are in a list bar and I can't figure out how to get them out so that I can display the actual photos instead of the Names of them. Also, to make things more difficult these photos don't all reside in the main "images" folder directory level, but in subfolders within. These subfolder names are stored with each record in my database, but I cannot figure out how to get the image name and the folder name into Flash.

                I have tried more tutorials than I care to admit with varying degrees of success.

                This one sort of worked(press 3,4,5).

                It was the simplest one I found, but I really need a method to grab all of my images so I can then get them into a scroller.

                In PHP, I can just grab the variable I need from SQL and fiddle with it, I cannot seem to get the variables I need into Flash and make them dance.

                I just need a down and dirty, simple method for pulling multiple variables into Flash.

                Any help would be appreciated.

                -Kirk
                • 5. Re: Pull images from SQL/PHP into Flash
                  xparrot_dude Level 1
                  My overall goal is to make a scrollbar of the images that you can click on for more details. Sort of a Apple "Cover Flow" look.

                  I would need to create movieClips dynamically that are then filled with variables from SQL. So I am currently in "baby step" mode. Get the photos in, then make them do gymnastics.
                  -Kirk
                  • 6. Pull images from SQL/PHP into Flash
                    xparrot_dude Level 1
                    I also tried this tutorial trying both methods under "Receiving using LoadVars.load" for getting variables into Flash. I followed the directions exactly, but got no results, so I made a dynamic text box with the variable filled in as "c" and this Actionscript on the cell:

                    var c = new LoadVars();
                    c.onLoad = function() {
                    returnvals.text = "returned from php: \n\n";
                    for (i in this) {
                    returnvals.text += i+" = "+this +"\n";
                    }
                    };
                    var siteUrl:String = " http://www.lbiphotos.com";
                    c.load(siteUrl+"/Flash/variables.php");
                    trace(siteUrl+"/Flash/variables.php");
                    trace(c);

                    The PHP file called "variables.php" has this code in it (exactly as written in the tutorial):
                    <?php
                    $lastName = "Anderson";
                    $phone = "301-424-5555";
                    echo "lastName=".$lastName."&phone=".$phone;
                    ?>

                    Since that code didn't work, I tried the following:
                    ...
                    echo "&lastName=".$lastName."&phone=".$phone;
                    ...
                    echo "$lastName=".$lastName."$phone=".$phone;
                    ...
                    echo "lastName=".$lastName&"phone=".$phone;

                    None worked. The tutorial never mentioned how these variables were going to be displayed in the Flash. This is all I get with a dynamic text box.

                    Anyway, as you can see from my posts, I really am trying everything I can find.

                    -Kirk
                    • 7. Re: Pull images from SQL/PHP into Flash
                      xparrot_dude Level 1
                      I seem to have this one working, kinda. It has an array and automatically numbers the buttons, but there are some buttons, that ask SQL to return a photo that doesn't exist. For instance, there is no 7 or 8 ID in my SQL, so those buttons don't work:
                      http://www.lbiphotos.com/Flash/loadImages_finished.html

                      PHP Code:
                      <?php require_once('my.php'); ?>
                      <?php
                      if (!function_exists("GetSQLValueString")) {
                      function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
                      {
                      $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

                      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

                      switch ($theType) {
                      case "text":
                      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                      break;
                      case "long":
                      case "int":
                      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                      break;
                      case "double":
                      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
                      break;
                      case "date":
                      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                      break;
                      case "defined":
                      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                      break;
                      }
                      return $theValue;
                      }
                      }

                      $colname_rs_Photos = "-1";
                      if (isset($_GET['passID'])) {
                      $colname_rs_Photos = (get_magic_quotes_gpc()) ? $_GET['passID'] : addslashes($_GET['passID']);
                      }
                      mysql_select_db($database_LBI_Photos_Connection, $LBI_Photos_Connection);
                      $query_rs_Photos = sprintf("SELECT ID_Photos, Image, YearPhotographed FROM Photos WHERE ID_Photos = %s", GetSQLValueString($colname_rs_Photos, "int"));
                      $rs_Photos = mysql_query($query_rs_Photos, $LBI_Photos_Connection) or die(mysql_error());
                      $row_rs_Photos = mysql_fetch_assoc($rs_Photos);
                      $totalRows_rs_Photos = mysql_num_rows($rs_Photos);

                      $imageId = $_GET['passID'];
                      //MUST use code below to grab $images
                      $images = $row_rs_Photos['YearPhotographed'].'/'.$row_rs_Photos['Image'];
                      print "image=" . $images;
                      ?>
                      <?php
                      mysql_free_result($rs_Photos);
                      ?>

                      ACTIONSCRIPT:
                      function loadImage(passID:Number) {
                      var resultLv:LoadVars = new LoadVars();
                      var sendLv:LoadVars = new LoadVars();
                      resultLv.onLoad = function(success) {
                      if (success) {
                      mcLoader.loadClip(imagePath+this.image+".jpg", loadContainer);
                      }
                      };
                      trace("passID: "+passID);
                      sendLv.sendAndLoad(phpPath+"?passID="+String(passID), resultLv);
                      }
                      function loadInit(target:MovieClip) {
                      trace("Begin Loading File");
                      trace("imagePath + this.image ; "+imagePath+this.image);
                      }
                      function assignButtons() {
                      for (var i in loadButtons) {
                      this[loadButtons ].numberTxt.text = String(Number(i)+1);
                      this[loadButtons
                      ].onRollOver = function() {
                      this.gotoAndStop(2);
                      };
                      this[loadButtons ].onRollOut = function() {
                      this.gotoAndStop(1);
                      };
                      this[loadButtons
                      ].onRelease = function() {
                      trace("String(this).length "+String(this).length);
                      if (String(this).length == 16) {
                      loadImage(Number(String(this).substring(String(this).length-1)));
                      }
                      if (String(this).length == 17) {
                      loadImage(Number(String(this).substring(String(this).length-2)));
                      }
                      };
                      }
                      }
                      var loadButtons:Array = new Array('loadBtn1', 'loadBtn2', 'loadBtn3', 'loadBtn4', 'loadBtn5', 'loadBtn6', 'loadBtn7', 'loadBtn8', 'loadBtn9', 'loadBtn10', 'loadBtn11', 'loadBtn12', 'loadBtn13', 'loadBtn14', 'loadBtn15', 'loadBtn16');
                      var loadContainer:MovieClip = this.createEmptyMovieClip("loadContainer", this.getNextHighestDepth());
                      var siteUrl:String = " http://www.lbiphotos.com";
                      var imagePath:String = siteUrl+"/images/Photograph/";
                      var phpPath:String = siteUrl+"/Flash/images.php";
                      var mcLoader:MovieClipLoader = new MovieClipLoader();
                      var mcLoadObj:Object = new Object();
                      mcLoadObj.onLoadInit = loadInit;
                      mcLoader.addListener(mcLoadObj);
                      //below centers called image
                      loadContainer._x = (Stage.width/2-100);
                      loadContainer._y = (Stage.height/2-100);
                      assignButtons();