gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:39:50 28 4
gpt4 key购买 nike

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

有什么有效的方法可以根据记录的数据动态改变下拉菜单的选中状态?

谢谢

注意:

  • 如果后面的clinicID记录的天数不止一行,就会出现多个下拉菜单

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

这是我的代码

    // 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 {
}

最佳答案

如果这是新代码,请使用 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 {
}

?>

希望对您有所帮助。

关于php - 动态更改每行数据的下拉列表的选定状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37114128/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com