[SOLVED] Dynamic variables in a class

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

[SOLVED] Dynamic variables in a class

Post by hawleyjr »

I have an UGLY tabel from a flat file that has about 120 fields 40 of which are labeled feature1,feature2,feature3....feature40.

It gets worse...

In each feature field there is a string that contains sub features such as:

01,16,32,2

This really is a mess but it's all I have to work with.

For each feature I have to do another query to get the sub features. The following is a class (scaled down for this forum) that gets all of the features. I would like to use ${"variableName$x"} in a loop so I don't have to write 40 functions for the same lookup.

My class:

Code: Select all

<?php
class FeaturesGetAll{
   
   
 	var $a_id=array();
 	var $a_feature1=array();
 	var $a_feature2=array();
 	var $a_feature3=array();
 	var $a_feature4=array();
 	var $a_feature5=array();
 	var $a_feature6=array();
 	var $a_feature7=array();
 	var $a_feature8=array();
 	var $a_feature9=array();
   
 	function FeaturesGetAll(){
   
 	 	$qry = "SELECT id, feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9 FROM table_name";

 	 	$result = @mysql_query($qry);
   
  
 	 	while($row = mysql_fetch_array($result))
 	 	{
 	 	 	$this->a_id[] = $row['id'];
 	 	 	$this->a_feature1[] = $row['feature1'];
 	 	 	$this->a_feature2[] = $row['feature2'];
 	 	 	$this->a_feature3[] = $row['feature3'];
 	 	 	$this->a_feature4[] = $row['feature4'];
 	 	 	$this->a_feature5[] = $row['feature5'];
 	 	 	$this->a_feature6[] = $row['feature6'];
 	 	 	$this->a_feature7[] = $row['feature7'];
 	 	 	$this->a_feature8[] = $row['feature8'];
 	 	 	$this->a_feature9[] = $row['feature9'];

 	 	}//END WHILE
 	 	
 	 	for($x=1;$x<=9;$x++){
 	 	
 	 		$feat = ${"this->a_feature$x"};
 	 		echo '<HR><PRE>'; print_r($feat); echo '</PRE>';;//THIS DOESN'T WORK 
 	 	
 	 	}

   
 	 	function getA_Id(){ return $this->a_id; }
 	 	function getA_Feature1(){ return $this->a_feature1; }
 	 	function getA_Feature2(){ return $this->a_feature2; }
 	 	function getA_Feature3(){ return $this->a_feature3; }
 	 	function getA_Feature4(){ return $this->a_feature4; }
 	 	function getA_Feature5(){ return $this->a_feature5; }
 	 	function getA_Feature6(){ return $this->a_feature6; }
 	 	function getA_Feature7(){ return $this->a_feature7; }
 	 	function getA_Feature8(){ return $this->a_feature8; }
 	 	function getA_Feature9(){ return $this->a_feature9; }
} 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$feat = "a_feature$x";
print_r($this->$feat);
I think
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Works,

Thanks Feyd, once again you rock!
Post Reply