Hi all, first post.
I have been trying to create a dynamic gallery to fit in with my existing site and had originally used pure javascript to code the gallery, but I'm getting fed up with having to continually edit the js file, so I thought php would be easiest, I have reworked a script I found to search my diectories, but I can't figure out how to output as a javascript array,
I can get it to list all items as:
/foldername[0] = "imagename.jpg"
but I need it to be formatted as:
array((foldername(0))
so I can put the php in my jscript file instead of a complete rewrite, I'm new to both jscript and php so any help would be much appreciated.

my php code:
<?php

function recursive_dir($root,$path_ext = "",$check_ext = "",$new_path_ext = "") {
$dh = opendir($root.$path_ext.$new_path_ext);
while(false !== ($entry=readdir($dh))) {
if($entry != "." && $entry != ".." && is_dir($root.$path_ext.$new_path_ext."/".$entry)) {
$prev_path_ext = $new_path_ext;
$new_path_ext .= "/".$entry;
recursive_dir($root,$path_ext,$check_ext,$new_path_ext);
$new_path_ext = $prev_path_ext;
}

elseif($entry != "." && $entry != ".." && eregi("($check_ext)$",$entry)) {
$curimage=0;
if($dh = opendir($root.$path_ext.$new_path_ext)) {
while(false !== ($entry = readdir($dh))){
//Output it as a JavaScript array element
echo $new_path_ext;
echo '['.$curimage.']';
echo '='.$entry .'<br />';
$curimage++;
}
}
}
}
closedir($dh);
}

recursive_dir($root_directory,$path_extension,$check_extension);
echo ' need output to look like this:<br />arrayname(foldername(number of items))';
?>

    essentially you need to write the script out in php similar to:

    echo '<script language="JavaScript" type="text/javascript">';
    while(false !== ($entry = readdir($dh))){
    //Output it as a JavaScript array element

    // echo the proper text for each javascript element

    echo $new_path_ext;
    echo '['.$curimage.']';
    echo '='.$entry .'<br />';
    $curimage++;

    ...... at the end

    echo '</script>';

      thanks for the reply, but a quick Q, should I get any output from the php file if I've done it right, or do I have to call it from a html page to get the array?

        You should see the java script in the source code of the html page, or if you run the php file from a command prompt you should see the code that would makeup the html page. This is a sample of script created by a similar php page as yours and part of the php that creates it
        :

        PHP CODE

        	while ($make_row = mysql_fetch_array($make_result)) {
        			// Get List of Models
        			$modelarray_counter = 0;
        			$sql =	"select * from models " .
        					"where make = '" . $make_row['make_ID'] . "' " .
        					"order by model_ID";
        			$model_result = mysql_query($sql,$modelconn);
        			while ($model_row = mysql_fetch_array($model_result)) {
        				$model_ID = trim($model_row['model_ID']);
        				$model = trim(strtoupper($model_row['model']));
        				$make = $model_row['make'];
        				$premium_miles = $model_row['premium_miles'];
        				print 'group[' . $makearray_counter . '][' . $modelarray_counter . ']=new Option("' . $model . '","' . $model_ID . '")';
        				print "\r\n";
        				$modelarray_counter ++;
        			}
        			$makearray_counter ++;
        		}

        HTML PAGE

        <script language ="Javascript">
        <!--
        var temp=document.MainForm.vehicle_model

        function redirect(x){
        for (m=temp.options.length-1;m>0;m--)
        temp.options[m]=null
        for (i=0;i<group[x].length;i++){
        temp.options=new Option(group[x].text,group[x].value)
        }
        temp.options[0].selected=true
        // Added to rescore vehicle
        score_entry(redisplay,"vehicle_model")
        // End rescore
        }


        // Create Model Dromp down Array to be used with the Make Drop Down List above.
        // Each Make has a corresponding list of Models
        var groups=document.MainForm.vehicle_make.options.length
        var group=new Array(groups)
        for (i=0; i<groups; i++)
        group=new Array()

        group[1][0]=new Option("MDX","1")
        group[1][1]=new Option("SLX","2")
        group[1][2]=new Option("CL","3")
        group[1][3]=new Option("INTEGRA","4")

          Thanks again, this'll probably feel like I'm asking you to hold my hand through it, but do I need to have the php script somewhere in my html page, thus making it a php page or can I link to it externally as I would with a js file?

          I really am totally lost with this whole server side, client side thing sorry.

            Either way. Easier to include it in the html page at the point you want the script. Alternatively you will have to write the js script as a file from the php page, i.e. fopen("SOMEJSNAME.js",w); use the query to build the code and fclose it all. Then it would be a dynamic js file you can call in an html page. This has other problems though because the js file would bre re-written each time tha page is called, and providing the opportunity for it to be called by a previous visitor while the current visitor is re-writing the js page. Better as a php page creating the html page unless there is a specific reason to do it the other way.

              Ok, this looks like it'll have to be a rewrite in php, which will be a killer job, maybe you could have a look at my page and tell me what you think would be the best way to do it for my site, and then I can figure out where to go from there. The link is to a test page but has the exact setup I use for my js. The issue I have is that it took me so long to get exactly what I wanted and I don't want all the time I spent on it to have been for nothing, hopefully you know what I mean 🙂

              http://h1.ripway.com/4evernomad/mami/index.html

                HTML pages can not render PHP code. You must have hosting that supports PHP and the extension of your page must be .php

                  My host does support php and I understand that a php page must have php suffix, but is it possible to put my php code into my external jscript, as my html page calls the js functions into <div> tags and doesn't need the code in the page as in my link above.

                    Write a Reply...