将功能合并到oop中

时间:2014-06-25 11:52:59

标签: php function oop

我正在尝试为我的一个学校作业编写代码,但我找不到它不起作用的原因。它可能是一些我还没有意识到的简单。任何人都可以指出我正确的方向。我正在努力让一个班级生成3个下拉框来选择日,月和年。第3个功能似乎也存在无限循环。代码如下,并在两个文件之间分开。

文件1:datselect.php

<?php

    include ("datselect_functions.php");

    $cls = new Dropdown();

    $monthname = array(1 => "January", "February", "March", "April", "May", "June",
                     "July","August", "September", "October", "November", "December");

    $today = time(); 
    $f_today = date("M-d-Y",$today);

    $func = insert_day();
    $func = insert_month($monthname);
    //$func = insert_year($today);

    echo html_head($f_today);
    echo $cls -> getHTML();
    echo html_foot();

?>

文件2:datselect_functions.php

<?php

function html_head($f_today) {
    return "<html>
            <head>
            <title>Select a date</title>
            </head>
            <body>
            <div>
                Today is ".$f_today."
            </div>";
}

class Dropdown {
    private $name = "";
    private $options = array();
    private $selected = "";

    public function __construct($name = "") {
        $this->name=$name;
    }

    public function setOption($var,$label) {
        $this->options[$var]=$label;
    }

    public function getHTML() {
        $html = "<select name='".$this->name."'>";
        var_dump($this->name);
        foreach($this->options as $var => $label) {
            if ($this->selected=$var) {
                $html.= "<option selected value='".$var."'>.$label.</option>";
            } else {
                $html.= "<option value='".$var."'>.$label.</option>";
            }
        }
        $html.= "</select>";
        return $html;
    }
}

function html_foot() {
return "</body></html>";
}

function insert_day(){
    $ddd = new Dropdown("day");
    for($i=1; $i<=31; $i++){
        $ddd -> setOption($i, $i);
    }
}

function insert_month($monthname){
    $ddm = new Dropdown("month");
    foreach($monthname as $key => $value) {
        $ddm -> setOption($key, $value);
    }
}

function insert_year($today){
    $currentyear = date("Y", $today);
    $ddy = new Dropdown("year");
    for($n=$currentyear;$n=$currentyear+3;$n++) {
        $ddy -> setOption($n, $n);
    }
}

?>

4 个答案:

答案 0 :(得分:0)

&#34; =&#34;不是比较而是设置(使用&#34; ==&#34;相反):

if ($this->selected=$var) {

无端环:

for($n=$currentyear;$n=$currentyear+3;$n++) {

使用此:

for($n=$currentyear;$n < $currentyear+3;$n++) {

答案 1 :(得分:0)

您的Dropdown类只是一个生成下拉html代码的帮助程序。您需要存储生成的代码或将其输出到某处,以便“工作”。

$func = insert_day(); // 1
$func = insert_month($monthname); // 2
//$func = insert_year($today);

echo html_head($f_today); // 3
echo $cls -> getHTML(); // 4
echo html_foot(); // 5

您在此处一步一步地执行以下操作:

  1. 您将日期选择的下拉html代码分配给$ func
  2. 您将月份选择的下拉html代码分配给$ func,覆盖1中的所有内容。您应该使用新变量或将其附加$func .= insert...,如果这是您想要的
  3. 您输出标题html
  4. 您输出一个空的下拉列表,因为Dropdown实例$ cls未在任何位置配置选项数组
  5. 您输出页脚html
  6. 缺少的是你还应该输出1和2之后的结果。 insert_year中的无限循环也是因为您在for循环中指定当前年份+ 3,而不是将其与===进行比较。

    同样,每个类只有一个文件并且没有别的东西是好的风格,但这不应该是问题。

    希望有所帮助。

答案 2 :(得分:0)

您的insert_day,insert_month函数不起任何作用。下面我更新了你的代码以显示日期,虽然你真的不需要这么多代码。

//dateselect.php
<?php

include ("datselect_functions.php");


$cls = new Dropdown();

$monthname = array(1 => "January", "February", "March", "April", "May", "June", "July",
                   "August", "September", "October", "November", "December");

$today = time(); 
$f_today = date("M-d-Y",$today);

$func = $cls->insert_day();
//$func = insert_month($monthname);
//$func = insert_year($today);

echo html_head($f_today);
echo $cls -> getHTML();
//echo html_foot();

?>


//datselect_functions


<?php

function html_head($f_today) {
return "<html>
            <head>
                <title>
                    Select a date
                </title>
            </head>
            <body>
                <div>
                    Today is ".$f_today."
                </div>";
}

class Dropdown {
private $name = "";
private $options = array();
private $selected = "";

public function __construct($name = "") {
    $this->name=$name;
}

public function setOption($var,$label) {
    $this->options[$var]=$label;
}

public function getHTML() {
    $html = "<select>";
    //var_dump($this->options);
    foreach($this->options as $var => $label) {
        if ($this->selected==$var) {
            $html.= "<option selected value='".$var."'>.$label.</option>";
        }
        else {
        $html.= '<option value="'.$var.'">'.$label."</option>";

        }
    }
    $html.= "</select>";
    //print_r($html);
    return $html;
}

function insert_day(){

for($i=1; $i<=31; $i++){
    $this -> setOption($i, $i);
}
}

function html_foot() {
return "</body></html>";
}


}

function insert_month($monthname){
$ddm = new Dropdown("month");
foreach($monthname as $key => $value) {
    $ddm -> setOption($key, $value);
}
}

function insert_year($today){
$currentyear = date("Y", $today);
$ddy = new Dropdown("year");
for($n=$currentyear;$n<=$currentyear+3;$n++) {
    $ddy -> setOption($n, $n);
}
}

?>

答案 3 :(得分:0)

有很多事情出错了。

//this is unnecessary, you now create an empty dropdown and echo it here.
$cls = new Dropdown();    
echo $cls -> getHTML();

//instead of this
$func = insert_day();
$func = insert_month($monthname);
//$func = insert_year($today);

//do this
$days = insert_day();
$months = insert_month($monthname);
$years = insert_year($today);

//then make the dropdows appear like this.
echo $days;
echo $months;
echo $years;

//These 3 functions require an extra line of code.
function insert_day(){
    $ddd = new Dropdown("day");
    for($i=1; $i<=31; $i++){
        $ddd -> setOption($i, $i);
    }
    //add this line to all of these three functions, but change the var name
    return $ddd->getHTML();
}

这应解决您的大多数错误,但现在可能会出现其他一些错误。