I'm relatively now to OOP, and I'm trying to write a class that I can pass values to which whill display a listing table of results from a MySQL table.
Another thing that I'd like to do is pass an array of values to the class, and then when I'm displaying all the results, enclose the values I pull from MySQL inside of that I passed in another function. Since the array titles and values will be different every time, I'd like to declare them as globals. Here's part of my class:
class listingTable {
// defining the variables that will be used to build the form and table
var $row_values = array();
function defineVars($define_row_values) {
if($define_row_values != "") $this -> row_values = $define_row_values;
}
function declareVars($var_array) {
while($each = each($var_array)) {
global ${$each["key"]};
${$each["key"]} = $each["value"];
}
}
function buildFormAndTable() {
echo "\n<a name=\"results\"></a>\n";
echo "<table cellspacing=\"0\" cellpadding=\"5\" class=\"listing_table\">\n";
// printing the table rows
while($value_array = mysql_fetch_array($this -> result)) {
if(isset($bgc) && $bgc == "bg1") {
$bgc = "bg2";
} else {
$bgc = "bg1";
}
echo "<tr class=\"".$bgc."\">\n";
echo "\t<td nowrap=\"nowrap\">\n";
if($value_array[$this -> row_values[$each["key"]]["field_name"]] != "") {
if($this -> row_values[$each["key"]]["type"] == "result_value") {
if(isset($this -> row_values[$each["key"]]["format"]) && $this -> row_values[$each["key"]]["format"] == "ucwords") {
echo htmlentities(ucwords($value_array[$this -> row_values[$each["key"]]["field_name"]]));
} elseif(isset($this -> row_values[$each["key"]]["array_val"])) {
print_r(${$this -> row_values[$each["key"]]["array_val"]});
//echo htmlentities(${$this -> row_values[$each["key"]]["array_val"]}[$value_array[$this -> row_values[$each["key"]]["field_name"]]]);
} else {
echo htmlentities($value_array[$this -> row_values[$each["key"]]["field_name"]]);
}
}
} else {
echo " ";
}
echo "</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
}
}
And then here's what I'm passing to it, w/ my $item_category_array defined earlier on the page:
$listing_table = new listingTable();
$table_header["item_category"] = "Category";
$row_values["item_category"]["field_name"] = "item_category";
$row_values["item_category"]["type"] = "result_value";
$row_values["item_category"]["array_val"] = "item_category_array";
$listing_table -> defineVars($row_values);
$listing_table -> declareVars(array("item_category_array" => $item_category_array));
$listing_table -> buildFormAndTable();
So why isn't that available as a global variable in the buildFormAndTable functions? Any ideas?