创建foreach数组的各个变量以放入mysql查询

时间:2018-01-03 13:41:38

标签: php jquery mysql arrays foreach

我想在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

?>

我希望所选的数组与显示的数组相同。谢谢你的回复

0 个答案:

没有答案