Jump to content

Seperating Groups - almost there


Local Hero

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.