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

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
}
}
?>

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.