Variable inside a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cczernia
Forum Newbie
Posts: 20
Joined: Tue May 16, 2006 2:00 pm
Location: San Diego, CA

Variable inside a variable

Post 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>';
			}
			?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Seems like an opportunity to create a function. :)
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post 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
}
User avatar
cczernia
Forum Newbie
Posts: 20
Joined: Tue May 16, 2006 2:00 pm
Location: San Diego, CA

Post 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.
User avatar
cczernia
Forum Newbie
Posts: 20
Joined: Tue May 16, 2006 2:00 pm
Location: San Diego, CA

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You forgot the keyword "array" after the first equal sign in that snippet.
Post Reply