2014-06-09 93 views
-2

我試圖創建簡單的下拉菜單與數字(0,1,2)作爲值。 爲了使啓動更容易,我將選定的值設置爲編號2.沒有出現錯誤, 下拉框存在,但沒有顯示任何值。我究竟做錯了什麼。請幫助下拉菜單在OOP php

<?php 
class pulldown { 

    function setName($name)   { $this->name = $name; } // sets name of   
    //select field 

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

    function showSelected() { 

     printf ("<select name='%s'>", $this->name); 

      foreach($this->lookupTable as $key => $val) 
      { 
       if($this->selected == $key){ 
        echo "<option select='selected' value='$key'>$val</option>"; 

       }else { 
        echo "<option value='$key'>$val</option>"; 

       } 

      } 

     echo" </select>"; 

    } 

    private $lookupTable; 
    private $selected; 
    private $name; 

} 

////////////////////////////////////////////////// 

class numbers extends pulldown { 

    function __construct() { 

     $this->setName('numbers'); 
     $this->setSelected("0"); 

     ///this are the numbers available to select 
     $this->lookupTable = Array (
      "0" => "zero", 
      "1" => "one", 
      "2" => "two" 
     ); 
    } 

} 


$mynumbers = new numbers(); 
$mynumbers->setSelected("2"); 

//$myresellers = new myresellers(); 
?> 
<html> 
<form action="pullDown.php" method="post"> 
    <table width="1000" border="0" align="center"> 
    <td> 
    <?php $mynumbers->showSelected(); ?> 
    </td> 
    </table> 
</form> 
+1

'$ this-> setSelected = $ selected; private $ selected;'你在那兒有錯誤的變量名。 – scragar

回答

1

在設置器中使用了錯誤的變量。您需要使用selected而不是setSelected。

function setSelected($selected) { $this->selected = $selected; } 
+0

我現在糾正了,但仍然沒有運氣 – user2815059

+1

我現在看到您使用錯誤的HTML語法。嘗試使用**選擇=選擇**而不是**選擇=選擇**。 –

+0

這也沒有幫助 – user2815059