Sort Multiple Arrays from MySQL DB

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    Sort Multiple Arrays from MySQL DB

    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($clien ts); $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_hour s = min($carers[$carer_num]['remaining'], $row['hours']);
    $carers[$carer_num]['clients'][] = array('client' => $row['name'],
    'hours' => $allocated_hour s);
    $carers[$carer_num]['remaining'] -= $allocated_hour s;
    }

    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>Nam e:</STRONG> <BR><UL><LI><?p hp echo $carer['name']?>
    </LI></UL>
    <P><STRONG>Hour s Allowed:</STRONG><BR><UL> <LI><?php echo
    $carer['hours']?></LI></UL>
    <P><STRONG>Rema ining Hours:</STRONG><BR><UL> <LI><?php echo
    $carer['remaining']?></LI></UL>
    <P><STRONG>Clie nts:</STRONG>
    <?php foreach ($carer['clients'] as $client)
    {
    echo "<UL><LI>{$clie nt['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:


    Many thanks,

    Janusz


Working...