Show content based on date

Hi there,

For my company’s info screen, we’d like a birthday calendar. Say I have a div for each employee with the name and a picture, and then only want to show that div if it’s actually their birthday. Here’s what I have now.

<?php if (date('M N') == ("Dec 4")) { ?>
<div id="birthday">
	<div class="birthday_today">
		<h1>Birthday today</h1>
		<img src="images/noname" width="198" height="256" />
		<h2>No Name</h2>
	</div>
</div>
<?php } ?>

The problem is, that I would have to write all this code for each employee instead of just changing the name, image and date for each employee. What would be the most clever way to go around this?

Cheers,
Peter

Essentially, say all of your employees’ data is kept in a single table,


employees
========
id | 23
name | Bob Hoskins
image | 23.jpg
dob | 1990-12-15

You’d fetch those with a birthday today with something like this:


SELECT name, image
FROM employees 
WHERE dob = CURRENT_DATE()
LIMIT 5;  // set a limit here

Then simply loop through the results set outputting to your div, something like this, untested by the way …


// connect to your db
$rows = // the results of that select - if any

// detect if there are any $rows at all ...
if( count($rows) > 0 ){
echo '<div id="birthday"> ';

// now loop through the rows found
foreach( $rows as $row){

echo '
    <div class="birthday_today"> 
        <h1>Birthday today</h1> 
        <img src="images/'.$row['image'].'" width="198" height="256" /> 
        <h2>'.$row['name'].'</h2> 
    </div> ';

}

echo '</div>';
}

Umm are you a PHP programmer? Do you know something about Database (MySQL), etc.? Sorry if you are pretty aware of them all already but from your question it seems you are not familiar about those.

So you have to design a database table where you put the employees information like name, date of birth, image, etc. Then it will be very easier to select which employee has birthday today by querying the database table like:

Select id,empname,dateofbirth,imagename from tbl_employees where dateofbirth=‘2011-12-15’ (today’s date).

How are you storing the data for each person? If it’s coming from a database you could just select those employees whose birthday it is today, but assuming that’s not the case I would say an array would be the simplest way to do it, eg:

$employees = array(
 array('name' => 'Peter', 'img' => 'peter.jpg', 'birthday' => '12-15'),
 array('name' => 'Martin', 'img' => 'martin.jpg', 'birthday' => '06-22')
);

foreach($employees AS $value) {
 if ($value['birthday'] == date('m-d')) {
  echo '<div class="birthday_today">';
  echo '<h1>Birthday today</h1>';
  echo '<img src="images/' . $value['img'] . '" width="198" height="256" />';
  echo '<h2>' . $value['name'] . '</h2>';
  echo '</div>'; 
 }
} 

Thank you so much,

I think I’m getting there. I have this:

<?php					
	$sql = "select * from employees where birthday = currentDate() limit 1";
	$rows = mysql_query($sql);
	if( count($rows) > 0 ){
		echo '<div id="birthday"> '; 
		foreach($rows as $row){ 
			echo ' 
			    <div class="birthday_today">  
			        <h1>Birthday today</h1>  
			        <img src="/images/'.$row['image'].'" width="198" height="256" />  
			        <h2>'.$row['name'].'</h2>  
			    </div> '; 
		}
		echo '</div>'; 
	}
?>

The only thing showing up is the birthday div, which has to mean that there are results. But no images and names. The row names are correct. What am I doing wrong?

martbean: I am in fact storing them in a database, as they’re pulled directly from our company intranet so we don’t have to update several places every time we get a new employee. I have the rows name, image and birthday in my database.

Well, I’m not the greatest programmer of them all, as you can tell. But I do have a database with the names, image paths and birthdays already. I just can’t make the query work.

Ah, I found the solution — I was using currentDate() when the function is actually called current_Date().

facepalm

Cool. You didn’t really allude to that in your original post which is why I assumed you weren’t but a database is the best way to do it.