www.webdeveloper.com
Results 1 to 3 of 3

Thread: using a string as a pointer to an array?

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    Exclamation using a string as a pointer to an array?

    So i've written up the code to do a lightbox esque overlay image gallery. everything is working smooth and fine. The only concern is that I have the following:

    lets say that I have a page that has multiple galleries. Obviously I would have to make each literal array with the gallery data different names, ex:

    Code:
    var _gallery1 = {
    	'images': [
    		{ 'id' : 1, 'src' : "" }
    	]
    }
    
    var _gallery2 = {
    	'images': [
    		{ 'id' : 1, 'src' : "" }
    	]
    }
    or would it be easier to do it multi dimensional?

    Code:
    var gallery_data = {
    	'gallery1': [
    		'images': [
    			{ 'id' : 1, 'src' : "" }
    		]
    	]
    	'gallery2': [
    		'images': [
    			{ 'id' : 1, 'src' : "" }
    		]
    	]
    }
    regardless of which direction I use... my main question is this:

    I have two variables which are the following:

    Code:
    galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null;
    galArray    = _gallery2.images;
    I'm unfamiliar with a way of doing something along the lines of this, and having it actually work:

    Code:
    galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null;
    galArray    = galVariable.images;
    obviously "galVariable" would be undefined... as i'm trying to get "_gallery2.images" but trying to use "galVariable" to point to "_gallery2"

    I'm familiar with solving a problem like this in PHP, not so much javascript. is there a JS function that can be used to ensure that it's using whatever "galVariable" equals to, and then that array's content is? Or is there a different way to go on about this.

    thanks in advance. if anyone needs some more clarification. please feel free to ask, and I'll respond asap.

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    A closure might actually be the easiest for multiple galleries, but it might mean a bit of a rewrite. Basically I mean that each gallery would be an instance of a gallery object, with access to functions like start, stop, random etc., but with a different id each time. I'd also strongly suggest you read this. If you don't get what I'm talking about, ask, because I don't think I've explained that well.

    As for your second question, I don't understand what you are getting at to be honest.

  3. #3
    Join Date
    Mar 2010
    Posts
    128
    This is kinda strange, I don't quite understand why your location would need to be set to something in order to display a gallery. In order to have seperate variables based off of your location, your location would need to be something like

    tester.html#set1=X&photo1=X&set2=X&photo2=X&set3=X&photo3=X

    and then you would do something like

    galVariable1 = (i = location.hash.match(/#set1=(.+)&photo1=(\d+)/)) ? i[1] : null;
    galVariable2 = (i = location.hash.match(/#set2=(.+)&photo2=(\d+)/)) ? i[1] : null;

    and so one... However, this doesn't seem like a proper method. Maybe explaining the purpose of the location would help make sense of this. Also, how your gallery system works will determine the format in which you should store your gallery data & settings. Personally, Declan made a very good suggestion, instantiating the gallery would be your most versatile and unobtrusive approach, but like already mentioned it would most likely require a lot of modification.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles