4

Edit: Nico Parodi's answer is correct. I will eventually return to finding out why, but for now I will just take it as it is and hope nothing else fails.

I have a table with three fields: "date", "name", "location". I want to group all the records selected from this table based on their date.

By copy-pasting some code from php mysql group by date with yyyy-mm-dd format, I've managed to get this array, date -> name:

$result = mysqli_query($con,"SELECT date, name, location FROM events");
while($row = mysqli_fetch_array($result)) {
    $foo[$row['date']][]=$row['name'];
}

Everything's great, I can iterate it without issues. Now I want to store all the row columns as a value for the date key, so I try to store the entire row as a value:

$result = mysqli_query($con,"SELECT date, name, location FROM events");
while($row = mysqli_fetch_array($result)) {
    $foo[$row['date']][]=$row;
}

And now I can't iterate it. count($rowCol) seems to give me the nr of columns in all the array for either keys, not just for one. How can I iterate it?

foreach($foo as $date => $events) {
    echo $date . ": "; // this is okay         

    foreach($events as $key => $rowCol){
        for ($i = 0; $i<count($rowCol); $i++) {
            echo $rowCol[$i] . " ";     
        }
    }
}
Community
  • 1
  • 1
Buffalo
  • 3,861
  • 8
  • 44
  • 69

4 Answers4

1

Can you tell us what's the output of this code? (show plain text, no HTML):

foreach($foo as $date => $event) {
    echo $date . ": ";

    foreach($event as $key => $value){
        print_r($value);
    }

    echo "\n";
}

Original answer:

$events contains a single event, your code should look more like this:

foreach($foo as $date => $event) {
    echo $date . ": ";

    foreach($event as $key => $value){
        echo $value . " ";
    }

    echo "\n";
}
Ast Derek
  • 2,739
  • 1
  • 20
  • 28
1
for ($i = 0; $i<count($rowCol); $i++) {
        echo $rowCol[$i] . " ";     
    }

this part of code doesn't work because rowCol is an associative array + numeric array. Try to replace mysqli_fetch_array with mysqli_fetch_row

Karim
  • 2,388
  • 1
  • 19
  • 13
1
for ($i = 0; $i<count($rowCol); $i++) {
    echo $rowCol[$i] . " ";     
}

$rowCol has the double of fields that you expect as mysqli_fetch_array() fetches as both associative and numeric array. So,

for ($i = 0; $i<(count($rowCol)/2); $i++) {
        echo $rowCol[$i] . " ";     
    }

should work.

Nico
  • 343
  • 3
  • 7
  • Because mysqli_fetch_row() fetches only as numeric array so you now have the number of fields you always expected, so in this case you don't have to use count($rowCol)/2, just use as before, count($rowCol). – Nico Jun 18 '13 at 19:28
  • use var_dump($rowCol) to print the array and see why it has the double of fields – Nico Jun 18 '13 at 19:32
  • Minor question: can I also get the key when I'm iterating? the name of the table column? – Buffalo Jun 18 '13 at 19:42
  • yes, to do it you should change mysqli_fetch_array call to [mysqli_fetch_assoc](http://php.net/mysqli_fetch_assoc) to fetch results into only associative array - and then you can use `foreach ($rowCol as $colName=>$val)` – Karim Jun 19 '13 at 07:20
1

Replace :

    for ($i = 0; $i<count($rowCol); $i++) {
        echo $rowCol[$i] . " ";     
    }

By

    foreach($rowCol as $k =>$v) {
        echo $v . " ";     
    }
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50