473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Multiple Array Selection From Database Instead of In-Page Arrays

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:

[PHP]
$carers = array(
array('names' => 'Carer 01', 'hours' => 8, 'remaining' => 8),
array('names' => 'Carer 02', 'hours' => 12, 'remaining' => 12),
array('names' => 'Carer 03', 'hours' => 6, 'remaining' => 6),
);

$clients = array(
array('names' => 'Client 01', 'hours' => 2),
array('names' => 'Client 02', 'hours' => 3),
array('names' => 'Client 03', 'hours' => 4),
array('names' => 'Client 04', 'hours' => 1),
array('names' => 'Client 05', 'hours' => 2),
array('names' => 'Client 06', 'hours' => 5),
);
[/PHP]

However, I want to actually retrive this data from a MySQL table. These
arrays are then processed further down the page. The DB looks similar to the
setup here: http://www.monkey-it.co.uk/db_schema.gif

As you can see, I have a DB called 'database' and two tables. Now, how would
I set the page up so that it would retrieve the data from the DB and use it
for processing further down the page like the above arrays would have
originally.

Many thanks,

Janusz

--

************************************************** ***

QOTSA: "Nicotine, Valium, Vicodin, Marijuana, Ecstacy and Alcohol"
Einstein: "Imagination is More Important Than Knowledge"
Jul 16 '05 #1
12 4283
On Sat, 30 Aug 2003 11:27:31 +0100, "James" <gr******@dsl.pipex.com> wrote:
However, I want to actually retrive this data from a MySQL table.


http://uk.php.net/manual/en/function...etch-array.php

--
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
Below is what I have so far. I can retrieve the data I need it, but how do I
sort it like I did in the original?

[PHP]

<TITLE>.: PHP/MySQL Testing :.</TITLE>
<STYLE TYPE="text/css">
h3 {font-family: Verdana, Arial, Helvetica, sans-serif}
font.font {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: x-small;
}
</STYLE>

<H3>Connecting to Database and Tables</H3>
<PRE>
<FONT CLASS="font">
<?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";
?>
</FONT>

<H3>Fetching Carers Arrays</H3>

<FONT CLASS="font"><?
$result = mysql_query("SELECT name, hours, remaining FROM carers");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{printf ("Name: %s Hours: %s Remaining: %s<BR>", $row["name"],
$row["hours"], $row["remaining"]);}
?>
</FONT>

<H3>Fetching Clients Arrays</H3>

<FONT CLASS="font"><?
$result = mysql_query("SELECT name, hours FROM clients");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{printf ("Name: %s Hours: %s<BR>", $row["name"], $row["hours"]);}
?>
</FONT>

<H3>Original Output</H3>

<FONT CLASS="font">
<?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($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);

?>
</FONT>
<HR ALIGN="LEFT" WIDTH="50%">
<H3>Better Output</H3>
<FONT CLASS="font">
<?php foreach ($carers as $carer)
{ ?>
<BR><STRONG>Name:</STRONG> <BR><UL><LI><?php echo $carer['name']?>
</LI></UL>
<P><STRONG>Hours Allowed:</STRONG><BR><UL><LI><?php echo
$carer['hours']?></LI></UL>
<P><STRONG>Remaining Hours:</STRONG><BR><UL><LI><?php echo
$carer['remaining']?></LI></UL>
<P><STRONG>Clients:</STRONG>
<?php foreach ($carer['clients'] as $client)
{
echo "<UL><LI>{$client['client']} : {$client['hours']} Hours
<br></LI></UL>";
}

echo "<BR><HR align=left WIDTH=25%>";
}
?>
</FONT>
</PRE>

