comprx Posted June 26, 2009 Share Posted June 26, 2009 I am attempting to pull an array out of a mysql table field. That much I have succeded at with the use of the implode and explode functions. BUT how do I check for a value within the array? Now I know that doesn't make a lot of sense so I am going to attempt to illustrate the problem Table: locations: id | Name | category | subcategories The location would belong to a parent category and any number of subcategories so I implode the subcategories and write that to the "subcategories" field. Then when i need it again I explode it again. but lets say I want to do something like: for each location check to see if it is in subcategory "9" (as an example) How do i take the newly built array and loop through it to see if it is in "9"? <?php $subcategories = "bar grill fastfood 9"; $pieces = explode(" ", $subcategories); foreach( $pieces as $key => $value) { echo $value." "; } ?> This will give me each value on the screen but how do i check to see if it has "9" in it (which this one does) Link to comment https://forums.phpfreaks.com/topic/163723-ifthen-within-foreach-loop/ Share on other sites More sharing options...
xtopolis Posted June 26, 2009 Share Posted June 26, 2009 First, you shouldn't have this problem, it seems your database isn't properly normalized: http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html Secondly, you could use stristr to search for a string in a string. foreach ($pieces as $subcat) { if(stristr($subcat, '9')) { echo 'Found a 9.'; } } Or perhaps in_array. if ( in_array('9', $pieces) ) { echo 'I found a 9.'; } Didn't test these as your database data should be handled differently if possible. Link to comment https://forums.phpfreaks.com/topic/163723-ifthen-within-foreach-loop/#findComment-863882 Share on other sites More sharing options...
comprx Posted June 26, 2009 Author Share Posted June 26, 2009 Handled differently how? I would love to know how to make this all easier. Also your answer was somewhat confusing but I think i got the gist of it. you are telling me I should make a seperate table for the subcategories and then yet another table for location_subcategories so I have these tables: location ID|Name|address|ect| categories ID|Name| subcategories ID|name| location_categories Location_id|category_id and lastly location_subcategories location_id|subcategory_id this makes some sense as far as keeping table fields free from arrays, is this what you were referring to? and if so then how would I then display the names of all locations within category 7 and subcategories 8 and 9?? as an example? Link to comment https://forums.phpfreaks.com/topic/163723-ifthen-within-foreach-loop/#findComment-863888 Share on other sites More sharing options...
xtopolis Posted June 26, 2009 Share Posted June 26, 2009 I am not exactly sure of what you're doing, so I can only speculate. You should not however store data in mysql in the "CSV" type format (you used space delimited instead of comma). It depends on how categories and subcategories correlate I think, but what you have is a better breakdown than your current storage format. I'm having a hard time focusing, but I would think that a category would have certain subcategories, rather than a location having separate subcategories. To start you off, a sample JOIN would be: SELECT L.Name FROM location L JOIN location_categories LC ON(L.ID = LC.Location_id) WHERE LC.category_id = 7 You can further join once you determine the subcategory setup. Link to comment https://forums.phpfreaks.com/topic/163723-ifthen-within-foreach-loop/#findComment-863895 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.