472,364 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 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 4778
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.