Using flashvars

Last updated on
30 April 2025

If you want to use flashvars to create dynamic flash content then this is easily done with flash node. You can set flashvars under Advanced flash node options. Simply write the flashvars string, separating each parameter with an ampersand. To illustrate how you might use flashvars some links to examples are provided below.

Simple example

In this example we are simply passing a very basic string that says first=These&second=Are&third=Flashvars.

Using PHP code

Where things get interesting is if you then enable the PHP input filter. You can then write PHP code in the Flashvars field and use that to generate the flashvars string. Here is another very simple example that uses the following code to generate the string.

<?php
global $user;
$rand = rand(0,100);
$now = date('Y-m-d');
$uid = $user->uid;
echo 'first='.$rand.'&second='.$now.'&third=UID '.$uid;
?>

In this case we generate a random number, the date, and retrieve the user id of the person viewing the page. Bear in mind that anonymous users get served cached pages so the content won't be that random as it will only update when the cache is refreshed.

Complex PHP example

Finally, you can get very complex, as in this example. In this last example PHP is used to query the site database and retrieve all the thumbnail images in a particular taxonomy category. The results of the query are then used to assemble a string that contains both images and hyperlinks to the appropriate nodes. This gets output as flashvars and the flash movie then uses the string to load the images and generate links!

In this case the content updates depending who you are (because the query checks your role via Drupal's security mechanisms), and the content develops as time goes by because more images get added. The advantage here is that the Flash movie doesn't have to process the site's file system to discover the images, and it respects site privacy - users only see content appropriate to their role. This sort of code could equally be used to make a random image cycler on a home page.

<?php 

// Construct sql query to retrieve relevant nids and thumbnails
$sql = "SELECT DISTINCT(n.nid), f.filepath FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {files} f ON n.nid = f.nid WHERE tn.tid = 8 AND n.status = 1 AND n.type = 'image' AND f.filename = 'thumbnail'";

// Run query against the database
$result = db_query(db_rewrite_sql($sql));

// Retrieve results and assemble in two arrays - Flash likes flashvars to be urlencoded
while ($node = db_fetch_object($result)) {
  $images[] = urlencode('/'. file_create_path($node->filepath));
  $urls[] = urlencode('/node/'. $node->nid);
}

// Implode arrays in to strings separated by commas
$images = implode(",", $images);
$urls = implode(",", $urls);

// How many moving pictures in the animation?
$instances = 10;

// Now create a flash macro...

echo "instances=". $instances ."&images=". $images ."&urls=". $urls;
?>

Help improve this page

Page status: Not set

You can: