FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


a grouping question?





d34db0lts
now i'm not sure if this belongs in a mysql section but something tells me it's more of a php question.

so the is the deal:
i have this part in my website that will show shows (concerts) in order of the months by date.

so like all january would be shown in one group and then all february shows would be shown as well...in this fashion:

January:
show1
show2
show3

February:
show4
show5
show6

the problem is that i dont know how to retrieve the data in this format. what i wanted to do is do a while function that loops the months (Jan, Feb) and then inside that loop put another while function that loops all the shows in that month. since this is the first time i do this, i'm basically asking if there's other ways to do this more efficiently. something tells me that there has to be an easier way.

if anybody can help that'd be great. thanks for any insights.
clogfan
You don't specify where your data is. If your data is in a database, like mysql, you can retrieve your data in the right order if you want. In your query (SELECT * FROM concerts WHERE 1 ORDER BY date), you can tell that you want the result ordered (by default ascending) by date. Then you only need a single loop where you check for month changes.

Good luck !
d34db0lts
clogfan wrote:
You don't specify where your data is. If your data is in a database, like mysql, you can retrieve your data in the right order if you want. In your query (SELECT * FROM concerts WHERE 1 ORDER BY date), you can tell that you want the result ordered (by default ascending) by date. Then you only need a single loop where you check for month changes.

Good luck !
sorry for not specifying, but yes it is from a mysql. but the thing is if i do a query, i'll have to do one for each month. if it orders it by month and i only do one query then it wont show a heading which will show the month and then just the dates, does that make sense?

if i do that query it'll output it this way

1/13/06 show1
1/15/06 show2
1/20/06 show3
2/04/06 show4
2/06/06 show5
3/16/06 show6

but that's not what i want, i want so that the month is shown in a header format like this:

January
1/13/06 show1
1/15/06 show2
1/20/06 show3
February
2/04/06 show4
2/06/06 show5
March
3/16/06 show6

is there some way that i can do a query that will give me this? thanks for any inputs.
clogfan
You can do this with a single query.

You can do the split logic in your loop of the result :

Here is the pseudocode :
Code:

currentmonth = null
for each entry in the result
{
  $currentrow = fetch row from result
  $month_of_currentrow = calculate month based on date in $currentrow
  if ($month_of_currentrow != $currentmonth)
  {
    // Put the code to show the month, e.g.
    echo "\n ".$month_of_currentrow
    // Update currentmonth
    currentmonth = month_of_currentrow
  }
  // Put the code to show the date and showname
  echo $currentrow['date']." : ".$currentrow['showname']
}


This should do the job.
Related topics
I Have a question . Is it the server support Chinese?
Question before requesting free hosting
question abt posts/mth
Kinda dumb question regarding DNS
A question
UT2004 U like the game? Do u have any question post here.
Question about DNS!
Question about Domain!
another uni.cc question
Question
Question abt lang. support...
question!!!!!!
A question about Naruto
C++ Question
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.