Adding Data to Flash


Now let's get to some PostgreSQL action. The target is to create a Flash movie that contains the result of a simple query. The idea is simple, but it allows you to build professional and very impressive Web applications.

We have compiled a small table containing four records that we will use in our sample program:

 myflash=#  SELECT * FROM persons;  name     profession ----------+---------------  Alan Cox  kernel hacker  Etschi    foodstylist  Hans      author  Falco     musician (4 rows) 

The next application selects all records from this table and makes the lines move from the upper side of the window to the bottom. The result already looks quite good and the code is, including all comments, only about 70 lines long. This example shows how easy it is to build Flash applications with the help of the Ming interface and PostgreSQL.

Here is the code:

 <?php   dl('php_ming.so');   # connecting to the database   $dbh=pg_connect("dbname=myflash user=hs");   if (!$dbh)   {     echo ("ERROR: connecting failed");     exit;   }   # selecting data from the PostgreSQL   $result=pg_exec($dbh, "SELECT * FROM persons");   if (!$result)   {     echo ("ERROR: cannot failed");     exit;   }   # creating font and movie object   $f = new SWFFont("test.fdb");   $m = new SWFMovie();   $m->setDimension(320, 240);   # computing number of lines   $num_res=pg_numrows($result);   # processing the data line by line   for ($i=0;$i<$num_res;$i++)   {     $line=pg_fetch_row($result,$i);     # creating a new object     $obj[$i] = new SWFText();     # setting the object's properties     $obj[$i]->setFont($f);     $obj[$i]->setColor(0xff, 0, 0);     $obj[$i]->setHeight(15);     $string="$line[0] 's profession is $line[1]";     $obj[$i]->addString($string);     # adding the object to the scenery     $inst[$i] = $m->add($obj[$i]);     # assigning the starting position     $ypos[$i]=0-$i*30;     $xpos[$i]=160-$obj[$i]->getWidth($string)/2;     # moving the object to the starting position     $inst[$i]->moveTo($xpos[$i], $ypos[$i]);   }   # repositioning 101 times   for($count=0; $count<=100; $count++)   {     # processing the loop for every object     for($j=0; $j<$num_res; $j++)     {       # moving the object and finding the new position       $inst[$j]->moveTo($xpos[$j], $ypos[$j]);       $ypos[$j]=$ypos[$j]+2;       $ypos[$j]=$ypos[$j]%200;     }     $m->nextFrame();   }   # displaying results   header('Content-type: application/x-shockwave-flash');   $m->output(); ?> 

Figure 14.4 shows the result.

Figure 14.4. Displaying the content of the database.

graphics/14fig04.gif

Let's have a closer look at the code. First we connect to the database ”as we showed in the earlier section, "PHP and Flash Interfaces" ”and select the data from the table. Then we create the Flash movie and compute the number of lines returned by the query. For every line returned by the query, we create a text object that will be stored in an array called $obj[] . Every object is assigned to the desired properties and added to the scenery. In the next step, we compute the current position of the objects and place the object in the correct position.

Note

The objects can be assigned to places that are outside the visual area (which means that the coordinates can be negative). This is a very comfortable feature, because it makes programming Flash applications even easier.


After we compile the various objects, we start animating the text. Every object is assigned to a new position 101 times. The position of an object is stored in the arrays $xpos and $ypos . The position of the data in those two arrays matches the order the data is returned by the database. Every time an object is moved, we calculate the new position of the object.

After displaying the set of objects, we go to the next frame. If the generation of the movie is ready, we display it in the browser.

Flash also supports features such as transparent objects. The following is a slightly modified version of the program we have just discussed. This time, we display only the first column of the result and use transparent objects. We also change the speed of the video. Here's the code:

 <?php   dl('php_ming.so');   # connecting to the database   $dbh=pg_connect("dbname=myflash user=hs");   if (!$dbh)   {     echo ("ERROR: connecting failed");     exit;   }   # selecting data from the PostgreSQL   $result=pg_exec($dbh, "SELECT * FROM persons");   if (!$result)   {     echo ("ERROR: cannot failed");     exit;   }   # creating font and movie object   $f = new SWFFont("test.fdb");   $m = new SWFMovie();   $m->setDimension(320, 240);   $m->setRate(24.0);   # computing number of lines   $num_res=pg_numrows($result);   # processing the data line by line   for ($i=0; $i<$num_res; $i++)   {     $line=pg_fetch_row($result,$i);     # creating a new object     $obj[$i] = new SWFText();     # setting the object's properties     $obj[$i]->setFont($f);     $obj[$i]->setColor(0, $i*30, $i*30, (($i+1)*20)%100 );     $obj[$i]->setHeight(60);     $string="$line[0]";     $obj[$i]->addString($string);     # adding the object to the scenery     $inst[$i] = $m->add($obj[$i]);     # assigning the starting position     $ypos[$i]=0-$i*30;     $xpos[$i]=160-$obj[$i]->getWidth($string)/2;     # moving the object to the starting position     $inst[$i]->moveTo($xpos[$i], $ypos[$i]);   }   # repositioning 101 times   for($count=0; $count<=100; $count++)   {     # processing the loop for every object     for($j=0; $j<$num_res; $j++)     {       # moving the object and finding the new position       $inst[$j]->moveTo($xpos[$j], $ypos[$j]);       $ypos[$j]=($ypos[$j]+2)%240;     }     $m->nextFrame();   }   # displaying results   header('Content-type: application/x-shockwave-flash');   $m->output(); ?> 

Using transparent objects with Flash is an easy task. Up to now, we have called the setColor method with only three parameters. Using a fourth parameter is the key to transparent objects. In the code, the line

 $obj[$i]->setColor(0, $i*30, $i*30, (($i+1)*20)%100 ); 

sets the level of transparency. Because we do not want all objects to have the same color, we tell PHP and Flash to make the color of an object depend on the position ( $i ) of an object in the result. As we mentioned earlier, we also want to set the speed of the video. Setting the speed means changing the frame rate of the video. This can be done with the help of the setRate method. In the example, we want 24 frames per second to be displayed - this is actually a little faster than the default setting.

The movie created by this code is shown in Figure 14.5.

Figure 14.5. An example using transparent objects.

graphics/14fig05.gif



PostgreSQL Developer's Handbook2001
PostgreSQL Developer's Handbook2001
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net