ejscottb Posted February 20, 2011 Share Posted February 20, 2011 I need to explode data separated with text separators, like this: banana|carrot|salmon|oak|tuna|apple|elm|pineapple So far so good with this: <?php $items = explode('|', $stuff); foreach($items as $key=>$value){ ?> But now I need a list, with categories from another table (column1=items, column2=corresponding categories) and order by categories (not worried about the order of the items under each category), like this: FISH salmon tuna FRUIT banana apple pineapple TREE oak elm VEGETABLE carrot Any suggestions on an approach would be appreciated. I'm foggy about creating arrays, but I think I need one here? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2011 Share Posted February 20, 2011 If this: banana|carrot|salmon|oak|tuna|apple|elm|pineapple is in a field in a database, what you need is a better database structure, not an array. Quote Link to comment Share on other sites More sharing options...
ejscottb Posted February 20, 2011 Author Share Posted February 20, 2011 I AGREE! Unfortunately, I've inherited this database and the previous programmer has set it up this way. Whoa is me. Just wondering if there's a solution before I start restructuring tables/data. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2011 Share Posted February 20, 2011 I don't see how you could write anything practical that would be able to categorize the explode()d string. I certainly can't think of any way for php or mysql to be able to know if 'salmon' should be put under 'FISH' or 'BULLDOZERS'. Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 20, 2011 Share Posted February 20, 2011 ok ok couldn't resist... salmon best grilled then placed over a bed of wild rice and under a honey dejon sauce; side of steamed garlic fused asparagus, and a glass of Pouilly Fuissé Quote Link to comment Share on other sites More sharing options...
ejscottb Posted February 21, 2011 Author Share Posted February 21, 2011 Possibly use 'usort' array command to order list? This works to order items under one category heading: while ($row_mystuff = mysql_fetch_array($mystuff)) { if ($current_variable != $variable) { echo $variable; $current_variable = $variable; } echo $other_variable; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.