我在桌子上有这样的数据:
pray_name | time_before_iqamah
Subuh | 1
Dhuhur | 10
Ashar | 10
Maghrib | 10
Isya | 10
我将它生成到PHP jSon
$qPray = mysqli_query($con, "SELECT * FROM tb_prays WHERE active = 'Y'");
while($dPray = mysqli_fetch_array($qPray))
{
$timeBeforeIqamah[] = $dPray['time_before_iqamah'];
}
jSon结果
{"timeBeforeIqamah":["1","10","10","10","10"]}
现在我希望根据JS上的祈祷名称获得timeBeforeIqamah
。
var list = ["Imsak", "Subuh", "Terbit", "Dhuhur", "Ashar", "Maghrib", "Isya"];
var times = prayTimes.getTimes(date, geo, timezone);
for(var i in list)
{
if(jsonStr.currentTime == times[list[i].toLowerCase()] + ":00")
{
if(list[i] == "Imsak" || list[i] == "Terbit")
{}
else
{
alert(jsonStr.timeBeforeIqamah[i]);
}
}
}
假设我们处于Subuh
时间意味着我尝试alert(jsonStr.timeBeforeIqamah[i]);
应该获得价值:1
,但我得到的是:10
< / p>
我想要的是基于JS上的祈祷名称得到time_before_iqamah
。
怎么做?
答案 0 :(得分:1)
问题是您的表格中的数据与list
中的数据的顺序不同:
Index | pray_name | time_before_iqamah
0 | Subuh | 1
1 | Dhuhur | 10
2 | Ashar | 10
3 | Maghrib | 10
4 | Isya | 10
将其与:
进行比较var list = ["Imsak", "Subuh", "Terbit", "Dhuhur", "Ashar", "Maghrib", "Isya"];
Index | Value
0 | "Imsak"
1 | "Subuh"
2 | "Terbit"
3 | "Dhuhur"
4 | "Ashar"
5 | "Maghrib"
6 | "Isya"
在第一种情况下,subuh
的索引为0
,在第二种情况下,它的索引为1
,因此无法与之匹配。
要解决此问题,您可以更改您的php,以便传递与值匹配的名称:
$qPray = mysqli_query($con, "SELECT * FROM tb_prays WHERE active = 'Y'");
while($dPray = mysqli_fetch_array($qPray))
{
$timeBeforeIqamah[$dPray['pray_name']] = $dPray['time_before_iqamah'];
}
然后将您的javascript更改为:
if(list[i] == "Imsak" || list[i] == "Terbit")
{}
else
{
alert(jsonStr.timeBeforeIqamah[list[i]]);
}