SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Essentially, say all of your employees' data is kept in a single table,

    Code:
    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:

    Code sql:
    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 ...

    PHP Code:
    // connect to your db
    $rows // the results of that select - if any

    // detect if there are any $rows at all ...
    if( count($rows) > ){
    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>';


  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    $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>'
     }

    Martin.

  5. #5
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much,

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

    PHP Code:
    <?php                    
        $sql 
    "select * from employees where birthday = currentDate() limit 1";
        
    $rows mysql_query($sql);
        if( 
    count($rows) > ){
            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?

  6. #6
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raju Gautam View Post
    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:
    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.

  8. #8
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I found the solution — I was using currentDate() when the function is actually called current_Date().

    *facepalm*

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Surrey, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pshoeg View Post
    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.
    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.
    Martin.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •