chester23 Posted July 25, 2006 Share Posted July 25, 2006 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:Provider1Learner1Learner2Learner3Learner4Provider2Learner5Learner6Learner9Learner24 Quote Link to comment https://forums.phpfreaks.com/topic/15628-how-do-i-run-foreach-arrays/ Share on other sites More sharing options...
effigy Posted July 25, 2006 Share Posted July 25, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/15628-how-do-i-run-foreach-arrays/#findComment-63663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.