Jump to content

[SOLVED] SELECT FROM table WHERE date_month = month... question..


Recommended Posts

I'm wanting to get all of the rows from the table events and displayed them chronologically by month.

 

Example:

January

Ev 1

Ev 2

 

April

Ev 4

Ev 5

 

etc.

 

I only want the month "category" to show up IF there is an event in it.

 

Here's my problem. The dates of the events are stored as a unix timestamp in the DB. How can I loop through an array of months ($months = array("jan","feb" etc..)) and do an SQL query for each one?

 

Here's what it would look like:

<?php
$months = array("jan","feb","mar");

foreach ($months as $key)
{
  $sql = mysql_query("SELECT * FROM events WHERE DATE_MONTH_NAME(ev_date)='{$key}'");

  while ($row = mysql_fetch_array($sql))
  {
    // do stuff
  }
}
?>

 

Hope im making sense. Thanks

Link to comment
Share on other sites

Ok, i solved it. Here's my code for anyone who has this problem.

 

<?php
foreach ($months as $key)
{
       	$query = mysql_query("SELECT * FROM events WHERE FROM_UNIXTIME(ev_date,'%M')='{$key}' ORDER BY ev_date ASC LIMIT 16",$dbh) or die("ERR: ".mysql_error());

while($arr = mysql_fetch_array($query))
{
	// do stuff
}
}
?>

Link to comment
Share on other sites

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.