[/PHP
Jul 16 '05 #3
On Sat, 30 Aug 2003 15:35:56 +0100, "James" <gr******@dsl.pipex.com> wrote:
Below is what I have so far. I can retrieve the data I need it, but how do I
sort it like I did in the original? $result = mysql_query("SELECT name, hours, remaining FROM carers");


SELECT name, hours, remaining FROM carers ORDER BY name

--
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
Cool,

So if I replace this with what is currently there, what would I have to do
next to make the sorting process read from the DB instead of the two static
arrays that are present?

J
Jul 16 '05 #5
???

As a newbie, I haven't the foggiest clue!
Jul 16 '05 #6
On Sat, 30 Aug 2003 17:22:12 +0100, "James" <gr******@dsl.pipex.com> wrote:
???

As a newbie, I haven't the foggiest clue!


I'm not going to write the whole thing for you. You're already fetching from
the database, but all you're doing is printing out the data at the moment.

Add it to an array instead. Read through the link I posted before, for how to
use arrays.

Use the print_r function on the $row variable to see its structure. Think
about what structure you need - it's an array of several of these rows.

--
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 #7
Fair enough.... I get the following out:

Array
(
[name] => Carer 01
[hours] => 10
[remaining] => 10
)
Array
(
[name] => Carer 02
[hours] => 15
[remaining] => 15
)
Array
(
[name] => Carer 03
[hours] => 9
[remaining] => 9
)
Array
(
[name] => Carer 04
[hours] => 8
[remaining] => 8
)
Array
(
[name] => Carer 05
[hours] => 12
[remaining] => 12
)

any further help would be greatly appreciated, now I have this, how do I put
it into an array?

J
Jul 16 '05 #8
Hi,

Thanks for the reply. Indeed, there are about a dozen factors such as
language issues, level of training etc. This script is one of the most
simple ones that I can do relevant to the project. The project is sctually
just a proof-of-concept that the GRID can provide optimisation for difficult
scheduling problems. In fact the main problem has been outsourced to a
company which are programming the problem in 'C' I believe.

Many thanks for your interest and if anyone could help me with that last
problem I'd be very happy.

J
Jul 16 '05 #9
On Sun, 31 Aug 2003 09:28:03 +0100, "James" <gr******@dsl.pipex.com> wrote:

Hi you seem to be moving on with project now. However a question? Your
method of allocation assumes that all the carers and clients are all in the
same location. Are they? If not you could end up with a carer named Ms
Adams living next door to a client called Mr Wilson driving across town to
care for Mrs Anderson and carer Mr Xavier driving back to Mr Adams to care
for him. You should address this location problem early in your project if
it affects you. Your caring would become more efficient if you had location
information coded into your database structure.


Thanks for the reply. Indeed, there are about a dozen factors such as
language issues, level of training etc. This script is one of the most
simple ones that I can do relevant to the project. The project is sctually
just a proof-of-concept that the GRID can provide optimisation for difficult
scheduling problems. In fact the main problem has been outsourced to a
company which are programming the problem in 'C' I believe.

Many thanks for your interest and if anyone could help me with that last
problem I'd be very happy.


That looks equivalent to the Travelling Salesman problem, which is certainly
NP-complete - except with even more factors. TSP only takes into account
distance. Solving it optimally probably isn't going to be practical unless you
have very few clients (or have made some breakthrough in quantum computing).

Search on Google, there's stacks of research in solving it with heuristics
etc. to get good, if not necessarily optimal, solutions.

http://www.google.com/search?hl=en&i...lling+salesman

--
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 #10
Cool,

All I want now however is to get rid of the error that shows up at the end

J
Jul 16 '05 #11
On Sun, 31 Aug 2003 14:35:25 +0100, "James" <gr******@dsl.pipex.com> wrote:
All I want now however is to get rid of the error that shows up at the end


What have you tried to do to fix it? If you don't understand what's going on,
then print out each variable involved so you can see what's being processed.

--
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 #12
I do but still can;'t get to the problem, it just messes up right at the end
when there are no more clients to process

J
Jul 16 '05 #13

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

Similar topics

3
by: Emmett Power | last post by:
Hi, I have a form with a table with two fields SelectionID and Experience. I am posting the data to a database using an array function. I have set out the code below. The problem I am having...
6
by: frizzle | last post by:
Hi there I'm building this CMS, and at a point i have 3 similar drop downs. The values of the drop downs are called from a MysqlDB. The first one is just fine: do{ $selected = ($_GET ==...
0
by: B | last post by:
Using Access2000, the sample code below is what I have been modifying and working on since the past week and I could not get it to work properly. What I wanted to accomplish: 1) read from a...
8
by: Les Coover | last post by:
I have tried writing the selection process after scanf many different ways. No matter what is selected program execution goes to the add_data function. How can I get this to work? /*...
0
by: Adis | last post by:
Asp.Net Visual Studio 2003 SQL Server. Hi, I have database in Sqlserver and ListBox (Multiple Selection Mode) in my Visual Studio Webform. I wish obtain various records from...
2
by: Dolorous Edd | last post by:
Hi, for a program I'm working on I need to be able to drag multiple files between Windows Explorer and a ListBox, in both directions. Implementing the "drag in" was pretty easy, but I can't find...
1
by: jjuan | last post by:
I have a multiple dropdown which have multiple selection. If I select multiple value on my multiple dropdown list and submit it,how can i write it on the tblotherlang sample code <select...
2
by: NourB | last post by:
I hope someone can help here; I have two lists, with multiple selection, say one is employees and the other is called members, when names are moved from employees into the other list box they become...
2
LegalIT
by: LegalIT | last post by:
Hello, I have a VB.net application that creates reports from database information. I am able to create the reports fine. The problem is each time I create a report my application starts a new...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.