473,626 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
http://www.monkey-it.co.uk/test.php

Many thanks,

Janusz
Jul 16 '05 #1
0 2859

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

Similar topics

12
4301
by: James | last post by:
Hi, Have posted before, but will simplify problem here. For original post go to http://forums.devshed.com/t80025/s.html I have setup 2 arrays like so in my one page script: $carers = array( array('names' => 'Carer 01', 'hours' => 8, 'remaining' => 8),
5
10213
by: somaBoy MX | last post by:
This may be a stupid question, but what is the easiest way to sort a mysql resultset? For example I have a resulset containing a colomn 'product_name_english' and 'product_name_french' and want to sort the result on one of these colums based on a language parameter. I know how to do this in a query ("ORDER BY") but how can I do this after the query has been executed?
7
5650
by: Gary | last post by:
I haver a table of students - Say 100 students that I need to be able to update/delete and amend. I know I can do this one student at a time which is simple but lets say I want to see all the students on the screen at the same time, modify some, mark some for deletion and even have blank fields at the end to add a new record. In HTML which is generated I label each row and input field with a name/number combination i.e <input type=text...
13
10939
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
3
2780
by: cpuracr8 | last post by:
i've been looking through the topics here and i can't quite find one that helps me. i'm trying to sort a struct that contains three arrays using the sort() function. the three arrays are parallel and need to be grouped together when it is sorted. for instance, if i sort the int array by ascending order, i need the name and grade associated with that int array to also move with it. i hope what i said is not too confusing. what am i doing...
21
3196
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
7
2720
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both of the queries work fine when I use them to query the MySQL server directly. My guess is that the MySQL extension only expects a single resource back from the database, but get's several, or that it just checks the statement first, and decides...
5
7017
by: TechnoAtif | last post by:
Hi All..'mAtif..i've got stuck within checkboxes these days..i've got many input items like checkboxes,textarea and along with them there are many checkboxes...all the data except the checkbox's is comfortably getting entered into the database but the real problem is with the checkboxes..only one entry is being stored ...but i want to enter the multiple entries from the checkboxes to enter into the database...the form is a voting poll..and i...
2
3156
by: beargrease | last post by:
I'm kind of comfortable with basic joins, but a current project requires a complex query of many tables. The GROUP_CONCAT(DISTINCT ...) function has been very useful as returning my values as comma delimited values when joining multiple tables. I have one table called 'floorplans' which has two fields (floorplan_jpg & floorplan_pdf), I'd like each of these fields to return arrays of the same length (they have the same # of values in the data...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8642
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7203
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1515
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.