Jump to content

dynamic array name


Rygar48

Recommended Posts

Hey everyone!  I'm currently working on a website for a local Theater company, and I need help with our Past Show archive.  I'm using javascript so that the page is split up into the year, and when the user click on the year, it slides down to show the different shows, and when they click on a show, it slides down and shows various images from that show.

 

All of that seems to be working.  My one problem:  I am currently using a different php file for each show, but I would like to use only one.  I've pseudo-successfully done this, but my one issue is that the array I create (that holds the location of each image) has the same name ($files[] ) so although the code knows to look in the right folder, it's using the same array, and thus if a show has pictures, and then the next show is clicked on, the webpage expects to find the pictures from the previous show.

 

My question:

 

if I'm passing a different variable depending on which show is "active" (so let's say we have the options: macbeth, original, dinner, and ayli), is there a way to change the name of the array to reflect the variable that's passed to the php file?

 

i.e.

Variables (found in html code, these are the $id in the php code):

Macbeth                User clicks on macbeth, the php file is called, macbeth [] is created

original                  User click on original, the php file is called, original [] is created, etc

dinner

ayli

 

Here's my code in case you need to see it:

	<?php
                              function detect ($id)
                           {
                                    switch ($id) {
    	                           case 'pinter':
	        $pinter = array ();
        		        return $pinter;
        			break;
    	                           case 'twelfth':
	        $twelfth = array ();
        		        return $twelfth;
        			break;
    	                           case 'original':
	        $original = array ();
        		        return $original;
        			break;
    	                           case 'ayli':
	        $ayli = array ();
        		        return $ayli;
        			break;
    	                           case 'dinner':
	        $dinner = array ();
        		        return $dinner;
        			break;
    	                           case 'macbeth':
	        $macbeth = array ();
        		        return $macbeth;
        			break;
		}

	/* settings */
	$image_dir = 'images/archive/';
                                    $image_dir .= $id;
                                    $image_dir .= "/";
	$per_column = 4;


	/* step one:  read directory, make array of files */
	if ($handle = opendir($image_dir)) {
		while (false !== ($file = readdir($handle))) 
		{
			if ($file != '.' && $file != '..') 
			{
				if(strstr($file,'-thumb'))
				{
					detect ($id) = $file;
				}
			}
		}
		closedir($handle);
	}

	/* step two: loop through, format gallery */
	if(count(detect ($id)))
	{
		foreach(detect ($id) as $file)
		{
			$count++;
			echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
			if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
		}
	}
	else
	{
		echo '<p>There are no images in this gallery.</p>';
	}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/184199-dynamic-array-name/
Share on other sites

Where is the closing brace for the function?

 

function detect ($id)
{
    switch ($id)
    {
        case 'pinter':
            $pinter = array ();
            return $pinter;
        break;
        case 'twelfth':
            $twelfth = array ();
            return $twelfth;
        break;
        case 'original':
            $original = array ();
            return $original;
        break;
        case 'ayli':
            $ayli = array ();
            return $ayli;
        break;
        case 'dinner':
            $dinner = array ();
            return $dinner;
        break;
        case 'macbeth':
            $macbeth = array ();
            return $macbeth;
        break;
    }
}

 

If that's where the closing brace should be, your function may as well be:

 


function detect()
{
   return array();
}

Link to comment
https://forums.phpfreaks.com/topic/184199-dynamic-array-name/#findComment-972485
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.