我想在foreach中创建一个数组的单个变量(数组是从数据库中获取的,在数据库中有2017,2018,2019,2020年值),通过jquery选择,但每次我选择前一年,结果是2020年。
$tahun = mysqli_query($koneksi,"SELECT year(log) FROM data_sensor WHERE log GROUP BY year(log)");
$tahuns = array();
while ($row = mysqli_fetch_array($tahun)) {
$tahuns[] = $row;
}
<script>
<?php foreach($tahuns as $row){ ?>
$(".year<?php echo $row[0]; ?>").click(function(){
<?php $choosen_year= $row[0]; ?> //declare choosen year
....
});
<?php } ?>
</script>
<?php
$bulan = mysqli_query($koneksi,"SELECT month(log) FROM data_sensor WHERE year(log) = '$choosen_year' GROUP BY month(log)");
$Bulan = mysqli_fetch_array($bulan);
echo $Bulan; //result always 2020
?>
我希望所选的数组与显示的数组相同。谢谢你的回复