[PHP] Implode? Explode?
Hi all, I'm trying to fetch similar things - in this case, rows which share a series ID no - and bring them into an array and display them grouped by what makes them similar (in this case, series id). I looked at implode and explode which seem wrong for this - the only separator I can see is that they're each in a table cell. The result of my query returns something like: art_id series_id art_titleseries_name 2 1 Weddings 2004Summer Special 4 1 Summer In The City Summer Special 5 2 Bags of NY Op-Art 7 2 Dogs of NY Op-Art I'd like to create a list of links to each art_id grouped together as series, href code obviously not complete but demonstrative: pSummer Specialbr / a href='2'Weddings 2004/a | a href='4'Summer In The City/p pOp-Artbr / a href='5'Bags of NY/a | a href='7'Dogs of NY/p ? Thanks in advance, Jack -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Implode? Explode?
You're close, but you need to group your data on the series_id first. You can do that by looping through your array and creating another array using series_id as the array key. You'll end up with a list of arrays named after the series_id. foreach( $result_set as $result_item ) { //Add an array item to the series_id slot of the grouped_result array $grouped_result[ $result_item['series_id'] ][] = array('id'=$result_items['art_id'], 'title'=$result_item['art_title']); } Now you have your data grouped by series_id. Each item of the grouped_result array will contain one or more arrays which contain the art_id and art_title. If you are not familiar with multidimensional arrays, this may be a little confusing. On Jun 6, 2005, at 8:32 AM, Jack Jackson wrote: Hi all, I'm trying to fetch similar things - in this case, rows which share a series ID no - and bring them into an array and display them grouped by what makes them similar (in this case, series id). I looked at implode and explode which seem wrong for this - the only separator I can see is that they're each in a table cell. The result of my query returns something like: art_id series_id art_titleseries_name 2 1 Weddings 2004Summer Special 4 1 Summer In The City Summer Special 5 2 Bags of NY Op-Art 7 2 Dogs of NY Op-Art I'd like to create a list of links to each art_id grouped together as series, href code obviously not complete but demonstrative: pSummer Specialbr / a href='2'Weddings 2004/a | a href='4'Summer In The City/p pOp-Artbr / a href='5'Bags of NY/a | a href='7'Dogs of NY/p ? Thanks in advance, Jack -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Brent Baisley Systems Architect Landover Associates, Inc. Search Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Implode? Explode?
Brent Baisley wrote: You're close, but you need to group your data on the series_id first. You can do that by looping through your array and creating another array using series_id as the array key. You'll end up with a list of arrays named after the series_id. foreach( $result_set as $result_item ) { //Add an array item to the series_id slot of the grouped_result array $grouped_result[ $result_item['series_id'] ][]= array('id'=$result_items['art_id'], 'title'=$result_item['art_title']); } That did the trick, Brent. Thanks so much for this useful information. Now you have your data grouped by series_id. Each item of the grouped_result array will contain one or more arrays which contain the art_id and art_title. If you are not familiar with multidimensional arrays, this may be a little confusing. A little is right! But tanks for the intro to multi-dimensional arrays! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Implode? Explode?
On Mon, June 6, 2005 5:32 am, Jack Jackson said: I'm trying to fetch similar things - in this case, rows which share a series ID no - and bring them into an array and display them grouped by what makes them similar (in this case, series id). I looked at implode and explode which seem wrong for this - the only separator I can see is that they're each in a table cell. $query = select series_id, ... from ... ; $query .= ORDER BY series_id, ... ; $art = mysql_query($query) or die(mysql_error()); $series = array(); while (list($series_id, ...) = mysql_fetch_row($art)){ $series[$series_name][] = a href=$art_id$art_title/a; } while (list($name, $links) = each($series)){ echo p$namebr /, implode( | , $links), /p\n; } The result of my query returns something like: art_id series_id art_titleseries_name 2 1 Weddings 2004Summer Special 4 1 Summer In The City Summer Special 5 2 Bags of NY Op-Art 7 2 Dogs of NY Op-Art I'd like to create a list of links to each art_id grouped together as series, href code obviously not complete but demonstrative: pSummer Specialbr / a href='2'Weddings 2004/a | a href='4'Summer In The City/p pOp-Artbr / a href='5'Bags of NY/a | a href='7'Dogs of NY/p You could actually use: ORDER BY series_date, series_name, series_id, art_id, art_title, art_id or something like that, so long you are sorting first and foremost by the SERIES information, and any other sorting happens AFTER all series_* fields. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php