Osaisiko kukaan neuvoa, kuinka tehdään oliolla kyselylomake?
Kuinka tehdään valmiilla oliolla
Kysymysmerk
6561
0Äänestä
Muut lukivat myös
Odottaa vastausta
Näihin keskusteluihin ei ole vielä vastattu. Ole ensimmäinen.
Turvaa tilisi vahvistamalla puhelinnumerosi. Voit silloin palauttaa unohtuneen salasanan tekstiviestillä.
Näihin keskusteluihin ei ole vielä vastattu. Ole ensimmäinen.
Vastaukset 6
Vanhimmat
erittäin epäselvä ja käsittämätön kysymys. ensinnäkin mikä on kyselylomake? ja minkä takia se olioilla pitäisi tehdä
Et todellakaan tarvitse olioita kyselylomakkeen tekemiseen. Jos tunnet tarvitsevasi, olet ilmeisen pahasti hakoteillä siitä mitä olet tekemässä.
Eli tarkoituksena olisi tehdä PHP-ohjelma, joka luo valmiilla olioilla mallin lomakkeen. Lomakkeessa kysyttäisiin esim. kolmen kaupungin lämpötilat ja tarkistettaisiin onko ne sopivat(onko syöte numero, osuuko lämpötila välille -50 50). Lisäksi kysyttäisiin näiden kolmen kaupungin säästä valintalaatikoilla (voitaisiin valita yksi tai useampi näistä paistaa sataa tuulee). Että tällanen dilemma.
en edelleenkään ymmärrä minkä takia siihen olioita pitäis käyttää. tai sitten tää on joku koulutehtävä. mutta mitä et siis osaa? vai etkö osaa php:tä alkuunkaan? minkä takia sitten ees yrität tommosta
...sen olion luokka, niin katsellaan sitten lisää...
-T
Joo eli kyseessä on todellakin koulutehtävä... Tossa noita luokkia...
CFormControl
Lomake-elementtien yhteiset ominaisuudet ja metodit, sisältää otsikot
esim. label input/option
- set_validate_func - voidaan määrätä lomakkeen arvot tarkastava funktio
- set_from_request - toteuttava metodi, joka asettaa lomakkeen arvon $_REQUEST-muuttujasta
- get_string - metodi, joka palauttaa elementin ja labelin XHTML:nä
*/
abstract class CFormControl {
protected $name = "", $value = "", $label = "";
protected $is_req = false; // onko oletusarvo saatu $_REQUEST-muuttujasta
protected $validate_func; // funktion nimi, jolle viedään tämän luokan olio
protected $display_errors = false;
// abstraktit funktiot, jotka pitää toteuttaa aliluokissa
abstract function set_from_request(); // palauttaa true jos onnistuu
abstract function get_string();
// tätä tarvitaan kun PHP menee muuttelemaan nameja $_REQUEST:ssa
protected function str_replace_underscore($string) {
return str_replace(array("."," "),"_",$string);
}
// olion luonnin yhteydessä haetaan mahdollinen arvo $_REQUEST-muuttujasta
function __construct($name, $value, $label = "") {
$this->name = $name;
$this->value = $value;
$this->label = $label;
$this->is_req = $this->set_from_request();
}
// is_-funktiot
function is_valid() {
$error = $this->get_error();
if (empty($error))
return true;
return false;
}
function is_set_from_req() {
return $this->is_req;
}
// set-funktiot
function set_validate_func($func) {
if (is_callable($func))
$this->validate_func = $func;
}
function set_name($name) {
$this->name = $name;
}
function set_label($value) {
$this->label = $value;
}
function set_value($value) {
$this->value = $value;
}
function set_display_errors($bool) {
if (is_bool($bool))
$this->display_errors = $bool;
}
// get-funktiot
function get_label() {
return $this->label;
}
function get_name() {
return $this->name;
}
function get_req_name() {
return $this->str_replace_underscore($this->name);
}
function get_value() {
return $this->value;
}
// tarkistaa annetulla funktiolla onko syötteessä virheitä
function get_error() {
if (isset($this->validate_func)) {
$call_func =$this->validate_func;
return $call_func($this);
} else {
return "";
}
}
// voidaan käyttää echo-funktion kanssa
function __toString() {
return $this->get_string();
}
}
/*
CText
Tekstilaatikko
*/
class CText extends CFormControl {
private $size;
const type = "text";
function __construct($name, $value, $label, $size="25") {
parent::__construct($name, $value, $label);
if (is_numeric($size))
$this->size = $size;
}
function set_from_request() {
if (!empty($_REQUEST[$this->get_req_name()])) {
$this->value = $_REQUEST[$this->get_req_name()];
return true;
}
return false;
}
function get_string() {
$string = "";
if (empty($this->label))
$string = "value)."\" name=\"$this->name\" size=\"$this->size\" maxlength=\"$this->size\" />";
else {
$id = uniqid("id_");
$string = "$this->labelvalue)."\" name=\"$this->name\" size=\"$this->size\" maxlength=\"$this->size\" />";
}
if ($this->display_errors) {
if (!$this->is_valid())
$string = $string."".$this->get_error()."";
}
return $string;
}
}
/*
CRadio
Yksittäinen valintanappi
*/
class CRadio extends CFormControl {
private $checked = false;
const type = "radio";
function set_from_request() {
if (isset($_REQUEST[$this->get_req_name()])) {
if($_REQUEST[$this->get_req_name()] == $this->value) {
$this->set_checked();
return true;
}
else {
$this->set_unchecked();
}
}
return false;
}
function is_checked() {
return $this->checked;
}
function set_checked() {
$this->checked = true;
}
function set_unchecked() {
$this->checked = false;
}
function get_value() {
if ( $this->is_checked() ) {
return $this->value;
}
}
function get_string() {
if ($this->is_checked())
$add_checked = "checked=\"checked\"";
else
$add_checked = "";
$add_error = "";
if ($this->display_errors) {
if (!$this->is_valid())
$add_error = "".$this->get_error()."";
}
if (strlen($this->label) < 1)
return "".$add_error;
else {
$id = uniqid("id_");
return "$this->label".$add_error;
}
}
}
/*
CCheckbox
Yksittäinen valintalaatikko
*/
class CCheckbox extends CFormControl {
private $checked = false;
private $name_indexed = false;
const type = "checkbox";
function set_from_request() {
if (isset($_REQUEST[$this->get_req_name()])) {
$values = $_REQUEST[$this->get_req_name()];
foreach ($values as $value) {
if ( $value == $this->value ) {
$this->set_checked();
return true;
}
}
}
$this->set_unchecked();
return false;
}
function is_checked() {
return $this->checked;
}
function set_checked() {
$this->checked = true;
}
function set_unchecked() {
$this->checked = false;
}
function get_value() {
if ( $this->is_checked() ) {
return $this->value;
}
}
function get_string() {
if ($this->is_checked())
$add_checked = "checked=\"checked\"";
else
$add_checked = "";
$add_error = "";
if ($this->display_errors) {
if (!$this->is_valid())
$add_error = "".$this->get_error()."";
}
if (strlen($this->label) < 1)
return "".$add_error;
else {
$id = uniqid("id_");
return "$this->label".$add_error;
}
}
}
/*
COption
Yksittäinen valintalistan vaihtoehto
*/
class COption extends CRadio {
//perintä CRadio:sta: olennaisesti samanlainen kuin radio, vähän erilainen tulostus
function get_string() {
if ($this->is_checked())
$add_checked = "selected=\"selected\"";
else
$add_checked = "";
if (empty($this->label))
return "";
else {
return "$this->label";
}
}
}
class COptionMultiple extends CCheckbox {
function get_string() {
if ($this->is_checked())
$add_checked = "selected=\"selected\"";
else
$add_checked = "";
if (empty($this->label))
return "";
else {
return "$this->label";
}
}
}
/*
CChoiceGroup
Valintaryhmän yhteiset ominaisuudet ja metodit
esim. select, checkbox-ryhmä, radiobutton-ryhmä
- set_group_validate_func - voidaan määrätä ryhmän valintojen virheettömyys
- create_choice - metodi, jonka tehtävä on luoda ja palauttaa sopiva lomake-elementti
- get_string - palauttaa ryhmän XHTML:nä
*/
abstract class CChoiceGroup {
protected $choices = array();
protected $groupname = "";
protected $group_validate_func;
protected $display_errors = false;
// $choices = Array( [label1] => value1, [label2] => value2, ...)
// $default = array, indeksi tai ""
function __construct($groupname, $choices, $default = "") {
$this->groupname = $groupname;
if (!empty($choices)) {
// luodaan kukin valinta
foreach ($choices as $label => $value) {
$this->choices[] = $this->create_choice($groupname, $value, $label);
}
// etsitään onko jokin saanut requestista oletusarvon
$is_req = false;
foreach ($this->choices as $choice) {
if ($choice->is_set_from_req()) {
$is_req = true;
break;
}
}
// jos requestista ei ole löytynyt valintaa, niin asetetaan default-arvon mukaan
if (!$is_req) {
$choices_count = count($this->choices);
if (is_array($default)) {
foreach ($default as $index) {
if (0 choices[$index]->set_checked();
}
} else {
if (is_numeric($default)) {
$index = $default;
if (0 choices[$index]->set_checked();
}
}
}
}
}
// is_-funktiot
function is_valid() {
$error = $this->get_error();
if (empty($error))
return true;
return false;
}
// set-funktiot
function set_validate_func($func) {
if (is_callable($func))
$this->group_validate_func = $func;
}
function set_display_errors($bool) {
if (is_bool($bool))
$this->display_errors = $bool;
}
// jos ryhmästä yksikin saa arvonsa requestista niin palauttaa true
function set_from_request() {
$isreq = false;
foreach($this->choices as $choice) {
if ($choice->set_from_request())
$isreq = true;
}
return $isreq;
}
// tarkistaa onko ryhmässä virheitä
// palauttaa virheilmoituksen tai tyhjän merkkijonon
function get_error() {
if (isset($this->group_validate_func)) {
$call_func = $this->group_validate_func;
// antaa tarkastavalle funktiolle kaikki valinnat, voisi antaa myös iteraattorin
return $call_func($this);
} else {
return "";
}
}
protected function get_choice_count() {
return count($this->choices);
}
function get_value() {
return $this->get_chosen_val();
}
function get_name() {
return $this->groupname;
}
abstract function get_label();
abstract function get_count(); // palauttaa valittujen määrän
abstract function get_chosen_ind(); // palauttaa valittujen indeksit taulukossa
abstract function get_chosen_val(); // palauttaa valittujen arvot
abstract protected function create_choice($groupname, $value, $label);
abstract function get_string();
function __toString() {
return $this->get_string();
}
}
/*
CRadioGroup
Valintanappiryhmä
*/
class CRadioGroup extends CChoiceGroup {
// palauttaa valitun indeksinumeron, jos ei löydy niin palauttaa -1
function get_chosen_ind() {
for ($i=0;$iget_choice_count();$i ) {
if ($this->choices[$i]->is_checked()) {
return $i;
}
}
return -1;
}
function get_chosen_val() {
for ($i=0;$iget_choice_count();$i ) {
if ($this->choices[$i]->is_checked())
return $this->choices[$i]->get_value();
}
return false; // ei pitäisi tulla tällaista tilannetta
}
function get_label() {
for ($i=0;$iget_choice_count();$i ) {
if ($this->choices[$i]->is_checked())
return $this->choices[$i]->get_label();
}
return false; // ei pitäisi tulla tällaista tilannetta
}
function get_count() {
if ($this->get_chosen_ind() == -1)
return 0;
return 1;
}
protected function create_choice($groupname, $value, $label) {
return new CRadio($groupname, $value, $label);
}
function get_string() {
$napit = "";
for ($i=0;$ichoices);$i ) {
$button = $this->choices[$i];
$napit = $napit."".($button->get_string())."\n";
}
if ($this->display_errors) {
if (!$this->is_valid())
$napit = $napit."".$this->get_error()."";
}
return $napit;
}
}
/*
CRadioGroup
Valintalaatikkoryhmä
*/
class CCheckboxGroup extends CChoiceGroup {
function is_checked($choice_index) {
return $this->choices[$choice_index]->is_checked();
}
function get_chosen_ind() {
$indexes = array();
for($i=0;$iget_choice_count();$i )
if ($this->is_checked($i))
$indexes[] = $i;
return $indexes;
}
function get_chosen_val() {
$values = array();
for($i=0;$iget_choice_count();$i )
if ($this->is_checked($i))
$values[] = $this->choices[$i]->get_value();
return $values;
}
function get_label() {
$values = array();
for($i=0;$iget_choice_count();$i )
if ($this->is_checked($i))
$values[] = $this->choices[$i]->get_label();
return $values;
}
function get_count() {
$sum = 0;
for($i=0;$iget_choice_count();$i )
if ($this->is_checked($i))
$sum ;
return $sum;
}
protected function create_choice($groupname, $value, $label) {
return new CCheckbox($groupname, $value, $label);
}
function get_string() {
$napit = "";
for ($i=0;$ichoices);$i ) {
$button = $this->choices[$i];
$napit = $napit."".($button->get_string())."\n";
}
if ($this->display_errors) {
if (!$this->is_valid())
$napit = $napit."".$this->get_error()."";
}
return $napit;
}
}
/*
CSelectGroup
Valintalista (alasvetovalikko yhdellä valinnalla)
*/
class CSelectGroup extends CRadioGroup {
protected $label = "";
function __construct($groupname, $choices, $label = "", $default = "") {
parent::__construct($groupname, $choices, $default);
$this->label = $label;
}
protected function create_choice($groupname, $value, $label) {
return new COption($groupname, $value, $label);
}
// palauttaa select-elementin mahdollisine virheineen
function get_string() {
$id = uniqid("id_");
if (strlen($this->label) > 0)
$valinnat = "$this->label";
else
$valinnat = "";
$valinnat = $valinnat."\n";
foreach($this->choices as $choice) {
$valinnat = $valinnat."\n ".$choice->get_string();
}
$valinnat = $valinnat."\n\n";
if ($this->display_errors) {
if (!$this->is_valid())
$valinnat = $valinnat."".$this->get_error()."";
}
return $valinnat;
}
}
/*
CSelectMultipleGroup
Valintalista (alasvetovalikko monella valinnalla)
*/
class CSelectMultipleGroup extends CCheckboxGroup {
protected $label = "";
protected $size = 4;
function __construct($groupname, $choices, $label = "", $size = 4, $default = "") {
parent::__construct($groupname, $choices, $default);
$this->label = $label;
if (is_numeric($size))
$this->size = $size;
}
protected function create_choice($groupname, $value, $label) {
return new COptionMultiple($groupname, $value, $label);
}
// palauttaa select-elementin mahdollisine virheineen
function get_string() {
$id = uniqid("id_");
if (strlen($this->label) > 0)
$valinnat = "$this->label";
else
$valinnat = "";
$valinnat = $valinnat."\n";
foreach($this->choices as $choice) {
$valinnat = $valinnat."\n ".$choice->get_string();
}
$valinnat = $valinnat."\n\n";
if ($this->display_errors) {
if (!$this->is_valid())
$valinnat = $valinnat."".$this->get_error()."";
}
return $valinnat;
}
}
?>