views:

448

answers:

1

I am using the jquery plugin simplegallery (http://www.dynamicdrive.com/dynamicindex4/simplegallery.htm). out of the box it works like their documentation says. However I want to use it in a way that gets a dynamic list of random images from the server via an ajax call so that the list is different each time the page is loaded. On the server I have a shell script that finds and moves only the images i need to the proper folder once a day. Then I use an ajax call to a php script that finds the current batch of images and randomly picks n # of images. The php script returns a string simulated below as "var ouput". Originally I tried to get the php script to hand back an array but was unable to do so; so I just built the string in the php script with two different delimiters and sent that back. Then on the client, with javascript, I split the string up and create the images array. However there seems to be a difference in an array created from the string and an array hard coded with values like in the simplgallery example. My code is below: does anyone know what I am doing wrong?

var rand_pix = [ ["../img/blog-pix/l-1600-1200-c80e69aa-bc3f-4264-823b-fcdf2614f87c-300x225.jpg ", "../img/blog-pix/l-1600-1200-c80e69aa-bc3f-4264-823b-fcdf2614f87c-300x225.jpg ", "", "" ],["../img/blog-pix/l-1600-1200-4bebed86-e7e7-4395-8f01-97ab7f57c3c5-300x225.jpg ", "../img/blog-pix/l-1600-1200-4bebed86-e7e7-4395-8f01-97ab7f57c3c5-300x225.jpg ", "", "" ],["../img/blog-pix/l-1600-1200-f4013041-1ec8-4a5d-b261-1bb9bb330a38-300x225.jpg ", "../img/blog-pix/l-1600-1200-f4013041-1ec8-4a5d-b261-1bb9bb330a38-300x225.jpg ", "", "" ] ];  // hard coded like simple gallery example
var output = "../img/blog-pix/l-1600-1200-a27e5201-e371-4a36-9eeb-68eb5f95efe2-300x225.jpg:../img/blog-pix/l-1600-1200-a27e5201-e371-4a36-9eeb-68eb5f95efe2-300x225.jpg:pix_text1:pix_text2|../img/blog-pix/l-1600-1200-0bc2fec3-5e17-4447-8cff-2f0649100d6f-300x225.jpg:../img/blog-pix/l-1600-1200-0bc2fec3-5e17-4447-8cff-2f0649100d6f-300x225.jpg:pix_text1:pix_text2|../img/blog-pix/l-1600-1200-5b7447cd-ea80-4b7e-9f9e-c0cfc52ef040-300x225.jpg:../img/blog-pix/l-1600-1200-5b7447cd-ea80-4b7e-9f9e-c0cfc52ef040-300x225.jpg:pix_text1:pix_text2|../img/blog-pix/l-1600-1200-83f3544b-235a-415f-b988-393e24f52d71-300x225.jpg:../img/blog-pix/l-1600-1200-83f3544b-235a-415f-b988-393e24f52d71-300x225.jpg:pix_text1:pix_text2|../img/blog-pix/l-640-480-5db9395b-0394-4c14-9092-8bb001f3912e-300x225.jpg:../img/blog-pix/l-640-480-5db9395b-0394-4c14-9092-8bb001f3912e-300x225.jpg:pix_text1:pix_text2|"; //simulate what is recieved back from the server
var out_list = output.split("|");
var images = []; // dynamic array of arrays that simplge gallry can use
var img_stuff = []; // temp array holder for the four things each image needs
//alert(out_list.length);
for (var i = 0; i<out_list.length-1; i++){
    var out_list_items = out_list[i].split(":");
    for(var p = 0; p<out_list_items.length; p++){
        var items = out_list_items[p].split(":"); // this array holds each sub array of items for an image that can then be added to list array ..
        img_stuff.push(items);
    }
    images.push(img_stuff);
    img_stuff = [];
}
alert(rand_pix.constructor); -> function Array() { [native code] }
alert(images.constructor); - > function Array() { [native code] }

However the following works:

var mygallery=new simpleGallery({
  wrapperid: "simplegallery1", //ID of main gallery container,
  dimensions: [300, 225], //width/height of gallery in pixels. Should reflect dimensions of the images exactly
  imagearray: rand_pix,
  autoplay: [true, 2500, 2], //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
  persist: false, //remember last viewed slide and recall within same session?
  fadeduration: 4500, //transition duration (milliseconds)
  oninit:function(){ //event that fires when gallery has initialized/ ready to run
  //Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
  },
  onslide:function(curslide, i){ //event that fires after each slide is shown
  //Keyword "this": references current gallery instance
  //curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
  //i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
  }
});

While this does not (it throws a jquery error: Node cannot be inserted at the specified point in the hierarchy" code: "3 [Break on this error] return jQuery(context).find(selector);}e...y.clean([container.innerHTML])[0];}else)

