Include empty values ​​in array from foreach statement

EDIT: I've tried to simplify it before, but I think I'll just leave everything out, because that might clear up some of the confusion as to what I'm trying to achieve.

I am using two mySQL tables to create a javascript chart showing a bar and line graph in the same chart. Line graph showing attendance and a bar graph showing which brewing processes were done on that date. Table 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

      

Table 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

      

Using the following code, I can get the arrays for use with 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>

      

Which gives me this:

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","",""];

      

The problem I am facing is the brewednames variable . This correctly gives me a list of beers with spaces filling in dates when the beer was not brewed, but it lists beers that have the same date in different array elements and I want them to show up in the same. For example:

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

      

The rationale for this is that this array is displayed when the user hovers over the appropriate column in the chart. So instead of showing the amount of beer brewed on that date, I want it to give the names of all beers brewed on that date.

I found this article that helped me to group every beer that was brewed on the same day in the same array element, but now they are not ordered according to when all the spaces were moved to the beginning of the array:

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 '",';
}
?>];

      

Which gives me this:

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

      

Any help would be much appreciated. Also I'm sure my code is not perfect as I am teaching myself a bit about mySQL and PHP, so if there are suggestions for cleaner / better codes feel free to weigh. Thank.

+3


source to share


2 answers


Thanks for all the help @Alex, but after looking at the GROUP_CONCAT () you included in the SQL select statement, I tried a different approach which gave me the same results with MUCH less code.

<?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'] . '",'; 
    }
    ?>];

      



Which gives me this:

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

      

0


source


<?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 To get quoted strings, just replace the string with:

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

      



FREE 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);
    ?>];

      

0


source







All Articles