Topic: Passing Variables to PHP/XML

I read around the forum for at least an hour trying to find a solution to this and although I found vague references, half explanations, and scattered information I did not find any posts that just stated an answer outright. (Although there is probably a post that does, I just couldnt find it)

So, the problem:

Most developers wont have the luxury of being able to simply add an XML file for displaying a gallery.  Coders, like myself, generally need these tools for managing dynamically placed media.  For my part, user uploads are placed in somewhat cryptic folders to prevent people from ever downloading or viewing files outside of their access levels.. such as...

/uploads/main/ax/cm/100023456/thefile.jpg

So the auto.php file, or anything really related to reading a directory structure is out of the question.  Fortunately I do, however, have all of the neccessary information linked and stored in mySQL.

The solution:

So, step 1, set my data source to a dynamic XML generator..

so.addVariable("data_source", "media_data.php");

This is all great, but still I am implying a static, or at least hyper intelligent, source.

That won't do, I need to pass additional information to the data source so that it can find the proper files.

In another post I read the admin talked about you can pass variables, so long as you meant to say 'variable', because any & will get cut off by the actionscript.

So, the second step in the solution is to consolidate the variables you want to pass into a single string.  Those of us who have had similar problems in the pass know all about serialization and base64 encoding, right?

PHP:

$vars2pass["example1"] = "Hello";
$vars2pass["example2"] = "World";
$passString = base64_encode( serialize( $vars2pass ) );

HTML/PHP/JAVSCRIPT:

$html .= so.addVariable(\"data_source\", \"media_data.php?str=$passString\")\n";

media_data.php:

     // Decipher passed data
    $tmp = $_REQUEST["str"];
    $params = unserialize( base64_decode( $tmp ) );

And there you have it.  Simple, effective, and $_SESSION free.

Serialize, encode, append, receive, decode, unserialize, enjoy!

... and if you're still reading, I thought I would provide you one more little fun tip.

If you just happen to be using smarty on your php site you can have this smarty function free of charge.  It may require some modifications for your purposes but here is a head start:

<?php

    function smarty_function_GenMedia($params, &$smarty) {

        global $SkinName;
        global $MediaHasRun;
        
        foreach( $params as $key=>$value ) {
            $paramsLC[ strtolower( $key )] = $value;
        }
        
        $default["width"]             = "100%";
        $default["height"]             = 445;
        $default["id"]                 = rand(0,999999);
        $default["assoc_type"]         = "none";
        $default["assoc_id"]         = 0;
        $default["bgcolor"]         = "#333333";
        $default["timestamp"]         = time();
        $default["title"]             = "Media";

        foreach( $default as $key=>$value ) {

            if( !isset( $paramsLC[$key] ) ) {
            
                $$key = $value;
            
            } else {
            
                $$key = $paramsLC[$key];
            
            }
            
            $vars2pass[ $key ] = $value;
        
        }
        


        // Build the HTML output
            if( !isset( $MediaHasRun ) ) {
                $html  = "<script type=\"text/javascript\" src=\"flash/mediaGallery/flashdetect.js\"></script>\n";
                $MediaHasRun = true;
            } else {
                $html = "";
            }
            
            $passString = base64_encode( serialize( $vars2pass ) );
            
            $html .= "<div id=\"flashcontent_$id\">\n";
            $html .= "            <h1>Oops!</h1>\n";
            $html .= "            <p>It looks like you don't have the latest flash player, which is required to view videos and image galleries on our site.<a href=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" >Click here</a> to go to Adobe download page.\n";
            $html .= "            </p>\n";
            $html .= "</div>\n";
            $html .= "<script type=\"text/javascript\">\n";
            $html .= "\n";
            $html .= "            var so = new SWFObject(\"/flash/mediaGallery/mediaGallery.swf\", \"gallery\", \"$width\", \"$height\", \"6\", \"$bgcolor\");\n";
            $html .= "            so.addVariable(\"data_source\", \"media_data.php?str=$passString\")\n";
            $html .= "            so.addParam(\"wmode\", \"transparent\");\n";
            $html .= "            so.write(\"flashcontent_$id\");\n";
            $html .= "\n";
            $html .= "</script>\n";
        
        return $html;

    }
    
    


?>

After successful implementation adding a media gallery will be quick and easy, like so..

{GenMedia 
width="96%" 
height=325
assoc_type="team_media"
assoc_id=$smarty.get.team
title="Team Media"
}

(Note: I use assoc_type and assoc_id as the basis for my media gathering, it goes without saying that this wont be the case for you)

Last edited by vmadman (2008-09-14 03:18:02)

Re: Passing Variables to PHP/XML

Nice simple function to include a gallery i just set this up with my drupal web content management and using lightbox as well as some other addons. I just can't wait for PHP6 with built in APC for better performance.

Re: Passing Variables to PHP/XML

I may be a beginner, but so glad to start out with the very matter-of fact- best practices presented here.
-------------------------------------
prom dresses | hobo bags