473,564 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Sorting

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
7 2431
On 28 Aug 2003 04:56:54 -0700, ja****@liv.ac.u k (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($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 (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
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
On Thu, 28 Aug 2003 19:34:10 +0100, "James" <gr******@dsl.p ipex.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
Where do I put this piece of code?

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

J
Jul 16 '05 #5
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
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
works.... sorry, forgot to post earlier

cheers
Jul 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2522
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
0
2719
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update not the whole page, but a portion of the page. Strangely enough the URL below ...
7
3247
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
19
25436
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to sort columns based on the column header the user has clicked in both Ascending and Descending formats.
10
2755
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to write such layers myself I got stuck on how to get filtered or sorted data from the data-layer. This is what I got: Objects
4
3089
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in grid. And with rebinding, sorting image (little triangle on the column header) goes away. I need to show sorting image as well custom sorting. Please...
7
4806
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want...
1
7175
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find...
5
3169
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user...
5
4916
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link:...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.