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

PHP Sorting

P: n/a
J J
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
Jul 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 28 Aug 2003 04:56:54 -0700, ja****@liv.ac.uk (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 (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2

P: n/a
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
Jul 16 '05 #3

P: n/a
On Thu, 28 Aug 2003 19:34:10 +0100, "James" <gr******@dsl.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 (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #4

P: n/a
Where do I put this piece of code?

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

J
Jul 16 '05 #5

P: n/a
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
Jul 16 '05 #6

P: n/a
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>";

Jul 16 '05 #7

P: n/a
works.... sorry, forgot to post earlier

cheers
Jul 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.