ricky spires Posted January 10, 2013 Share Posted January 10, 2013 Hello . im having trouble with a loop. i want the title to only appear ones and have the items listed under each title but it seems to be echoing the title more than once. i want to output something like this: Category1 item1 Category2 item1 item2 etc... but im getting this: Category1 item1 Category2 item1 item2 Category2 item1 item2 in my database i have this: id - book_id - cat - item 1 - 1 - cat1 - item1 2 - 1 - cat2 - item1 3 - 1 - cat2 - item2 my loop looks like this: //this part finds the categories by book id. $book_id = "1"; $bookCats = Categories::find_by_book($book_id); //this part finds the books categories foreach($bookCats as $bookCat){ $cat_name = $bookCat->category; echo $cat_name; //then in put the rest of the code here..... } this is how part of the class looks public static function find_by_book($book_id) { return self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE book_id={$book_id}"); } the above code gives me: cat1 cat2 cat2 i want it to only give me cat1 cat2 thanks ricky Link to comment https://forums.phpfreaks.com/topic/272954-please-could-you-help-me-with-a-loop/ Share on other sites More sharing options...
Barand Posted January 10, 2013 Share Posted January 10, 2013 Book_id "1" is in all three records, cat1/cat2/cat2. Try SELECT DISTINCT cat FROM .... Link to comment https://forums.phpfreaks.com/topic/272954-please-could-you-help-me-with-a-loop/#findComment-1404717 Share on other sites More sharing options...
ricky spires Posted January 10, 2013 Author Share Posted January 10, 2013 thanks . looks like a good direction to investigate. p.s. there could be any number of book id's i.e: id - book_id - cat - item 1 - 1 - cat1 - item1 2 - 1 - cat2 - item1 3 - 1 - cat2 - item2 4 - 2 - cat1 - item1 5 - 3 - cat1 - item1 6 - 3 - cat2 - item2 Link to comment https://forums.phpfreaks.com/topic/272954-please-could-you-help-me-with-a-loop/#findComment-1404718 Share on other sites More sharing options...
ricky spires Posted January 10, 2013 Author Share Posted January 10, 2013 Yipppeeeee. It worked:) public static function find_by_book($book_id) { return self::find_by_sql("SELECT DISTINCT cat FROM ".self::$table_name." WHERE book_id={$book_id} group by cat"); } Link to comment https://forums.phpfreaks.com/topic/272954-please-could-you-help-me-with-a-loop/#findComment-1404719 Share on other sites More sharing options...
The Letter E Posted January 15, 2013 Share Posted January 15, 2013 Yipppeeeee. It worked:) public static function find_by_book($book_id) { return self::find_by_sql("SELECT DISTINCT cat FROM ".self::$table_name." WHERE book_id={$book_id} group by cat"); } FYI, SELECT DISTINCT, is much higher overhead and should be avoided if possible, especially on large (hundres of thousands of records) databases; Make sure to run "EXPLAIN" on your sql to check for any performance hits, like using temporary and using filesort. Link to comment https://forums.phpfreaks.com/topic/272954-please-could-you-help-me-with-a-loop/#findComment-1405749 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.