Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/15628-how-do-i-run-foreach-arrays/
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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.