MySpaza - How To
Web Design Tips and PC and Linux Related Information

Subscribe
 



    follow me on Twitter

    Generate Date Images using PHP and Array() 's
    generate date images using php arrays mysql css

    Added: 2008-05-13 12:16:17

    Have you ever wanted to use images to generate links, thus allowing you to use any font or style you like?? In this example I will show you a simple way using a php function, an archive of images and some arrays.

    First step in the script we have the CSS, very simple I have gone for a border at the bottom of my images:

    <style type="text/css">
     .imgTxtRow {
      width:200px;
      border-bottom:1px solid #000;
      }
    </style>

    Then moving on to the PHP arrays and function:

    <? 
     // month image array()
     $months = array(1=>"january0.png",
         "february0.png",
         "march0.png",
         "april0.png",
         "may0.png",
         "june0.png",
         "july0.png",
         "august0.png",
         "september0.png",
         "october0.png",
         "november0.png",
         "december0.png");
        
     // year image array()
     $years = array("00"=>"000.png",
         "01"=>"010.png",
         "02"=>"020.png",
         "03"=>"030.png",
         "04"=>"040.png",
         "05"=>"050.png",
         "06"=>"060.png",
         "07"=>"070.png",
         "08"=>"080.png",
         "09"=>"090.png",
         "10"=>"100.png",
         "11"=>"110.png",
         "12"=>"120.png",
         "13"=>"130.png",
         "14"=>"140.png",
         "15"=>"150.png",
         "16"=>"160.png",
         "17"=>"170.png",
         "18"=>"180.png",
         "19"=>"190.png",
         "20"=>"200.png");
     
     // MAKE DATE FUNCTION;
     function makedates($date) {
      // global arrays $years and $months
      global $years;
      global $months;
      // split $date into $month and $year
      list($month,$year) = split("-",$date);
      // make $output
      $output = '<div class="imgTxtRow"><img src="images/'.$years[$year].'" alt="'.$year.'" height="15px" />';
      $output .= '<img src="images/'.$months[$month].'" alt="'.$year.'" height="15px" /></div>
      ';
      // return
      return $output;
      }   

                //TESTING TESTING 123
       print makedates('2-08');
       print makedates('1-08');
       print makedates('12-07');
       print makedates('11-07');
       print makedates('10-07');
       print makedates('9-07');
       print makedates('8-07');
       print makedates('7-07');
       print makedates('6-07');
       print makedates('5-07');
       print makedates('4-07');
       
       
       
       // mock mysql query - WORKING
       
       /*$query = mysql_query("SELECT DISTINCT DATE_FORMAT(startdate,'%c %y') FROM blog ORDER startdate DESC");*/
    ?>

    The above print makedates() will produce a line with the date as provided within ''.

    This script can also be used to produce dates from mysql data.

    i.e.

    <?

     $query = mysql_query("SELECT DISTINCT DATE_FORMAT(startdate,'%c-%y') FROM blog ORDER startdate DESC");

    while($row=mysql_fetch_array($query)) {

    print makedates($row['startdate']);

    ?>

    To download the files for this tutorial click here

    Enjoy.

     




    Add a comment to this post
    Email confirmation is required for each post

    Name:
    Email:
    Comment:
     

    Comments from other users:




    Copyright MySpaza.Co.Uk - 2010