?
Solved

Pushing MySQL output to PHP Arrays and Sub Arrays

Posted on 2008-06-08
7
Medium Priority
?
904 Views
Last Modified: 2013-12-13
Below is a sequence in PHP loading a series of arrays and sub arrays.

I need to do the same thing but using an output from MySQL.
I know how to fire the MySQL statements into a regular array (Such as $specialty below)
but what I don't know is how to load the sub arrays.

Any ideas?
Thanks.
$specialty = array('air','water','land');
$location = array(
        'air'=>array('plane','helicopter','air-baloon'),
        'water'=>array('boat','watersky','submarine'),
        'land'=>array('motorcycle','car')
      );
$Names = array(
        'plane'=>array('Boeing707', 'F-16', 'Columbia'),
        'helicopter'=>array('BayWatch Heli', 'Police Heli'),
        'air-baloon'=>array('Zeppelin'),
        'boat'=>array('Baywatch Safeguard Boat', 'JetBoat'),
        'watersky'=>array('JetSky','Board-Sky'),
        'submarine'=>array('K-19'),
        'motorcycle'=>array("Yamaha", "Harley Davidson", "Ducati"),
        'car'=>array("Hummer", "Renault", "Porsche")
      );

Open in new window

0
Comment
Question by:Evan Cutler
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:Nerdwood
ID: 21738778
Hi

What form is the data in when it's pulled from the DB? What's the query like?

You should be able to just you a couple of nested for each loops or something to assign all the appropriate values.

Michael
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 21738912
I haven't decided yet.
I was probably going to do a nested query string.

I know how to put the query into an array:

while($row=mysql_fetch_assoc($SQLreturn1))
{
$specialty[]=$row
}

But I don't know how to take that $row and add another array to it.  It appears to be a recursive thing, but I haven't figured out the logic.
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 21738917
I can also just do a left join SQL Query.

My problem isn't the data, it's the format on the nested array.
Thanks.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 7

Accepted Solution

by:
Nerdwood earned 2000 total points
ID: 21757809
Hi

Sorry I didn't reply for a while! :P

How's the data stored in your DB? Just trying to get a picture of what it looks like when it gets pulled out... what are field names, values, etc.

Michael
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 21759902
Three Tables:  Specialties, Locations, Physicians.

Physician has a FK => LocationID (Matches Locations.PK):  Data Needed is Physician.Name
Locations has a FK => SpecialtyID (Matches Specialty.PK): Data Needed is Location.ZIPCODE
Specialty Data Needed is Specialty.Specialty_Name

This is going to a Cascading dropdown that I found, and it uses nested arrays loaded at runtime.
I am trying to figure out how to load the arrays.

The three arrays are (of course) specialty, Location, and Physician.

The specialty array just has a list of specialties from the Specialty Table.  Did this.

Working on the second one now: Locations.

Locations contains the same array of specialties, however, each specialty in this array is in itself an array containing the zip codes.

The Name has the array of zip codes, with the doctors names as the list of sub arrays.

I can program the initial list of arrays, but where I'm stuck is how to array_Push into the sub arrays, while placing the arrays into the variable.

Thanks


0
 
LVL 9

Author Closing Comment

by:Evan Cutler
ID: 31465153
I found a solution where I can do this, plus it gave me the ability to push it to a drop down, which is what I wanted in the first place.
Here's the link:
http://www.fornewbie.com/node/44
0
 
LVL 7

Expert Comment

by:Nerdwood
ID: 21787549
Glad you found something!

You're using AJAX?
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question