-2

我想$bankShowDistrict函数中使用来选择行

$sql = "SELECT DISTINCT `district` FROM `ctable` WHERE state='$_POST[id]' AND bank='$bank' ";

但它不起作用......有什么方法可以做到这一点?...提前致谢。

 class SelectList
 {
   protected $conn;

    public function __construct()
    {
        $this->DbConnect();
    }

    protected function DbConnect()
    {
        include "db_config.php";
        $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
        mysql_select_db($db,$this->conn) OR die("can not select the database $db");
        return TRUE;
    }

    public function ShowBank()
    {
        $sql = "SELECT DISTINCT `bank` FROM `ctable` WHERE 1 LIMIT 0, 30 ";
        $res = mysql_query($sql,$this->conn);
        $bank = '<option value="0">choose...</option>';
        while($row = mysql_fetch_array($res))
        {
            $bank .= '<option value="' . $row['bank'] . '">' . $row['bank'] . '</option>';
        }
        return $bank;
    }

    public function ShowState()
    {
        $sql = "SELECT DISTINCT `state` FROM `ctable` WHERE bank='$_POST[id]'";
        $res = mysql_query($sql,$this->conn);
        $state = '<option value="0">choose...</option>';
        while($row = mysql_fetch_array($res))
        {
            $state .= '<option value="' . $row['state'] . '">' . $row['state'] . '</option>';
        }
        return $state;
    }


    public function ShowDistrict()
    {
        $sql = "SELECT DISTINCT `district` FROM `ctable` WHERE state='$_POST[id]'";
        $res = mysql_query($sql,$this->conn);
        $district = '<option value="0">choose...</option>';
        while($row = mysql_fetch_array($res))
        {
            $district .= '<option value="' . $row['district'] . '">' . $row['district'] . '</option>';
        }
        return $district;
    }


          public function ShowBranch()
    {
        $sql = "SELECT DISTINCT `bname` FROM `ctable` WHERE district='$_POST[id]'";
        $res = mysql_query($sql,$this->conn);
        $bname = '<option value="0">choose...</option>';
        while($row = mysql_fetch_array($res))
        {
            $bname .= '<option value="' . $row['bname'] . '">' . $row['bname'] . '</option>';
        }
        return $bname;
    }

}

$opt = new SelectList();
4

2 回答 2

1

正如 deceze 在评论中所说,这样做:

$bank = $this->showBank();

showBank()函数返回$bank值。因此,您然后将函数的返回值传递给$bank可以使用的变量。

$this允许您选择类中的函数/变量。

于 2012-06-19T09:04:40.670 回答
0

在您的代码中 $bank 是一个字符串,每次它与另一个字符串连接时都在 while 循环中。

$bank 包含一个字符串,如

 "<option value='bank1'>bank1</option><option value='bank2'>bank2</option>...";

因此,如果您返回该字符串,它将包含不必要的数据

$sql = "SELECT DISTINCT `district` FROM `ctable` WHERE state='$_POST[id]' AND bank='$bank' ";
于 2012-06-19T09:07:08.430 回答