473,730 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array (multidimension al) help - using array like SQL tables to get round lack of Union in MySQL3.

Hi there.

Because of the lack of a Union query in MySQL 3 I have decided to take the
approach where I populate two arrays with values from similar tables in DB.
In this case they are `courses` and `lessons`

Lets say Courses has fields
CourseID(PK), Date, Name, RunBy, Status

The lessons table is related to the Courses table in that a Course can have
a number of lessons and a lesson is always part of a course.
Lessons has fields
LessonID(PK), CourseID (FK), Date, Name, Status

and therefore I do two SQL calls which fills two arrays in PHP.
So array $arrayCourses might contain :
[0] = Array("CourseID " => "12", "Date" => "12/12/03","Name" =>"Physics",
"Status" => "Active")
[1] = Array("CourseID " => "14", "Date" => "14/12/03","Name" =>"English",
"Status" => "Active")
[2] = Array("CourseID " => "15", "Date" => "14/12/02","Name" =>"Latin",
"Status" => "Closed")
....etc.

So array $arrayLessons might contain :
[0] = Array("CourseID " => "12", "Date" => "18/12/03","Name" =>"Physics -
Triangles", "Status" => "Active")
[1] = Array("CourseID " => "12", "Date" => "19/12/03","Name" =>"Physics -
Circles", "Status" => "Active")
[2] = Array("CourseID " => "14", "Date" => "15/12/03","Name" =>"English -
Literature", "Status" => "Active")
[3] = Array("CourseID " => "15", "Date" => "15/12/02","Name" =>"Latin -
nouns", "Status" => "Closed")
....etc.

I now want to combine (add one to the bottom of the other) these two arrays
(As the fields are the similar) and know I can do it (I believe) by just
adding the arrays together.
So
$CombinedArray = $arrayCourse + $arrayLessons;
would give me an array containing 7 elements in the above example (although
each element is another array).

However, I want the newly formed $CombinedArray to be so that the main
elements are ordered by the courseID. So rather then the elements of
$arrayLessons being after those of $arrayCourse, I want the elements to
appear in an order defined by the value held in courseID.

How can I achieve this? It is very complicated. With a Union query in SQL it
would be easier to do this I think but I am do not want to have to learn the
lack of Union work around.

Further to this. Lets say one of the arrays like $arrayLessons had an extra
element (e.g. number attending) that was not in $arrayCourses what would
happen to that element?

Thanks in advance for any help.

Kind regards

Dave


Jul 17 '05 #1
2 4899
Dave Smithz wrote:
Hi there.
<SNIP>


A better alternative might be to just have a nested SQL query... eh.
$listing = array();
$sql = 'SELECT * FROM COURSES';

// insert the mysql_query stuff here. left out for this post

while($row = mysql_fetch_ass oc($result))
{
$listing[] = $row;
// or maybe echo '<h1>'.$row['name'].'</h1>' etc.. here instead?
// NOTE THE '_' prepended to the variable names here - don't //
forget :)
$_sql = 'SELECT * FROM LESSIONS WHERE COURSE_ID = '.
$row['course_id'] .' ORDER BY name DESC';

// NOTE THE '_' prepended to the variable names here - don't //
forget :)
// insert the mysql_query stuff here. left out for this post
while($_row = mysql_fetch_ass oc($_result))
{
$listing[] = $_row;
// or maybe echo '<b>'.$row['name'].'</b>' etc.. ?

}
}
Should give you what you want... Although if you do this, you can
probably just print it out directly without having to use an array at all.

Jul 17 '05 #2
"Dave Smithz" <SPAM FREE WORLD> wrote:
I now want to combine (add one to the bottom of the other) these two
arrays (As the fields are the similar) and know I can do it (I believe) by
just adding the arrays together.
So $CombinedArray = $arrayCourse + $arrayLessons;
No, see http://www.php.net/manual/en/languag...tors.array.php
Only elements from the 2nd array with keys that do not exist in the 1st
array are appended. Use array_merge() in stead. See
http://php.net/arraymerge
However, I want the newly formed $CombinedArray to be so that the main
elements are ordered by the courseID.


http://php.net/usort , see example 2.

--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3

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

Similar topics

15
2457
by: M.Siler | last post by:
<HTML> <HEAD> <TITLE></TITLE> <SCRIPT> <!-- var factor_val = new Array(8,7) factor_val = 68.8 factor_val = 55
10
5635
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I create a single View and specify also in this View the WHERE clause that is common in these stored procedures, I will have the new stored procecures changed to be like:
20
4716
by: Pavel Stehule | last post by:
Hello, Is possible merge two arrays like array + array => array select array_append(array, array); ERROR: function array_append(integer, integer) does not exist
9
2312
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
3
2542
by: Eric Laberge | last post by:
Aloha! I've been reading the standard (May '05 draft, actually) and stumbled across this: 6.7.1 Initialization §20 "If the aggregate or union contains elements or members that are aggregates or unions, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the...
8
1826
by: ais523 | last post by:
I've checked the FAQ for this and couldn't find the answer. Is the following code snippet portable? int a; a=6; printf("%d\n",(*a)); This prints "6" on my compiler. I've been told it's always legal, but it seems slightly suspect to me. Can you really go out-of-bounds like this on one of the 'sub-arrays' a, a, etc.?
8
6400
by: Skay | last post by:
Someone challenged me to create a dynamic multidimensional array using auto_ptr http://www.gotw.ca/gotw/042.htm Provides a good way to create a single dimensional array using an adapter. However I am unable to come up with a way to create and access an array for more than 1-dimension. Any one up for the challenge?
2
2364
by: phattymatty | last post by:
I am having trouble getting this loop to do what I would like it to. I need to display information from a multidimensional array in order. The order has already been sorted in the array using a usort. How do I display this information the correct way? I previously used mysql_fetch_assoc and returned stored values of win, ties, and losses. Now I have written a function which evaluates each game and determines winners and losers and ties and...
9
4499
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize function: x = new int;
0
8770
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
9301
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...
0
9176
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8184
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...
0
4543
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4801
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3257
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
2
2713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2175
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.