首页 > 解决方案 > 我通过 json_encode 数组的基本样式属性有什么问题?

问题描述

我有一个 Javascript 日历<table id="calendario">,它允许我通过calendario.rows[i].cells[i].

此日历允许用户进行预订,并在用户觉得已经有太多预订时提供关闭一天的选项。

我将他的选项保存在布尔变量中,并将对应的位置 , 保存到SQL DBcalendario.rows[i].cells[i]的字符串中。

当我将查询的单个结果保存在 javascript 变量中时,我可以成功更改样式属性颜色,但是当我遍历结果数组时,我的页面中没有任何变化。

我在表中有 2 列"reservation"称为"Closed" (包含布尔值 0 或 1)"Position" (包含字符串 calendario.rows[i].cells[i])

当我获取数组的一个结果时:

while($row = $result->fetch_assoc()) {
    $position = $row["Position"];
    }

然后我将它保存在一个 Javascript 变量中:

<script>
var red_day =  <?php echo $position; ?>;
</script>

然后我使用一个函数来更改样式并打印我的结果以进行测试:

<script> 
color_red(red_day);
function color_red(position){
position.style.color = "red";
document.getElementById("result").innerHTML = position;
}
</script>

我把最后一天涂成红色,在“结果”中<div>我得到了[object HTMLTableCellElement]

当我尝试:

$phparray = array();
 while($row = $result->fetch_assoc()) {
$phparray[] = $row["Position"];
}
    
    

然后将其保存在 Javascript 中并调用一个函数:

<script>
var jsarray = <?php echo json_encode($phparray); ?>;
result(jsarray){
var  text, pLen, i;
pLen = jsarray.length;
text = "<ul>";
for (i = 0; i < pLen; i++) {
jsarray[i].style.color = "red";
 text += "<li>" + jsarray[i] + "</li>";
}
text += "</ul>";


document.getElementById("result").innerHTML = text;
}

现在我没有任何单元格被涂成红色,但我在 var 文本中得到以下结果:

calendario.rows[4].cells[1]

calendario.rows[5].cells[5]

我究竟做错了什么?

标签: javascriptphpsqljsondom

解决方案


看来您在定义之前调用 color_red()


推荐阅读