By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Sort Multiple Arrays from MySQL DB

P: n/a
Hi,

I have the following code:

<PRE>
<?php
$username = "####t";
$password = "####";
$hostname = "####";
mysql_connect($hostname, $username, $password) or die("Unable to connect to
MySQL");
print "Connected to MySQL<br>";
mysql_select_db("database,");
print "Connected to Database<br>";

?>

<?php
$carers = array(
array('name' => 'Carer 01', 'hours' => 10, 'remaining' => 10),
array('name' => 'Carer 02', 'hours' => 12, 'remaining' => 12),
);

$clients = array(
array('name' => 'Client 01', 'hours' => 2),
array('name' => 'Client 02', 'hours' => 3),
array('name' => 'Client 03', 'hours' => 4),
array('name' => 'Client 04', 'hours' => 1),
array('name' => 'Client 05', 'hours' => 2),
array('name' => 'Client 06', 'hours' => 5),
array('name' => 'Client 07', 'hours' => 4),
array('name' => 'Client 08', 'hours' => 2),
array('name' => 'Client 09', 'hours' => 2),
);

$carer_num = 0;

for ($i=0; $i<count($clients); $i++) {

$row = $clients[$i];

// If current carer doesn't have enough hours left, move to the next
while ($carer_num < count($carers) && $row['hours'] >
$carers[$carer_num]['remaining'])
$carer_num++;

// No more carers
if ($carer_num == count($carers)) {
break;
}

// Allocate time
$allocated_hours = min($carers[$carer_num]['remaining'], $row['hours']);
$carers[$carer_num]['clients'][] = array('client' => $row['name'],
'hours' => $allocated_hours);
$carers[$carer_num]['remaining'] -= $allocated_hours;
}

print_r($carers);

?>
<HR ALIGN="LEFT" WIDTH="50%">

<H3><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Better
Output</FONT></H3>
<?php foreach ($carers as $carer)
{ ?>
<FONT SIZE="2" FACE="Verdana, Arial, Helvetica, sans-serif">
<BR><STRONG>Name:</STRONG> <BR><UL><LI><?php echo $carer['name']?>
</LI></UL>
<P><STRONG>Hours Allowed:</STRONG><BR><UL><LI><?php echo
$carer['hours']?></LI></UL>
<P><STRONG>Remaining Hours:</STRONG><BR><UL><LI><?php echo
$carer['remaining']?></LI></UL>
<P><STRONG>Clients:</STRONG>
<?php foreach ($carer['clients'] as $client)
{
echo "<UL><LI>{$client['client']} : {$client['hours']} Hours
<br></LI></UL>";
}

echo "<BR><HR align=left WIDTH=25%>";
}
?></pre>

As you can see, I have two arrays at the start. However I now want the
script to be able to pull the arrays from a database I have already. A basic
schema is here: http://www.monkey-it.co.uk/db_schema.gif Is this possible to
do? The output of the above code can be seen here:
http://www.monkey-it.co.uk/test.php

Many thanks,

Janusz
Jul 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.