在foreach语句

时间:2015-05-05 23:39:27

标签: javascript php mysql arrays

编辑:之前我试图简化它,但我想我会把它全部放在那里,因为它可能会让我对我想要达到的目标感到困惑。

我使用两个mySQL表来创建一个javascript图表,在同一个图表上显示条形图和折线图。显示出勤率的线图和显示该日期的酿造过程的条形图。 表A:

id |   date   | attended
1  |2015-01-14| 3
2  |2015-01-20| 4
3  |2015-01-31| 2
4  |2015-02-21| 3
5  |2015-02-25| 4
6  |2015-03-16| 4

表B:

id |    name    |    brewdate   | bottledate
1  | Honey Ale  |   2015-01-14  | 2015-02-21
2  |     IPA    |   2015-01-14  | 2015-01-31
3  |   Porter   |   2015-01-20  | 2015-02-25
4  | Brown Ale  |   2015-02-21  | 2015-03-16

使用下面的代码,我可以获得与zingchart一起使用的数组:

<?php
    $sql = "SELECT * DATE_FORMAT(DATE(date), '%m/%d') AS bpdate
    FROM TableA ORDER BY date";
    $data = $conn->query($sql);
?>
<script
    //GET array of attendance for line chart
    var present=[<?php
    mysqli_data_seek($data, 0);
    while($info=mysqli_fetch_array($data)) {
        echo $info['present'].",";
    }
    ?>];
    //Get dates for x-axis of chart
    var dates=[<?php
    mysqli_data_seek($data, 0);
    while($info=mysqli_fetch_array($data))
        echo '"'.$info['bpdate'].'",';
    ?>];
    <?php
        $sql = "SELECT COUNT(TableB.brewdate) AS brewed
        FROM TableB
        RIGHT JOIN TableA
        ON TableB.brewdate=TableA.date
        GROUP BY TableA.date ORDER BY TableA.date";
        $events = $conn->query($sql);
    ?>
    //Get number of beers brewed per date
    var brewed=[<?php
    while($info=mysqli_fetch_array($events)) {
        echo $info['brewed'];
    }
    ?>];
    <?php
        $sql = "SELECT TableB.*
        FROM TableB
        RIGHT JOIN TableA
        ON TableB.date=TableA.date
        ORDER BY TableA.date";
        $names = $conn->query($sql);
    ?>
    //Get the name of each beer that was brewed.
    var brewednames=[<?php
    while($info=mysqli_fetch_array($names))  {
        echo '"' . $info['name'] . ',"';
    }
    ?>];
</script>

这给了我这个:

var present=[3,4,2,3,4,4];
var dates=["01/14","01/20","01/31","2/21","02/25","03/16"];
var brewednames=["Honey Ale","IPA","Porter","","Brown Ale","",""];

我遇到的问题是变量 brewednames 。它正确地给了我啤酒的列表,其中填充了没有酿造啤酒的日期的空白,但是它列出了在不同的数组元素中具有相同日期的啤酒,并且我希望它们在同一个中显示。像这样:

var brewednames=["Honey Ale IPA","Porter","","Brown Ale","",""];

这背后的原因是当用户将鼠标悬停在图表上的相应栏上时会显示此数组。因此,我希望它能够显示当天酿造的所有啤酒的名称,而不是显示该日期酿造的啤酒数量。

我发现this article能够帮助我将同一日期在相同数组元素中酿造的每种啤酒分组,但现在它们没有根据日期排序,因为所有空白都已移至开头数组:

var brewednames=[<?php
$info = array();
while($row=$names->fetch_assoc())  {
    $date = $row['date'];
    $name = $row['name'];
    $info[$date][] = $name;
}
foreach ($info as $date => $values) {
    echo '"';
    foreach($values as $value) {
        echo $value . ' ';
    }
    echo '",';
}
?>];

这给了我这个:

var namegroup=["   ","Honey Ale IPA","Porter","Brown Ale"]

非常感谢任何帮助。另外我确信我的代码并不完美,因为我在某种程度上教自己mySQL和PHP,所以如果有更清晰/更好的代码的建议,请随意权衡。谢谢。

2 个答案:

答案 0 :(得分:0)

<?php
    $sql = "SELECT TableB.*
    FROM TableB
    RIGHT JOIN TableA
    ON TableB.date=TableA.date
    ORDER BY TableA.date ASC";
    $names = $con->query($sql);
?>
var namegroup=[<?php
$info = array();
$weirdArr = array();
while($row=$names->fetch_assoc())  {
    $date = $row['date'];
    $name = $row['name'];
    $info[$date][] = $name;
    $weirdArr[] = $name; 
}
echo implode(',',$weirdArr);
?>];

WEIRD UPDATE 要获取引号中的字符串,只需将行替换为:

echo '"'.implode('","',$weirdArr).'"';

WEIRD UPDATE 2

<?php
        $sql = "SELECT TableA.date, GROUP_CONCAT(TableB.name SEPARATOR ' ') as gr_name
        FROM TableB
        RIGHT JOIN TableA
        ON TableB.date=TableA.date
        GROUP BY TableA.date
        ORDER BY TableA.date ASC";
        $names = $con->query($sql);
    ?>
    var namegroup=[<?php
    $info = array();
    $weirdArr = array();
    while($row=$names->fetch_assoc())  {
        $date = $row['date'];
        $name = $row['gr_name'];
        $info[$date][] = $name;
        $weirdArr[] = $name; 
    }
    echo implode(',',$weirdArr);
    ?>];

答案 1 :(得分:0)

感谢@Alex的所有帮助,但在看到你在SQL select语句中包含的GROUP_CONCAT()之后,我尝试了一种不同的方法,它给了我相同的结果,代码少了很多。

<?php
        $sql = "SELECT TableA.date, GROUP_CONCAT(TableB.name) as gr_name
        FROM TableB
        RIGHT JOIN attendance
        ON TableB.brewdate=TableA.date
        GROUP BY TableA.date
        ORDER BY TableA.date";
        $names = $conn->query($sql);
    ?>
    var namegroup=[<?php
    while($info=mysqli_fetch_array($names)) {
        echo '"' . $info['gr_name'] . '",'; 
    }
    ?>];

这给了我这个:

var namegroup=["Honey Ale, IPA","Porter","","Brown Ale","",""];