Hi,

does anyone have any idea if this is possible?

I have 5 buttons in a flash file, and each one points to a page which displays a set of records from a table. I'm looking to have the button greyed out/inactive if the associated table contains no records.

I'd imagine it can be done, but I'm not really sure how to go about it as I've never worked with databases and flash before.

Thanks in advance.

    sounds like a flash question, not a php one

      This is true - I just hoped someone here would have a bit of experience in integrating the two.
      I'll try a flash forum...

        it's possible
        I used Flash to show a slideshow of images based on Flash calling a php page which called the database - I modified a Flash project I found and showed text and pics from the db - it was from "rabid gadfly"s website - google it
        ..but I don't know Flash well enough to advise further

          Hi, like said before, this is more of flash question.

          If you haven't solved it already, the way to do this is to pass an argument to the flash swf file, this is done through passing paramaeters by appending it to the URL (as you would in any normal page).

          I am forgetting the exact ActionScript syntax for this but can look it up if you want (used something like this in a project more than 3 years back)

            flash guy here.

            on startup, use flashvars to pass any parameters, i'd suggest you use SWFObject. during runtime use the LoadVars object to make and receive request from a php file.

            flashvars sample - simplified

            var so = new SWFObject(...arguments);
            so.addVariable("key", "value");
            

            LoadVars sample

            var res = new XML();
            res.onData = function (str) {
                // do something in flash once you have received your data
                trace("php returned: " + str);
            }
            
            var req = new LoadVars();
            req.param1 = "aaa"; // parameters you want to send to your php
            req.param2 = "bbb"; // parameters you want to send to your php
            req.loadAndSend("url_to_your_php", res); // call your php file and set the call back
            

              While sophistikat's solution will work in AS2, it won't in AS3 which has gotten rid of LoadVars. You can instead access URL variables using loaderInfo:

              root.loaderInfo.parameters.var1
              root.loaderInfo.parameters.var2

              etc., replacing var1, var2 with the actual variable names.

                This is entirely feasible. This tutorial might be helpful for helping you understand how Flash and PHP can work together.

                  7 days later

                  Hi,

                  Thanks for the suggestions.
                  I ended up getting around the flash part of this question by using LoadVariables.
                  There's one more thing holding me back - flash requires that the output is formatted like

                  var1=value1&var2=value2 etc...

                  For testing, I had set this up using

                  // count instances of each region in each table
                  $query = 'SELECT COUNT(*) FROM `infrastructure` WHERE `Region_Id` = 1';
                  $result = mysql_query($query) or die('Sorry, we could not count the number of results: ' . mysql_error());
                  $inf1 = mysql_result($result, 0);
                  // echo the variables
                  print "&inf1=$inf1";

                  I'm assuming there's an easier way to do this for all regions than to repeat that block of code for each one. I've set up a loop as follows, which echoes the variables for region 1 as many times as there are regions (as I expected).

                  mysql_select_db($database_ernact, $ernact);
                  $query_getRegions = "SELECT Region_Id FROM regions";
                  $getRegions = mysql_query($query_getRegions, $ernact) or die(mysql_error());
                  $row_getRegions = mysql_fetch_array($getRegions);
                  $totalRows_getRegions = mysql_num_rows($getRegions);
                  
                  while ($row_getRegions = mysql_fetch_array($getRegions)) {
                  // count instances of each region in each table
                  $query = 'SELECT COUNT(*) FROM `infrastructure` WHERE `Region_Id` = 1';
                  $result = mysql_query($query) or die('Sorry, we could not count the number of results: ' . mysql_error());
                  $inf1 = mysql_result($result, 0);
                  
                  // echo the variables
                  print "&inf1=$inf1";
                  }
                  

                  So what I'm looking to do is replace the number 1 with the region id each time the loop is executed.

                  Thanks again for the help guys, and sorry for being such a n00b!

                    Doing the whole &var=$val thing can be a real pain in the ass. Each var needs its own distinct name. You might do something like this:

                    $counter = 0;
                    foreach ($arr as $val) {
                      echo '&var' . $counter . '=' . urlencode($val);
                      $counter++;
                    }
                    

                    echo 'total=' $counter;

                    Then within Flash you know how many vars there are by checking 'total' first.

                    I would encourage you to use the php function json_encode to serialize your output and then use AS3CoreLib within flash to unserialize it.

                      Hi Sneakyimp,

                      Unfortunately, this won't work for me, as region ids will not necesarrily always be in sequence (for example, the region with id of '2' might be removed at some stage).
                      Before I even get there, I'm wondering what I would replace the number '1' in this line with so that the loop cycles through all regions present in the region table?

                      $query = 'SELECT COUNT(*) FROM infrastructure WHERE Region_Id = 1';

                      I assumed it would be $row_getRegions["Region_Id"], or something like that, but that hasn't worked for me.

                        I think I'm getting close....

                        So now I've got the following.

                        while ($row_getRegions = mysql_fetch_array($getRegions)) {
                        // count instances of each region in each table
                        $query = 'SELECT COUNT(*) FROM `infrastructure` WHERE `Region_Id` = 1';
                        $result = mysql_query($query) or die('Sorry, we could not count the number of results: ' . mysql_error());
                        $inf1 = urlencode(mysql_result($result, 0));
                        echo '&inf' . $row_getRegions["Region_Id"] . '=' . $inf1;
                        }

                        It appears that I'm getting a properly formatted string, but it's starting with the second element of the array, rather than the first. I'm also still stuck as to what I can put in place of the 1 on the first line of the query, so that it is replaced with a region id for each iteration of the loop.
                        Can anyone point me in the right direction?

                          Okay, it's working now EXCEPT that the loop seems to be skipping the first element of the array. Any idea why this might be happening?

                          Code is currently:

                          mysql_select_db($database_ernact, $ernact);
                          $query_getRegions = "SELECT Region_Id FROM regions";
                          $getRegions = mysql_query($query_getRegions, $ernact) or die(mysql_error());
                          $row_getRegions = mysql_fetch_array($getRegions);
                          $totalRows_getRegions = mysql_num_rows($getRegions);
                          
                          while ($row_getRegions = mysql_fetch_array($getRegions)) {
                          // count instances of each region in each table
                          $query = 'SELECT COUNT(*) FROM `infrastructure` WHERE `Region_Id` = ' . $row_getRegions["Region_Id"];
                          $result = mysql_query($query) or die('Sorry, we could not count the number of results: ' . mysql_error());
                          $inf1 = urlencode(mysql_result($result, 0));
                          echo '&inf' . $row_getRegions["Region_Id"] . '=' . $inf1;
                          }
                          

                            your statement "SELECT COUNT()..." tells the database to count all the records in the table 'infrastructure' where Region_Id has the value 1. It sounds like you could be a bit better acquainted with the with how mysql works. Try looking at the examples for [man]mysql_fetch_assoc[/man]. You'll need to change your query to fetch the fields you want from your table rather than just a COUNT() value. Like maybe this:

                            $query = "SELECT * FROM `infrastructure`"; // this tells mysql to fetch all of the fields for all the records
                            $result = mysql_query($query);
                            if (!$result) {
                              echo "Could not successfully run query ($query) from DB: " . mysql_error();
                              exit;
                            }
                            if (mysql_num_rows($result) == 0) {
                                echo "No rows found, nothing to print so am exiting";
                                exit;
                            }
                            while ($row = mysql_fetch_assoc($result)) {
                              print_r($row);
                            }
                            
                            mysql_free_result($result);
                            

                            As for getting that kind of complex data into flash, you could cram each field you fetch into a contiguous array:

                            $arr = array();
                            $counter = 0;
                            while($row = mysql_fetch_assoc($result)) {
                              echo '&Region_Id_' . $counter  . '=' . urlencode($row['Region_Id']);
                              echo '&foo_' . $counter  . '=' . urlencode($row['foo']);
                              echo '&bar_' . $counter  . '=' . urlencode($row['bar']);
                              $counter++;
                            }
                            echo '&total=' . $counter;
                            

                            As you can see, the more fields you are fetching and feeding to flash, the more difficult it gets.

                            I would highly recommend learning the JSON bit. It'll save you a lot of trouble in the long run.

                            Another amazing tool is AMFPHP (http://amfphp.org). This will seem complicated at first, but once you become fluent, PHP to flash communication becomes trivial.

                              j_a_g;10966865 wrote:

                              Okay, it's working now EXCEPT that the loop seems to be skipping the first element of the array. Any idea why this might be happening?

                              The problem is that first time you do this:

                              $row_getRegions = mysql_fetch_array($getRegions);

                              That reads the first row and then does nothing with it before you enter your while loop which is doing most of the work.

                                It sounds like you could be a bit better acquainted with the with how mysql works.

                                DEFINITELY!! I don't usually do this sort of stuff, but this job has evolved into a bit of a monster for me!

                                The reason I'm using SELECT COUNT(*) is because all i need to know is whether there is a record in the table 'infrastructure' relating to any particular region - I don't need any data from any fields at this stage. So each region has a button labelled 'infrastructure', and if there are no records in the table from that region, flash disables the button and greys it out.

                                I've removed that line now and it seems to be working fine, although inevitably once i test it, i'll be proven wrong!

                                Thanks a lot for the help, and cheers for the pointers on what I should be learning next - I wish I had more time to train myself up more, but it seems like I only manage to learn stuff when I need to for a job.

                                Cheers again!

                                  Write a Reply...