The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Explode to array
Discuss Explode to array in the PHP Development forum on Dev Shed. Explode to array PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 5th, 2013, 03:07 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Location: Paris, France
Posts: 15
Time spent in forums: 4 h 38 m 36 sec
Reputation Power: 0
|
|
|
PHP-General - Explode to array
Using GROUP_CONCAT in my SQL I have a comma-separated list of values for each year.
What do I need to now do in PHP to allow those values to be separated by <td>'s
So my query returns something like this:
2013: a,b,c,d,...
2012: b,c,e,f,...
...
I want all of those including the year to be placed into <tr>'s and then separated by <td> with no commas
I've ready about exploding into arrays, but my knowledge of this in PHP isn't quite there yet.
Any ideas?
|

February 5th, 2013, 03:39 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
don't misuse GOUP_CONCAT for fetching groups of values. Fetch normal rows (ordered by the date), and whenever the date changes, add a </tr><tr>. If you don't have too many dates, you could also fetch the values separately for each date.
|

February 5th, 2013, 06:43 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Location: Paris, France
Posts: 15
Time spent in forums: 4 h 38 m 36 sec
Reputation Power: 0
|
|
|
Yeah I didn't think that GROUP_CONCAT was what I really needed, but the reason I went to that was because I wasn't sure how to do precisely what you suggested.
How do I make it so that a new row is formed once the year changes?
|

February 5th, 2013, 06:58 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Make a variable which holds the last date. And then check it on each iteration:
PHP Code:
$last_date = '';
foreach ($values as $value) {
if ($value['date'] != $last_date) {
$last_date = $value['date'];
echo '</tr><tr>';
}
...
}
|

February 5th, 2013, 11:10 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Location: Paris, France
Posts: 15
Time spent in forums: 4 h 38 m 36 sec
Reputation Power: 0
|
|
Thanks, one more question though: how do I get the year to show up in the first <td> of each <tr>
Here's what I have:
PHP Code:
$last_date = '';
while($row = mysql_fetch_assoc( $result ))
{
if (0 == ($i++ & 1))
{
$sClass = 'odd';
}
else
{
$sClass = 'even';
}
if ($row['Year_GG'] != $last_date) {
$last_date = $row['Year_GG'];
echo '</tr><tr class="'.$sClass.'">';
}
printf('
<td>%s</td>' . PHP_EOL, $row['LName']);
}
|

February 5th, 2013, 02:08 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Location: Paris, France
Posts: 15
Time spent in forums: 4 h 38 m 36 sec
Reputation Power: 0
|
|
|
scratch that, found it.. was easier than I had thought..
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|