Jump to content

Archived

This topic is now archived and is closed to further replies.

chester23

How do I run foreach arrays?

Recommended Posts

I am running a database with two tables Providers and Learners, they are linked by a provider code on each table. So some learners are linked to the same provider. How can I make it show this for example:

Provider1
Learner1
Learner2
Learner3
Learner4

Provider2
Learner5
Learner6
Learner9
Learner24

Share this post


Link to post
Share on other sites
Select the results from the table, joining the Providers and Learners together, then:

[code]
<?php

### This is an example of a MySQL result.
$mysql_row_result = array(
### Each of these represent a row.
array(
### And each of these a field/value pair.
Provider => 'Provider1',
Learner => 'Learner1',
),
array(
Provider => 'Provider1',
Learner => 'Learner2',
),
array(
Provider => 'Provider1',
Learner => 'Learner3',
),
array(
Provider => 'Provider1',
Learner => 'Learner4',
),
array(
Provider => 'Provider2',
Learner => 'Learner5',
),
array(
Provider => 'Provider2',
Learner => 'Learner6',
),
array(
Provider => 'Provider2',
Learner => 'Learner9',
),
array(
Provider => 'Provider2',
Learner => 'Learner24',
),
);

$previous_provider = '';
### Coming from MySQL, you don't want the foreach, you want
### while ($row = mysql_fetch_array($result))
foreach ($mysql_row_result as $row) {
if ($row['Provider'] != $previous_provider) {
echo '<b>', $row['Provider'], '</b><br />';
}
echo $row['Learner'], '<br />';
$previous_provider = $row['Provider'];
}

?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.