动态更改每行数据的下拉列表的选定状态

时间:2016-05-09 11:03:18

标签: php html mysql arrays

我想根据数据库中记录的内容显示包含所选日期的下拉菜单。

是否有任何有效的方法可根据记录的数据动态更改下拉菜单的选定状态?

谢谢

注意:

  • 如果下列诊所ID的记录日期超过一行,将会有很多下拉菜单

  • $ day是一个整数,1表示星期日,2表示星期一,依此类推

这是mycode

    // Check if any row existed
    if ($count>0) {
        // If row existed then start printing it
        while($row = mysql_fetch_assoc($retval))
        {
            $day = $row['day'];
            $startHour = $row['startHour'];
            $startMin = $row['startMin'];
            $endHour = $row['endHour'];
            $endMin = $row['endMin'];

            echo 
            "<span>" .
            "<select name='day[]'>" .
            "<option value='1' selected='selected'>Sunday</option>" . 
            "<option value='2'>Monday</option>" . 
            "<option value='3'>Tuesday</option>" .
            "<option value='4'>Wednesday</option>" .
            "<option value='5'>Thursday</option>" .
            "<option value='6'>Friday</option>" .
            "<option value='7'>Saturday</option>" .
            "<option value='0'>Everyday</option>" .
            "</select>"

            //Please ignore this below
            "<br>start : " . $startHour . "." . $startMin . 
            "<br>end : " . $endHour . "." . $endMin . 
            "<br><br>";
        } 
    }
    else {
    }

1 个答案:

答案 0 :(得分:1)

如果这是新代码,请使用PDO或MySQLi。 mysql是折旧的,不应该在新代码上使用php.net/manual/en/function.mysql-query.php试试这个链接,它对我帮助很大:phpdelusions.net / pdo

将您的代码更改为此类代码(此代码包含PDO实现):

<?php
$db = new PDO('mysql:host=yourhost;dbname=dbname', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")); 

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



//Printing schedule already in the database
$getbusinesshours = "select * from businesshours where clinicID = $clinicID";

$stmt = $db->prepare($getbusinesshours);
$stmt->execute();

$count = $stmt->rowCount();

// Check if any row existed
if ($count>0){
    // If row existed then start printing it
    foreach ($stmt as $row){
    {
        $day = $row['day'];
        $startHour = $row['startHour'];
        $startMin = $row['startMin'];
        $endHour = $row['endHour'];
        $endMin = $row['endMin'];

        $i      = 0;

        $days   =   array('Everyday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
        $select = '<span><select name="day[]">';


        while($i <= 7){
            if($i == $day){
                $selected   =   'selected="selected"';
            }
            else{
                $selected   =   '';
            }

            $select =   $select.
            '<option value="'.$i.'"'.$selected.'>'.$days[$i].'</option'>

            $i++;
        }

        $select =   $select.'</select>';
        echo $select;

        //Please ignore this below
        "<br>start : " . $startHour . "." . $startMin . 
        "<br>end : " . $endHour . "." . $endMin . 
        "<br><br>";
    } 
}
else {
}

?>

我希望这会有所帮助。