0

I'm struggling to understand the syntax of for each/while loops in relation to MySQL queries.

I'm trying to output a list of dates that are grouped by year. I understand that I need a for each loop that contains a while statement...

for each -> 'year' -> list all dates.

I'm not even sure if the 'for each' needs to be a 'for each' (or a 'while').....I'm currently using a 'while' to get the dates list.

This is the code that I'm using to pull out the dates in an un-grouped list that I need to be part of the 'for each'...

$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo  [html containing date fields - {$row['year']}{$row['month']}{$row['day']} etc]}
};

I'm fairly happy with how to enclose this in an outer loop (year) - I'm just unsure of the syntax to make the outer loop act on the 'year' field.

4
  • this all sounds wrong. Try explainig what you want to acheive. Im sure SQL queries in a loop isnt the answer Commented Jan 27, 2014 at 12:22
  • I just need to understand how to group my output by year using the 'year' field from the same query Commented Jan 27, 2014 at 12:24
  • Whereever possible, do this type of thing in the database, not php. So you would add GROUP BY to your sql query Commented Jan 27, 2014 at 12:28
  • I tried this but could not get an output....I guess I need to simply echo the output instead of trying to loop through it? Commented Jan 29, 2014 at 13:11

2 Answers 2

1

Refering to IPO principle you should fetch all your records now and then group them. This keeps your code structured, reusable and easier to read:

//fetch all records
$records = array();
while($tmp = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $records[] = $tmp;
}

//group
$finalResult = groupRecordsBy($records, 'month');

function groupRecordsBy($data, $keyToGroupBy) {
    $result = array();
    foreach($data as $index => $record) {
        $result[$record[$keyToGroupBy]][] = $record;
    }
    return $result;
}

Hope that get's you started!

PS: You should take a look at OOP to even structure your code more! And you should take a look at PHP's MySQLi or even better PDO instead of using mysql_* functions. They will be marked deprecated soon!

Sign up to request clarification or add additional context in comments.

2 Comments

By default, PHP already throws a notice now when you use mysql in a php script. php.net says: "This extension is deprecated as of PHP 5.5.0, and will be removed in the future."
Looks like i've got a lot of reading to do. I'm fine with SQL but know only very little about PHP...maybe I should go on a course!
0

Check StackOverFlow Post for the differences between foreach and while loop.

For Each Loop Structure as follows-

$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
   echo $value ."<br/>";
}

And While Loop Structure as follows-

$arr = array(1, 2, 3, 4);

$i = 0;

while ($i <= count($arr)) {
   $a = $arr[$i];
   echo $a ."<br/>";
   $i++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.