Link to home
Start Free TrialLog in
Avatar of Evan Cutler
Evan CutlerFlag for United States of America

asked on

Pushing MySQL output to PHP Arrays and Sub Arrays

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

Avatar of Nerdwood
Nerdwood
Flag of Australia image

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
Avatar of Evan Cutler

ASKER

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Nerdwood
Nerdwood
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


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
Glad you found something!

You're using AJAX?