Jump to content

Recommended Posts

This is my first dive into writing a class (i hope this is considered a class *lol*), and i'm not trying to re-invent the wheel here - just figured a select list would be an overcoming thing to start fiddeling with to get a class up and running.

I don't know if this is done in fairly good practice or wwwway off - it works though.
Kill me with improvements/changes/possible problems on such class (green on when to use var,private,public etc...)

[code]

<?php

class select
{
var $options = array();

function selected($value="")
{
$this->selected = $value;
}

function add($key,$value=false)
{
if($value == false) $value = ucfirst($key);
$this->options[$key] = $value;
}

function name($name=false)
{
if($name==false) $this->name = ""; else $this->name = " name=\"$name\"";
}

function id($id=false)
{
if($id==false) $this->id = ""; else $this->id = " id=\"$id\"";
}

function write()
{
$liste = "";
foreach($this->options as $key => $value)
{
if($key==$this->selected) $selected = " selected=\"selected\""; else $selected = "";
$liste .= "<option value=\"".$key."\"".$selected.">".$value."</option>\r\n";
}
$head = "<select".$this->name.$this->id.">\r\n";
$foot = "</select>\r\n";
echo $head.$liste.$foot;
}
}

$select = new select();
$select->selected("1");
$select->name("my_select_name");
$select->id("my_select_id");
$select->add("choose");
$select->add("1","Email");
$select->add("2","Phone");
$select->add("3","Letter");
$select->write();

?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/26221-basics-ok-or-not-in-this-class/
Share on other sites

Please consider this alternative:

[code]<?php
class Select
{
var $options = array();
var $id;
var $selected;
var $name;

function add($options){
foreach($options as $key=>$text){
if(empty($text)){
$text = ucfirst($key);
}
$this->options[$key] = $text;
}

}
function render(){
foreach($this->options as $key => $text){
$selected = $key == $this->selected ? " selected=\"selected\"": '';
$optionHtmlArr[] = "\t<option value=\"".$key."\"".$selected.">".$text."</option>";

}
if(count($optionHtmlArr)){
echo "<select id ='".$this->id."' name='".$this->name."'>\n".implode("\n",$optionHtmlArr)."\n</select>";
}
}
}
$select = &new Select();
$select->selected = "1";
$select->name = "my_select_name";
$select->id = "my_select_id";
$select->add(array('choose'=>'','1'=>'Email','2'=>'Phone','3'=>'Letter'));
$select->render();
?>[/code]

Mayor differences:

1) It properly initializes properties.
2) It doesn't use methods to assign properties that should be publicly available.
3) It seperates business and presentation logic (no html except in "render").

LOL, i thought you were on php4..

I missed this comment:
[quote]green on when to use var,private,public etc..[/quote]

And it's also in your sig. Sorry, modification:

[code]
<?php
class Select
{
private $options = array();
public $id;
public $selected;
public $name;

public function add($options){
foreach($options as $key=>$text){
if(empty($text)){
$text = ucfirst($key);
}
$this->options[$key] = $text;
}

}
public function render(){
foreach($this->options as $key => $text){
$selected = $key == $this->selected ? " selected=\"selected\"": '';
$optionHtmlArr[] = "\t<option value=\"".$key."\"".$selected.">".$text."</option>";

}
if(count($optionHtmlArr)){
echo "<select id ='".$this->id."' name='".$this->name."'>\n".implode("\n",$optionHtmlArr)."\n</select>";
}
}
}
$select = new Select();
$select->selected = "1";
$select->name = "my_select_name";
$select->id = "my_select_id";
$select->add(array('choose'=>'','1'=>'Email','2'=>'Phone','3'=>'Letter'));
$select->render();
?>[/code]
Thanks

As a note to the original post:
The "handle"-php and ways of doing it - that was not my issue here, simply how to make a class the way it should be done. What's in it (different functions and the php rendering) can be somewhat different and based on individual needs and prefs - here it was provided just to fire it up.

Thanks guys!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.