473,395 Members | 1,574 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,395 software developers and data experts.

Array (multidimensional) 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 4867
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_assoc($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_assoc($_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
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
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...
20
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
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
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...
8
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...
8
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. ...
2
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...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.