...

View Full Version : Seperating Groups - almost there



Local Hero
04-24-2009, 07:00 PM
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

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.

Fumigator
04-24-2009, 07:38 PM
When you say the column has a list of months, does that mean one row contains a value "Jan, Feb, Jan" or does that mean you have several rows, each containing one month?

Local Hero
04-24-2009, 07:46 PM
I apologize. There are several rows, each containing a month:
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

Fumigator
04-24-2009, 08:48 PM
Since you are using PHP already you can simply ORDER BY month, day and then echo the results. You'll at first get this:



Jan 1
Jan 2
Jan 7
Jan 30
Feb 5
Feb 12
Feb 14
Feb 16


That can be turned into your desired output by only printing the month when it changes. Example:



$previousMonth = "";

for ($i = 0; mysql_num_rows($result); $i++) {
if ($previousMonth != $rows['month']) {
echo $rows['month']."<br />";
$previousMonth = $rows['month'];
}
echo $rows['day']."<br />";
}


I have to wonder why you don't just have a single column of data type "date", which would store your month and day in one column.

Local Hero
04-24-2009, 09:23 PM
Thanks,
The reason for not having the date is that I was trying to understand the answer and wanted an easy way to ask the question. The actual information is Projects and PDF files where multiple PDF files (dates) are associated with the same Project (Month).

I understand what you're trying to do, but I think there is a problem. I'm not sure if it's the code you gave me or something I did, but when I open the page, it is "thinking" indefinatly and never opens. I'll look over my code some more unless you see something that was omitted from your code? We're both in Utah, I should just come and find you with my laptop :p

Thanks for your help, I'll search what I have for mistakes.

Fumigator
04-24-2009, 10:44 PM
Whoops yeah don't assume I've tested and debugged my sample code. The "for" loop condition is wrong, should be $i < mysql_num_rows($result) and inside the loop I forgot to include a call to mysql_fetch_array().

Local Hero
04-25-2009, 03:16 AM
Thanks,
I've been working on this for the past few hours. My biggest problem.....I was using group by instead of order by. Thanks for all the help!


<?php

$previousMonth = "";
for ($i = 0; $i < mysql_num_rows($result); $i++) {
while ($rows = mysql_fetch_array($result)) {

if ($previousMonth != $rows['month']) {
echo $rows['month']."<br />";
$previousMonth = $rows['month'];
}
echo $rows['day']."<br />";
}
}
?>

Fumigator
04-25-2009, 08:43 AM
The "for" loop is useless if you use a "while" loop too. Just take out the "for" loop.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum