Jump to content


Photo

How do I run foreach arrays?


  • Please log in to reply
1 reply to this topic

#1 chester23

chester23
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 25 July 2006 - 09:36 PM

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

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 25 July 2006 - 09:49 PM

Select the results from the table, joining the Providers and Learners together, then:

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

?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users