#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Paris, France
    Posts
    15
    Rep Power
    0

    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?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Paris, France
    Posts
    15
    Rep 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?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    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>';
        }
        ...

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Paris, France
    Posts
    15
    Rep 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 (
    == ($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']); 

  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Paris, France
    Posts
    15
    Rep Power
    0
    scratch that, found it.. was easier than I had thought..

IMN logo majestic logo threadwatch logo seochat tools logo