PHP Sorting

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

    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
  • Andy Hassall

    #2
    Re: PHP Sorting

    On 28 Aug 2003 04:56:54 -0700, janus2@liv.ac.u k (J J) wrote:
    [color=blue]
    >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?[/color]

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

    ?>
    </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 (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    • James

      #3
      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


      Comment

      • Andy Hassall

        #4
        Re: PHP Sorting

        On Thu, 28 Aug 2003 19:34:10 +0100, "James" <graduate@dsl.p ipex.com> wrote:
        [color=blue]
        >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.[/color]

        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 (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        • James

          #5
          Re: PHP Sorting

          Where do I put this piece of code?

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

          J


          Comment

          • James

            #6
            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


            Comment

            • RonS

              #7
              Re: PHP Sorting

              James wrote:
              [color=blue]
              > 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
              >
              >[/color]
              Try:
              echo "{$client['client']}: {$client['hours']} Hours<br>";
              instead of:
              echo "{$client['name']}: {$client['hours']} Hours<br>";

              Comment

              • James

                #8
                Re: PHP Sorting

                works.... sorry, forgot to post earlier

                cheers


                Comment

                Working...