var mygallery=new simpleGallery({
  wrapperid: "simplegallery1", //ID of main gallery container,
  dimensions: [300, 225], //width/height of gallery in pixels. Should reflect dimensions of the images exactly
  imagearray: images,
  autoplay: [true, 2500, 2], //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
  persist: false, //remember last viewed slide and recall within same session?
  fadeduration: 4500, //transition duration (milliseconds)
  oninit:function(){ //event that fires when gallery has initialized/ ready to run
  //Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
  },
  onslide:function(curslide, i){ //event that fires after each slide is shown
  //Keyword "this": references current gallery instance
  //curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
  //i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
  }
});

Besides solving my problem (how to dynamically create an array that simple gallery will take) I am interested in understading what is different about the array I build by parsing the string vs. the hard coded array [ ["blah", "blah", "img1" "img1" ], ["blah2", "blah2", "img2" "img2" ] ].

Any thoughts? suggestions for a better way to do this are also welcome .... Thanks

A: 

Well, I'm not sure that this is what you're looking for and it certainly isn't pretty. But after ten minutes or so of playing around with (the quite impressive) simplegallery demo, I cobbled* this together

<?php

// variables

$dir = "img"; // <- the directory in which your images are stored, I've omitted the closing '/' deliberately.

$images = scandir($dir); // <- builds an array of images from the folder, above.

// foreach, below, removes the "." and ".." relative paths
foreach($images as $key => $value) {

    if ($value == "." || $value == "..") {
     unset($images[$key]);
    }

}
// gets the imagesize for the last (entirely arbitrary) image in the $images variable.
$img_dimensions = getimagesize($dir ."/" . end($images));

     if ($images) {

      foreach($images as $k => $v) {
       if (end($images)) {
       $simplegallery_image_array .= "[\"$dir/$v\", \"\", \"\", \"\"],\n\t";
       }
      }

     }

$simplegallery_image_array = rtrim($simplegallery_image_array,","); // removes the last comma generated from the above foreach.

/*

I know that foreach loops are inefficient, but I couldn't think of an easier way. If there's an easier way, please, let me know =)

*/

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Stu's gallery</title>
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />

    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/simplegallery.js"></script>

<!-- Stuff for simplegallery, from: http://www.dynamicdrive.com/dynamicindex4/simplegallery.htm on 27/08/09 -->

<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>

<style type="text/css">

/*Make sure your page contains a valid doctype at the top*/
#simplegallery1{ //CSS for Simple Gallery Example 1
position: relative; /*keep this intact*/
visibility: hidden; /*keep this intact*/
border: 10px solid darkred;
}

#simplegallery1 .gallerydesctext{ //CSS for description DIV of Example 1 (if defined)
text-align: left;
padding: 2px 5px;
}

</style>

<script type="text/javascript" src="simplegallery.js">

/***********************************************
* Simple Controls Gallery- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
***********************************************/

</script>

<script type="text/javascript">

var mygallery=new simpleGallery({
    wrapperid: "simplegallery1", //ID of main gallery container,
    dimensions: [<?php echo $img_dimensions[0] ;?>, <?php echo $img_dimensions[1] ;?>], /* inserts image dimensions width then height */
    imagearray: [
    <?php
     echo "$simplegallery_image_array";
    ?>
    ],
    autoplay: [true, 1500, 5], //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
    persist: false, //remember last viewed slide and recall within same session?
    fadeduration: 500, //transition duration (milliseconds)
    oninit:function(){ //event that fires when gallery has initialized/ ready to run
     //Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
    },
    onslide:function(curslide, i){ //event that fires after each slide is shown
     //Keyword "this": references current gallery instance
     //curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
     //i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
    }
})

</script>
<!-- End of Simplegallery-->

</head>

<body>
<div class="gallery_demo_unstyled">
<?php
    if ($images) {
?>
    <div id="simplegallery1">
     <ul>
<?php
     foreach($images as $k => $v) {
      $i++;
      echo "\n\t\t<li><img src=\"thumbs/$images[$k]\" /><span class=\"imgNum\">$i</span></li>";
     }
?>


     </ul>
    </div>
<?php
    }

?>
</div>
</div>
</body>

</html>


  • I'd like to say 'hacked' but that would unjustly dignify my terrible, terrible atttempt.
David Thomas
I saw a question on so similar to this answered like you did. my js file is seperate from the php scripts, hence hte ajax call insead. I would like to keep them seperate if possible. if I can't figure out why the js arrays aredifferent i may have to go that way to make it work the way I want it to. I would vote you up but I don't have a high enough reputation at this point ...
hippee-lee
I ended up taking a hybred approach and echoed out the string from php into a js var up top like you were doing. I would still like to know what is different from creating a js array like this var rand_pix = [ ["path/to/image", "path/to/image", "", "" ], ["path/to/image", "path/to/image", "", "" ] ]; and a js array thats created by using push to add elements if anyone knows. Thanks ricebowl.
hippee-lee
@hippee-lee, you're more than welcome; I just wish I could answer your question (in the above comments) =)
David Thomas