Class output - unique data in each <tr>?

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
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Class output - unique data in each <tr>?

Post by mattcooper »

Following on from my earlier post, how would one go about using the data in each instance of a class to ensure that each row of the table echoed out unique data? The example below echoes out the same data for each row, and I would ordinarily knwo how to do this with procedural code... If I had, for example, the results of a MySQL query and I wanted to echo the data from each row in its own <tr>, I would have the data there within the function, ready to reiterate through and output.

Code: Select all

class create_Table {
	
		var $width;
		var $height;
		var $border;
		var $rows;
		var $content;
		var $data;
		
		function create_Frame ( ) { // create the framework for the table
		
		echo "<table border = ". $this->border ." width = ". $this->width ."% height = ". $this->height .">\n";
			
		$num = 1
		$rows = $this->rows;
			
		while ( $num <= $rows ) {
			
			echo "<tr><td>". $this->content ."</td><td>Function call: ". $this->add_Data ( ) ."</td></tr>\n";
				
			$num++;
				
		}
			
		echo "</table>\n";
			
		} // end of function create_Frame ( )
		
		function add_Data( ) { // Add some data to the cells calling this function
			
			return $this->data;
   		
		}
		
	} // end of class
How do I provide the data for the class (i.e, syntactically), and how should I alter the variable $this->content? I have tried "$this->content[$num] to try to provide a key, and the corresponding "$table->content[1] = SomeData'" to populate it - with no luck.

thanks in advance!
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

Do you mean that you are going to fetch the values of '$content' from a MySQL table, or else the entire set of all variables in the form of rows of data, each correspodning to values for each of your <tr> ?

Either cases, you can use mysql_fetch_row() or mysql_fectch_assoc to extract each row from the databse and print it within the <tr> tag.

Second workaround is use a list of all your $content values. Then use foreach() to extract each value one by one and print the <tr> statement, provided your other values are not going to change.

Hope, I din't understand what you meant in a wrong way :D
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

What I meant was that, eventually, I would like to populate each row with unique data, regardless of the source - but that I wanted to provide the data for the output from the instance of the class, not from within it.

The reason for this is that when I create instances of this class, I may wish to populate it with different combinations of data - therefore precluding the idea of running a single method within the class.

For example:

Code: Select all

function create_Rows ( ) {
		
			$host = |||||||||||;
			$db    = |||||||||||;
			$un    = |||||||||||;
			$pw   = |||||||||||;
			
			// mysql_connect ( $host,$un,$pw );
			mysql_connect ( $host,$un,$pw );
			mysql_select_db ( $db );
			
			$sql = "SELECT * FROM users";
			$result = mysql_query ( $sql ) or die ( "Could not perform query - ". mysql_error () );
			$rows = mysql_num_rows ( $result );
			
			$num = 1;
				
				while ( $num <= $rows ) {
				
					$data = mysql_fetch_assoc ( $result );
			
					echo "<tr><td>". $data[notes] ."</td><td>Function call: ". $this->add_Data ( ) ."</td></tr>\n";
				
					$num++;
				
				}
		} // end of function create_Rows ( )
called within the class itself outputs a set of results presented in a particular way. I may wish to present it in a slightly different way depending on the environment in the page on which the class is called, so would naturally want to have a different function that does this.

How would I instantiate the class and define which method to call for this purpose from outside the class? I would like to use the syntax $this->table_structure = function_x(); or, alternatively, function_y() , or something similar... thereby defining from the calling page which method should be used.

What is the right way to acheive this?
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Maybe you could populate the contents array from outside the class. Let's say an SQL query returns 3 rows, containing "apples", "oranges" and "bananas".

You can create an instance of the object, and assign the mySQL result to the data property:

Code: Select all

//pseudocode
$result = SELECT fruits FROM fruit_table
$rows = Number of rows in $result;

$tab = New create_Table;
$tab->rows = $rows;
$tab->data = $result;
$tab->create_Frame();
You would need to change your create_Frame() and add_Data() functions so that they could deal with an array as the data values: something like:

Code: Select all

function create_Frame ( ) { // create the framework for the table 
     echo "<table border = ". $this->border ." width = ". $this->width ."% height = ". $this->height .">\n"; 
                        
          $num = 0 
          $rows = $this->rows; 
                        
          while ( $num < $rows ) { 
                        
               echo "<tr><td>". $this->content ."</td><td>Function call: ". $this->add_Data ($num) ."</td></tr>\n"; 
                                
               $num++; 
                                
          } 
                        
          echo "</table>\n"; 
                        
} // end of function create_Frame ( ) 

function add_Data($num) { // Add some data to the cells calling this function 
                        
         return $this->data[$num];
              
}
Is this the kind of thing you meant?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Not really, although it is useful code, so thanks.

Lets say that I want two versions of the function: the first outputs 4 <td>'s and populates them, and the second outputs 8. So, within the class I would create the two functions.

How, then, so I create two instances of the same class and use function "1" in the first and function "2" in the second, thereby creating two slightly different versions of the classes?

Hope this is more descriptive of my needs!
Post Reply