Jump to content

Recommended Posts

Lets say I have a table with 1 column having a list of months:

Jan, Jan, Feb, Jan, Feb, Feb, Jan, Feb

And another column having a list of dates associated with those months:

1,2,5,7,14,12,30,16

It looks like this:

Jan | 1

Jan | 2

Feb | 5

Jan | 7

Feb | 14

Feb | 12

Jan | 30

Feb | 16

 

I want to view the results like this:

Jan

1

2

7

30

 

Feb

5

14

12

16

 

Here is what I'm working with:

<?php
$sql="SELECT month, day FROM calendar GROUP BY month";
$sql2="SELECT month, day FROM calendar ORDER BY month";
$result=mysql_query($sql);
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result)){
?>
<table><tr><td>
<?php echo( $rows['month'] ); ?>
</td></tr><tr><td>
<?php
while($rows2=mysql_fetch_array($result2)){
echo $rows2['day'] ;?>

}
?>
</td></tr></table>
<?php
}
?>

I'm getting 2 tables - Jan & Feb, but all the dates go in Jan. I can see why it's doing that. I can't figure out how to get the Feb dates over in the Feb table. Thanks for any suggestions or direction.

Link to comment
https://forums.phpfreaks.com/topic/155532-seperating-groups-almost-there/
Share on other sites

I think by using Group Concat you could achieve a dataset that looks like this:

[pre]

Month | Days     

Jan  | 1,2,7,30

Feb  | 5,14,12,16

[/pre]

 

Perhaps that version would be helpful, since you can split the dates using explode.  Be aware of the limitations of group concat cited in the manual however.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.