SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Tulsa
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extending the value of an array

    Hey guys,

    I'm developing an event calendar that I am going to port into a Joomla component but I'm having a little bit of trouble. I am a bit of a noob with PHP.

    I'm using Keith Devens PHP Calendar 2.3 to generate the calendar itself. That works great. The part that I'm having trouble with is getting multiple events to show up on a single day.

    The PHP Calendar uses an array to list which day's have information in them. I've got a script that generates the day array from the MySQL database. The only problem is that if a specific day has multiple events, it will only show the last event for the day.

    The day array is composed like this array = (day1 = array (content, eventid), day2 = array (content, eventid), and so on.

    Here is the code for the calendar:

    PHP Code:
    function generate_calendar($year$month$days = array(), $day_name_length 3$month_href NULL$first_day 0$pn = array()){
        
        
    $first_of_month gmmktime(0,0,0,$month,1,$year);
        
    #remember that mktime will automatically correct if invalid dates are entered
        # for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
        # this provides a built in "rounding" feature to generate_calendar()

        
    $day_names = array(); #generate all the day names according to the current locale
        
    for($n=0,$t=(3+$first_day)*86400$n<7$n++,$t+=86400#January 4, 1970 was a Sunday
            
    $day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name

        
    list($month$year$month_name$weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
        
    $weekday = ($weekday $first_day) % 7#adjust for $first_day
        
    $title   htmlentities(ucfirst($month_name)).'&nbsp;'.$year;  #note that some locales don't capitalize month and day names

        #Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
        
        
    @list($p$pl) = each($pn); @list($n$nl) = each($pn); #previous and next links, if applicable
        
        
    if($p$p '<span class="calendar-prev">'.($pl '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' $p).'</span>&nbsp;';
        
        if(
    $n$n '&nbsp;<span class="calendar-next">'.($nl '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' $n).'</span>';
        
        
    $calendar '<table class="calendar" width="100%">'."\n".
            
    '<caption class="calendar-month">'.$p.($month_href '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' $title).$n."</caption>\n<tr>";

        if(
    $day_name_length){ #if the day names should be shown ($day_name_length > 0)
            
            #if day_name_length is >3, the full name of the day will be printed
            
    foreach($day_names as $d)
                
    $calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length substr($d,0,$day_name_length) : $d).'</th>';
            
    $calendar .= "</tr>\n<tr>";
        }

        if(
    $weekday 0$calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>'#initial 'empty' days
        
        
    for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month$day++,$weekday++){
            if(
    $weekday == 7){
                
    $weekday   0#start a new week
                
    $calendar .= "</tr>\n<tr>";
            }
            
            if(isset(
    $days[$day]) and is_array($days[$day])){
                list(
    $content$eventid) = $days[$day];
                
    //if(is_null($content))  $content  = $day;
                
    $calendar .= '<td valign="top" width="14%">'.$day.'<br />'.$content.'</td>';
            }
            else 
    $calendar .= '<td valign="top" width="14%">'.$day.'</td>';
        }
        if(
    $weekday != 7$calendar .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>'#remaining "empty" days

        
    return $calendar."</tr>\n</table>\n";

    Here is the code that generates the days array:

    PHP Code:

    //Generate the Day Array
    function getDays($greq_year$greq_month){
        
    $days = array();
        
    $query_days "SELECT a.EventID, d.Title, d.Type, DateBegin, DateEnd, DAYOFMONTH(DateBegin) As event_day, YEAR(DateBegin) AS event_year, MONTH(DateBegin) AS event_month FROM jos_reg_events AS a LEFT JOIN jos_reg_descriptions AS d on d.DescriptionID = a.DescriptionID WHERE MONTH(DateBegin) = $greq_month";
        
    $rs_days mysql_query($query_days) or die(mysql_error());
        while(
    $events mysql_fetch_array($rs_days)){
                    
                
    $link =  'description.php?eventid='.$events['EventID'];
                
    $class $events['Type'];
                
    $title $events['Title'];
                
    $eventid $events['EventID'];
                
                
    //Generate the HTML to go with the event
                
    $content '<span class="'.$class.'"><a href="'.$link.'">'.$title.'</a></span><br /><br /><a href="form.php?EventID='.$eventid.'">Register Now!</a><br />';    
        
                
    // Cope with no end date set i.e. set it to same as start date
                
    if  (($events['DateEnd'] == '0000-00-00') or (is_null($events['DateEnd'])))
                {
                    
    $eyear $events['event_year'];
                    
    $emonth $events['event_month'];
                    
    $eday $events['event_day'];
            
                }
                else
                {
                    list(
    $eyear$emonth$eday) = explode('-'$events['DateEnd']);
                }
                
                
    // The two cases for roll over the year end with an event that goes across the year boundary.
                
    if ($greq_year $eyear
                {
                    
    $emonth $emonth 12
                }                    
                if (
    $events['event_month'] < $greq_year
                {
                    
    $events['event_month'] = $events['event_month'] - 12;
                }

                if (  (
    $greq_year >= $events['event_year']) && ($greq_year <= $eyear
                   && (
    $greq_month >= $events['event_month']) && ($greq_month <= $emonth) )
                {
                
    // Set end day for current month

                        
    if ($emonth $greq_month)
                        {
                            
    $emonth $greq_month;
                            
    $eday date('t'mktime(0,0,0$greq_month1$greq_year));
                        }
                        
                        
    // Set start day for current month
                        
    if ($events['event_month'] < $greq_month)
                        {
                            
    $events['event_month'] = $greq_month;
                            
    //$events['event_day'] = 1;
                        
    }    
                        for (
    $count $events['event_day']; $count <= $eday$count++)
                        {                
                            
    $uxdate mktime(0,0,0,$greq_month,$count,$greq_year); 
                            
    $tdate strftime('%Y%m%d',$uxdate);
                            
    $event_day $count;
                                                                    
                            
    $days[$count] = array($content$eventid);
                        }
                                                    
                }

        
        }
        return 
    $days;



    What I thought I could do was modify the code to generate the array with day as a value and not the key. Then I could loop through the newly created array and combine both values for that day together.

    For example,

    array = (day => 16, eventinfo => information goes here)
    array = (day => 16, eventinfo => here's the second event on that day)

    create a loop or function that results in:

    array = 16 = array(information goes here AND here's the second event on that day);

    I have searched Google and haven't been able to find anything that seems to create the effect that I want.

    Any help would be greatly appreciated.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An easier alternative (in my opinion) is to keep the day as the key, but make the event info be an array of arrays

    instead of the current
    (day1 = array (content, eventid),

    use
    (day1 = array( array(content, eventid), array(content, eventid) ),

    a quick look in getDays() you would change it to
    PHP Code:
    $days[$count][] = array($content$eventid);
    //           ^^ notice new brackets 
    And then in the generate_calendar() function
    PHP Code:
            if(isset($days[$day]) and is_array($days[$day])){
                
    $buffer '';
                foreach (
    $days[$day] as $event_info) {
                    list(
    $content$eventid) = $event_info;
                    
    $buffer .= "$content<br>\n";
                }

                
    $calendar .= '<td valign="top" width="14%">'.$day.'<br />'.$buffer.'</td>'
            } 

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    Tulsa
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked fantastically. Thank you so very much, I was so convinced I had to add to the value somehow I didn't even think about doing it any other way.


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
  •