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
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » 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();
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Wed Apr 23, 2008 12:48 am
Looks good. You might want to think about splitting it into three classes. A Form class, a Select class and a Submit class. The you could add form input objects to the form object.
(#10850)
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Wed Apr 23, 2008 1:00 am
I'm not sure what you mean by "Good code". But I tend to not like limiting pieces of code for only certain situations. (Yours is only for things in the database).
Also echoing in a class for me is a no no. I like having it return the string then I can echo/do what I need with it.
Here is a chunk of one of my html "helpers".
Code: Select all
/**
* Builds HTML Select List
* @param string $Name name of list
* @param array $Values array of items in list
* @param boolean $Remember to select if post isset
* @param boolean $UseValues to use array keys as option value, or not
* @return string
*/
function select($Name, $Values = array(), $Remember=true, $UseValues=false){
$Cur = isset($_POST[$Name])? $_POST[$Name] : NULL; // Current Value
$Html = '<select name="' . $Name . '">' . PHP_EOL;
if(!is_array($Values)) $Values = (array)$Values;
foreach($Values as $ID=>$Value){
$Select = $Cur==$Value && $Remember? ' SELECTED' : ''; // Is it set?
$Val = $UseValues? $ID : $Value; // Are we using array keys?
$Html.= '<option' . $Select . ' value="' . $Val . '">' . $Value . '</option>' . PHP_EOL;
}
$Html.='</select>' . PHP_EOL;
return $Html;
}
Example usage.. (In a View)
Code: Select all
<form name="posts" method="post">
<div class="editing hold">
<div class="hr">
<div class="left title">Manage Posts</div>
<div class="right">
<?php echo select('categorys" class="test',$Categorys); // Little Trick here for the name ?>
<?php echo select('status',array('Show All','Published','Drafts','Private')); ?>
<input type="submit" name="submit" value="Filter"/>
</div>
<div class="clear"></div>
</div>
</div>
</form>
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Wed Apr 23, 2008 8:21 am
arborint, like so? Do you need a constructor for each class?
Code: Select all
<?php
class NewForm{
protected $action;
protected $method;
function __construct($action, $method) {
$this->action=$action;
$this->method=$method;
}
public function openForm(){
$html="<form action=\"{$this->action}\" method=\"{$this->method}\" />\n";
return $html;
}
public function closeForm(){
$close="</form>\n\n";
return $close;
}
}
class FormList{
protected $table;
protected $value;
protected $title;
protected $top;
protected $errorMsg = '';
public function makeList($table, $value, $title, $top="Select..."){
$this->table=$table;
$this->value=$value;
$this->title=$title;
$this->top=$top;
$html="<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{
$html.="<option value=\"\">{$this->top}</option>";
while(list($value, $title)=mysql_fetch_array($result)){
$html.= "<option value=\"{$value}\">{$title}</option>\n";
}
}
$html.="</select>\n\n";
return $html;
}
}
class FormBox {
protected $inputType;
protected $inputName;
protected $inputSize;
protected $inputValue;
public function inputBox($inputType, $inputName, $inputSize="", $inputValue=""){
$this->inputType=$inputType;
$this->inputName=$inputName;
$this->inputValue=$inputValue;
$this->inputSize=$inputSize;
$html="<input type=\"{$this->inputType}\" name=\"{$this->inputName}\" value=\"{$this->inputValue}\" size=\"{$this->inputSize}\" />\n";
return $html;
}
}
class FormButtons{
protected $submitValue;
$this->submitValue=$submitValue;
public function submitForm($submitValue='Submit'){
$this->submitValue=$submitValue;
$html="<input type=\"submit\" value=\"{$this->submitValue}\" />\n";
return $html;
}
}
?>