Page 1 of 1

Variable inside a variable

Posted: Mon Oct 16, 2006 2:50 pm
by cczernia
I have a long string of code that I think can be turned into a function. The only difference is a different variable inside each loop. I have an if statement determine which loop to use. I want to be able to change out the array for each loop but can't figure out how to store a variable inside a variable. Below is the code.

Code: Select all

$clothingretail[0] = "Felicitys Lingerie";
$clothingretail[1] = "K-5 Boardshop";
$clothingretail[2] = "Sun Diego";

$sportsretail[0] = "Upper Deck";
$sportsretail[1] = "LA Dodgers";
$sportsretail[2] = "Phoenix Suns";
$sportsretail[3] = "Asylum Skate";

$specialtyretail[0] = "Bosa Bella";
$specialtyretail[1] = "West Coast Corvette";

$museumsretail[0] = "Reagan Library";
$museumsretail[1] = "Air Force 1";
$museumsretail[2] = "Titanic";


			if ($pagecat == "clothingretail") {
			  print 'Clothing Retail
			  <table cellspacing="3" cellpadding="3">
  				<tr>';
			  $total = count($clothingretail);
			  for ($i=0; $i<$total; $i++) {
				
				echo '<td align="center"><a href="'. str_replace(" ", "", $clothingretail[$i]).'.php"><img src="images/thumbs/'. str_replace(" ", "", $clothingretail[$i]) . '.jpg" border="'. addborder($i, $pagenum, $navcat) .'"><br><a href="'. str_replace(" ", "", $clothingretail[$i]).'.php">'. $clothingretail[$i].'</a></td>';
				 
			  }
			  print '</tr></table>';
			}
			?>
			
					   
            <?php
			if ($pagecat == "sportsretail") {
			  print 'Sports Retail
			  <table cellspacing="3" cellpadding="3">
  				<tr>';
			  $total = count($sportsretail);
			  for ($i=0; $i<$total; $i++) {
			  if ($i==4) {
			   echo '</tr><tr>';
			   }
				echo '<td align="center"><a href="'. str_replace(" ", "", $sportsretail[$i]).'.php"><img src="images/thumbs/'. str_replace(" ", "", $sportsretail[$i]) . '.jpg" border="'. addborder($i, $pagenum, $navcat) .'"><br><a href="'. str_replace(" ", "", $sportsretail[$i]).'.php">'. $sportsretail[$i].'</a></td>';
				
			  }
			  print '</tr></table>';
			}
			?>
		 
		 
		    
            <?php
			if ($pagecat == "specialtyretail") {
			  print 'Specialty Retail
			  <table cellspacing="3" cellpadding="3">
  				<tr>';
			  $total = count($specialtyretail);
			  for ($i=0; $i<$total; $i++) {
			    echo '<td align="center"><a href="'. str_replace(" ", "", $specialtyretail[$i]).'.php"><img src="images/thumbs/'. str_replace(" ", "", $specialtyretail[$i]) . '.jpg" border="'. addborder($i, $pagenum, $navcat) .'"></a><br><a href="'. str_replace(" ", "", $specialtyretail[$i]).'.php">'. $specialtyretail[$i].'</a></td>';
			
			
			
				
			  }
			  print '</tr></table>';
			}
			?>
		  
		  		   
            <?php
			if ($pagecat == "museumsretail") {
			  print 'Museum/Exhibition Retail
			  <table cellspacing="3" cellpadding="3">
  				<tr>';
			  $total = count($museumsretail);
			  for ($i=0; $i<$total; $i++) {
				
				echo '<td align="center"><a href="'. str_replace(" ", "", $museumsretail[$i]).'.php"><img src="images/thumbs/'. str_replace(" ", "", $museumsretail[$i]) . '.jpg" border="'. addborder($i, $pagenum, $navcat) .'"><br><a href="'. str_replace(" ", "", $museumsretail[$i]).'.php">'. $museumsretail[$i].'</a></td>';
			  }
			  print '</tr></table>';
			}
			?>

Posted: Mon Oct 16, 2006 2:54 pm
by hawleyjr
I didn't go through your code but based off of your subject title this may help: http://in.php.net/manual/en/language.va ... riable.php

Posted: Mon Oct 16, 2006 3:03 pm
by feyd
Seems like an opportunity to create a function. :)

Posted: Mon Oct 16, 2006 6:59 pm
by Cameri
If all you have is a huge chunk of php, you don't have to separate it with <?php and ?>. Just put one at the beginning and one at the end.

For the $pagecat variable, I would have reused the same code, and changed only the things that are different in the for loops for example, that way the script will be shorter, which makes it easier when you have to fix something in each $pagecat. Example: using a switch statement for that.

I find more confortable this way of making arrays:

Code: Select all

$clothingretail = array("Felicitys Lingerie", "K-5 Boardshop", "Sun Diego");
or you can split it in many lines to make it more easier to look at:

Code: Select all

$clothingretail = array(
   "Felicitys Lingerie",
   "K-5 Boardshop",
   "Sun Diego"
);
but still I wouldnt be satisfied, why not just make an associative array of arrays:

Code: Select all

$retails = (
  "clothingretail" => array("Felicitys Lingerie","K-5 Boardshop","Sun Diego"),
  "sportsretail" => array("Upper Deck","LA Dodgers","Phoenix Suns","Asylum Skate"),
  ...
);
And then just check $pagecat against this array like this:

Code: Select all

if (array_key_exists($pagecat, $retails)) {
 // retail found, $retails[$pagecat] is a valid $pagecat
}
or a faster alternative (array_key_exists() is slower than isset()):

Code: Select all

if (isset($retails[$pagecat]) {
 // retail found, $retails[$pagecat] is a valid $pagecat
}

Posted: Tue Oct 17, 2006 10:35 am
by cczernia
Thanks a ton. I've been having trouble understanding object oriented elements but that array seems to make sense. I'll try implementing it.

Posted: Tue Oct 17, 2006 12:03 pm
by cczernia

Code: Select all

$retails = (
  "clothingretail" => array("Felicitys Lingerie","K-5 Boardshop","Sun Diego"),
  "sportsretail" => array("Upper Deck","LA Dodgers","Phoenix Suns","Asylum Skate"),
  ...
);
I tried implementing the statment above and I got the error:

Code: Select all

Parse error: syntax error, unexpected T_DOUBLE_ARROW
Unfortunatly, I have no idea how to debug it. Anyone have any ideas. Thanks.[/quote]

Posted: Tue Oct 17, 2006 12:07 pm
by feyd
You forgot the keyword "array" after the first equal sign in that snippet.