User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » PHP/MySQL - create array from query w/ diff. array Page [1]  
vertigo
Veteran
135 Posts
user info
edit post

I have two tables - one has a list of users' assigned colors and another has a list of shapes associated with each color. When a user logs in, I want to get an array of their colors and then generate an array of their shapes based on their colors. So I would be doing a PHP/MySQL search using an array as WHERE criteria and generating an array from the results.

+------|-------+
| user | color |
+------|-------+
| 11 | 67 |
| 11 | 72 |
| 12 | 89 |
| 13 | 89 |
+------|-------+
+-------|-------+
| color | shape |
+-------|-------+
| 67 | 34 |
| 67 | 35 |
| 67 | 49 |
| 72 | 56 |
| 72 | 57 |
| 89 | 39 |
+-------|-------+
So when user 11 logs in, array_colors would be populated with 67 and 72. Then it would query the second table to create an array_shapes populated with 34, 35, 49, 56, and 57.

Suggestions as to how I would do this? Thanks for the help!

[Edited on February 1, 2010 at 3:45 PM. Reason : sample tables]

2/1/2010 3:43:43 PM

Novicane
All American
15413 Posts
user info
edit post

I would use a left join of some sort to bring the tables together.

this is probably way off base but i think it would look something like this


SELECT user
,(SELECT color FROM colortable LEFT JOIN color.usertable ON color.shapetable = colorID
WHERE user = color ') AS color_array
,(SELECT shape FROM colortable LEFT JOIN color.usertable ON color.shapetable = colorID
WHERE user = color ') AS array_shapes

FROM usertable ORDER BY color ASC


ps there is nice mysql thread already. might want to find it.

[Edited on February 1, 2010 at 3:57 PM. Reason : ignore the name syntax.]

2/1/2010 3:56:02 PM

Golovko
All American
27023 Posts
user info
edit post

you can do this with just mysql.

select color.user, color.color, shape.shape FROM color, shape WHERE color.user = 11 AND shape.color = color.color

something like that, I just assumed your table names with color (for user/color) and shape for (color/shape)

^that works too

[Edited on February 1, 2010 at 3:57 PM. Reason : .]

2/1/2010 3:56:28 PM

vertigo
Veteran
135 Posts
user info
edit post

Oh, sorry for creating a new thread. I had forgotten about the other one. Thanks for the help!

2/1/2010 7:12:16 PM

 Message Boards » Tech Talk » PHP/MySQL - create array from query w/ diff. array Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.