Professional Web Applications Themes

PHP Sorting - PHP Development

Hi, I would like some help on the following problem please if anyone can help me. I have two datasets as follows: * Carers * Clients Each client needs X amount of hours to be looked after and each carer can afford Y amount of hours to give their services. So each Client is allocated a value X which indicates how long they need treatment for and each carer is allocated a Y value to indicate how much time they can afford. I want to sort this out so that a group of clients are allocated to a carer. Obviously, ...

  1. #1

    Default PHP Sorting

    Hi,

    I would like some help on the following problem please if anyone can
    help me. I have two datasets as follows:

    * Carers
    * Clients

    Each client needs X amount of hours to be looked after and each carer
    can afford Y amount of hours to give their services. So each Client is
    allocated a value X which indicates how long they need treatment for
    and each carer is allocated a Y value to indicate how much time they
    can afford.

    I want to sort this out so that a group of clients are allocated to a
    carer. Obviously, the total amount of X hours must be less than or
    equal to the Y value of a give carer

    E.G.

    Client 01: 2 Hours |
    Client 02: 3 Hours | Carer 01: 10 Hours
    Client 03: 4 Hours |
    Client 04: 1 Hours |

    Client 05: 2 Hours |
    Client 06: 5 Hours | Carer 02: 12 hours
    Client 07: 4 Hours | (One hour to spare but next client is has
    X=2)

    Client 08: 2 Hours |
    Client 09: 2 Hours |
    Client 10: 3 Hours | Carer 03: 18 Hours
    Client 11: 4 Hours |
    Client 12: 5 Hours |

    The clients and carers will be sorted alpahabetically so we'll group
    as many of the clients (that are ordered alphabetiaclly) to the 1st
    carer (alphabetically and repeat this so that when the 'Nth' carer is
    full, then we'll go to 'N+1' carer and start allocating them clients
    from where we left of.

    Is this possible to do via a MySQL database which stores the data and
    PHP scripts to manipulate the sorting? If so, can anyone help?

    Many thanks,

    Janusz
    J J Guest

  2. #2

    Default Re: PHP Sorting

    On 28 Aug 2003 04:56:54 -0700, [email]janus2liv.ac.uk[/email] (J J) wrote:
    >I would like some help on the following problem please if anyone can
    >help me. I have two datasets as follows:
    >
    >* Carers
    >* Clients
    >
    >Each client needs X amount of hours to be looked after and each carer
    >can afford Y amount of hours to give their services. So each Client is
    >allocated a value X which indicates how long they need treatment for
    >and each carer is allocated a Y value to indicate how much time they
    >can afford.
    >
    >I want to sort this out so that a group of clients are allocated to a
    >carer. Obviously, the total amount of X hours must be less than or
    >equal to the Y value of a give carer
    >
    >E.G.
    >
    >Client 01: 2 Hours |
    >Client 02: 3 Hours | Carer 01: 10 Hours
    >Client 03: 4 Hours |
    >Client 04: 1 Hours |
    >
    >Client 05: 2 Hours |
    >Client 06: 5 Hours | Carer 02: 12 hours
    >Client 07: 4 Hours | (One hour to spare but next client is has
    >X=2)
    >
    >Client 08: 2 Hours |
    >Client 09: 2 Hours |
    >Client 10: 3 Hours | Carer 03: 18 Hours
    >Client 11: 4 Hours |
    >Client 12: 5 Hours |
    >
    >The clients and carers will be sorted alpahabetically so we'll group
    >as many of the clients (that are ordered alphabetiaclly) to the 1st
    >carer (alphabetically and repeat this so that when the 'Nth' carer is
    >full, then we'll go to 'N+1' carer and start allocating them clients
    >from where we left of.
    >
    >Is this possible to do via a MySQL database which stores the data and
    >PHP scripts to manipulate the sorting? If so, can anyone help?
    It's pushing it a bit to do it in MySQL's level of SQL, so do part of it in
    PHP.

    Fetch your Carers into an array, with their name and hours, so you end up with
    something like:

    $carers = array(
    array(name => 'Carer 01', hours => 10, remaining => 10),
    array(name => 'Carer 02', hours => 12, remaining => 12),
    array(name => 'Carer 03', hours => 18, remaining => 18),
    );

    Fetch your clients, and loop through them, allocating time to the 'current'
    carer, or moving to the next.

    Something like (just using arrays rather than fetching from DB, but it ought
    to give the idea):

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

    $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),
    array('name' => 'Client 10', 'hours' => 3),
    array('name' => 'Client 11', 'hours' => 4),
    array('name' => 'Client 12', 'hours' => 5),
    );

    $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);

    ?>
    </pre>

    Outputs:

    Array
    (
    [0] => Array
    (
    [name] => Carer 01
    [hours] => 10
    [remaining] => 0
    [clients] => Array
    (
    [0] => Array
    (
    [client] => Client 01
    [hours] => 2
    )

    [1] => Array
    (
    [client] => Client 02
    [hours] => 3
    )

    [2] => Array
    (
    [client] => Client 03
    [hours] => 4
    )

    [3] => Array
    (
    [client] => Client 04
    [hours] => 1
    )

    )

    )

    [1] => Array
    (
    [name] => Carer 02
    [hours] => 12
    [remaining] => 1
    [clients] => Array
    (
    [0] => Array
    (
    [client] => Client 05
    [hours] => 2
    )

    [1] => Array
    (
    [client] => Client 06
    [hours] => 5
    )

    [2] => Array
    (
    [client] => Client 07
    [hours] => 4
    )

    )

    )

    [2] => Array
    (
    [name] => Carer 03
    [hours] => 18
    [remaining] => 2
    [clients] => Array
    (
    [0] => Array
    (
    [client] => Client 08
    [hours] => 2
    )

    [1] => Array
    (
    [client] => Client 09
    [hours] => 2
    )

    [2] => Array
    (
    [client] => Client 10
    [hours] => 3
    )

    [3] => Array
    (
    [client] => Client 11
    [hours] => 4
    )

    [4] => Array
    (
    [client] => Client 12
    [hours] => 5
    )

    )

    )

    )


    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  3. #3

    Default Re: PHP Sorting

    Magnificant help!

    I have done it and it seems to work well. Output is shown as below:

    [1] => Array
    (
    [name] => Carer 02
    [hours] => 12
    [remaining] => 1
    [clients] => Array
    (
    [0] => Array
    (
    [client] => Client 05
    [hours] => 2
    )

    [1] => Array
    (
    [client] => Client 06
    [hours] => 5
    )

    [2] => Array
    (
    [client] => Client 07
    [hours] => 4
    )

    )

    )

    etc......

    However, I don't want it to 'look' like this to the end user, how could I
    clean it up so that a non-technical user would easily understand it. An
    example is shown below.... obviously a basic look.

    #######################

    Name: Carer 2
    Hours Allowed: 12
    Remaining Hours: 1
    Clients:
    Client 05: 2 Hours
    Client 06: 5 Hours
    Client 07: 4 Hours

    #######################

    I will try and tinker with the formatting, but many thanks to Andy. I will
    require further help but would like to progress in stages and not go for it
    all at once.

    Many thanks,

    J


    James Guest

  4. #4

    Default Re: PHP Sorting

    On Thu, 28 Aug 2003 19:34:10 +0100, "James" <graduatedsl.pipex.com> wrote:
    >Magnificant help!
    >
    >I have done it and it seems to work well. Output is shown as below:
    >
    > [1] => Array
    > (
    > [name] => Carer 02
    > [hours] => 12
    > [remaining] => 1
    > [clients] => Array
    > (
    > [0] => Array
    > (
    > [client] => Client 05
    > [hours] => 2
    > )
    >
    > [1] => Array
    > (
    > [client] => Client 06
    > [hours] => 5
    > )
    >
    > [2] => Array
    > (
    > [client] => Client 07
    > [hours] => 4
    > )
    >
    > )
    >
    > )
    >
    >etc......
    >
    >However, I don't want it to 'look' like this to the end user, how could I
    >clean it up so that a non-technical user would easily understand it. An
    >example is shown below.... obviously a basic look.
    >
    >#######################
    >
    >Name: Carer 2
    >Hours Allowed: 12
    >Remaining Hours: 1
    >Clients:
    > Client 05: 2 Hours
    > Client 06: 5 Hours
    > Client 07: 4 Hours
    >
    >#######################
    >
    >I will try and tinker with the formatting, but many thanks to Andy. I will
    >require further help but would like to progress in stages and not go for it
    >all at once.
    Something like:

    <?php
    foreach ($carers as $carer) {
    ?>
    Name: <?php echo $carer['name']?><br>
    Hours Allowed:<?php echo $carer['hours']?><br>
    Remaining Hours:<?php echo $carer['remaining']?><br>
    Clients:<br>
    <?php
    foreach ($carer['clients'] as $client) {
    echo "{$client['name']}: {$client['hours']} Hours<br>";
    }
    }
    ?>

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  5. #5

    Default Re: PHP Sorting

    Where do I put this piece of code?

    Apologies, still waking up and it's my birthday!

    J


    James Guest

  6. #6

    Default Re: PHP Sorting

    Me again:

    I narrowed it down to:

    "{$client['name']}:

    If I put carer instead of client, then it works... but I want client and not
    carer - please help!

    J


    James Guest

  7. #7

    Default Re: PHP Sorting

    James wrote:
    > Me again:
    >
    > I narrowed it down to:
    >
    > "{$client['name']}:
    >
    > If I put carer instead of client, then it works... but I want client and not
    > carer - please help!
    >
    > J
    >
    >
    Try:
    echo "{$client['client']}: {$client['hours']} Hours<br>";
    instead of:
    echo "{$client['name']}: {$client['hours']} Hours<br>";

    RonS Guest

  8. #8

    Default Re: PHP Sorting

    works.... sorry, forgot to post earlier

    cheers


    James Guest

Similar Threads

  1. Sorting array vs sorting paginated array
    By limitedwave777 in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: May 14th, 01:58 PM
  2. sorting
    By John Vinson in forum Macromedia ColdFusion
    Replies: 7
    Last Post: November 16th, 08:56 AM
  3. Sorting XML
    By CUA web dev in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: May 18th, 01:35 PM
  4. Help with sorting... please.. :)
    By Sarah in forum ASP Database
    Replies: 1
    Last Post: July 21st, 08:30 AM
  5. Sorting in SQL
    By Jacco Schalkwijk in forum Microsoft SQL / MS SQL Server
    Replies: 0
    Last Post: July 9th, 08:11 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139