Select Menu Class - How's it look?
Posted: Tue Apr 22, 2008 10:37 pm
With a lot of help from arborint, here is my class for building select menu's from a database. It works really well, but is it good code?
Code: Select all
class FormList {
protected $action;
protected $method;
protected $table;
protected $value;
protected $title;
protected $top;
protected $orderby = '';
protected $errorMsg = '';
protected $submitValue;
public function __construct($action, $method) {
$this->action=$action;
$this->method=$method;
$form="<form action=\"{$this->action}\" method=\"{$this->method}\" />\n";
echo $form;
}
public function makeList($table, $value, $title, $top="Select..."){
$this->table=$table;
$this->value=$value;
$this->title=$title;
$this->top=$top;
echo "<select name=\"{$this->table}\">\n";
$result=mysql_query("SELECT {$this->value}, {$this->title} FROM {$this->table}{$this->orderby}") or die (mysql_error());
if(mysql_num_rows($result) < 1){
$this->errorMsg="There was no content found.";
}else{
echo "<option value=\"\">{$this->top}</option>";
while(list($value, $title)=mysql_fetch_array($result)){
echo "<option value=\"{$value}\">{$title}</option>\n";
}
}
echo "</select>\n\n";
}
public function submitForm($submitValue='Submit'){
$this->submitValue=$submitValue;
echo "<input type=\"submit\" value=\"{$this->submitValue}\" />\n";
}
public function closeForm(){
echo "</form>\n\n";
}
}
Code: Select all
## Filter Select Boxes
$filterList=new FormList('index.php?mode=default', 'get');
echo "Office: ";
echo $filterList->makeList('office', 'o_id', 'o_name','All');
echo "Discipline: ";
echo $filterList->makeList('discipline','d_id','d_name', 'All');
echo "Level: ";
echo $filterList->makeList('level','l_id','l_name','All');
echo $filterList->submitForm('Refine Search');
echo $filterList->closeForm();