我试图从mysql表中获取数据并在Javascript中处理这些数据。 我在php的家里和javascript有许多不同之处:(而我的问题是我在php中认为需要了解javascript:)
好的继承人问题: 我从我的PHP脚本中获取了以这种格式表格中的数据:使用
while($Search=mysql_fetch_assoc($abfrage)){
$data[] = $Search;
}
echo json_encode( $data );
并获取此数据:
Array ( [0] => Array ( [id] => 1 [date] => 2017-12-01 [time] => 00:00:00 [active] => 1 ) [1] => Array ( [id] => 2 [date] => 2017-12-02 [time] => 00:00:00 [active] => 1 ) [2] => Array ( [id] => 3 [date] => 2017-12-08 [time] => 00:00:00 [active] => 1 ) [3] => Array ( [id] => 4 [date] => 2017-12-09 [time] => 00:00:00 [active] => 1 ) [4] => Array ( [id] => 5 [date] => 2017-12-15 [time] => 00:00:00 [active] => 1 ) [5] => Array ( [id] => 6 [date] => 2017-12-16 [time] => 00:00:00 [active] => 1 ) [6] => Array ( [id] => 7 [date] => 2017-12-22 [time] => 20:00:00 [active] => 1 ) )
所以我用javascript来获取数组或对象或其他任何东西。我真的不太了解对象 - 。
var myArr = JSON.parse
从这些数据中获取数组:
[[object Object] {
active: "1",
date: "2017-12-01",
id: "1",
time: "00:00:00"
}]
这是什么 [[对象] 意思是这是一个数组或一个对象,我该如何使用它?
我的目标是查看数据中是否有来自数据库的日期,如果是,请检查是否还有一个时间设置为此日期。
我在jsbin做了一个测试示例,但我不知道如何继续接收我想要的数据: http://jsbin.com/wariparoce/edit?html,js,console,output
感谢你所有的时间和耐心:)
甜菊
答案 0 :(得分:0)
尝试访问
public final class PointFactory {
// private classes
private class PointImpl implements Point {
private int x;
private int y;
private PointImpl(int x, int y) {
this.x = x;
this.y = y;
}
public int x() {
return x;
}
public int y() {
return y;
}
}
private class PointOrigoImpl implements Point {
public int x() {
return 0;
}
public int y() {
return 0;
}
}
// end private classes
public Point of(int x, int y) {
if (x == 0 && y == 0) {
return new PointOrigoImpl();
}
return new PointImpl(x, y);
}
}
这应该返回字段active,date ,,
的idenx 0中的元素这意味着您获得了一个数组(基于0索引)的对象,其结构类似于PHP关联数组..其中PHP中的键是JSOn中的键名称)
答案 1 :(得分:0)
也许这会有所帮助。解码JSON字符串后,您有一个数组myArr。只需直接访问这些项目:
var myArr = JSON.parse('[
{"id":"1","date":"2017-12-01","time":"00:00:00","active":"1"},
{"id":"2","date":"2017-12-02","time":"00:00:00","active":"1"},
{"id":"3","date":"2017-12-08","time":"00:00:00","active":"1"},
{"id":"4","date":"2017-12-09","time":"00:00:00","active":"1"},
{"id":"5","date":"2017-12-15","time":"00:00:00","active":"1"},
{"id":"6","date":"2017-12-16","time":"00:00:00","active":"1"},
{"id":"7","date":"2017-12-22","time":"20:00:00","active":"1"}
]');
var gew_datum="2017-12-08";
for(var i = 0;i < myArr.length;i++) {
if(gew_datum == myArr[i]['date']){
console.log(i + ") date: " + myArr[i]["date"] + ", time: " + myArr[i]["time"] + ", active: " + myArr[i]["active"]);
}
}
答案 2 :(得分:0)
非常感谢你做了这个伎俩 - 我在“结果”(你可以在jsbin上看到)的事情中跑来跑去,我后来创建的是为了找出给定的日期是否在数组中 - 并且我没有得到结果[0] ['时间']我预期的数据...... 非常感谢您对双方的快速和完美的帮助!