1 (edited by twohawks 2006-10-31 03:50)

Topic: How to Pull out "1 of" from a Seeming Array in a For Each Statement

How to Pull out "1 of" from a Seeming Array in a For Each Statement

Hi Folks,
We are working on Gizzmo's Calendar Mod 2.0.x (will be posting an update with his blessing soon) , and beeing as how my php skills are a work in progress (or so we hope), I am hoping to get some pointers in a viable direction here...

The below function code uses a for/each loop to create a configurable display of 3 months, i.e. last month, this month, and next month.  There are a couple things I wish to do... I think I will simply ask one thing at a time...

So First thing then, is how to 'call out' from the function  -the current month, or 2nd item (during the loop)?
I thought maybe 'mini_cal' function would result in some sort of array I could fetch the desired result out of after completion, or that I could catch the item while 'for/each' is looping ...but I still cannot figure it out (after a couple days at it - whew-).  I am missing some pivitol understanding so I can work the problem.  Maybe some of you can help point me?

Here is the mini_cal function code.  I think its pretty staightforward, with a lot of conditions and such in the middle which we are mostly unconcerned with:

/*=================*/
/*= Mini Calendar =*/
/*=================*/
function mini_cal($month, $year, $place){
global $db, $pun_user, $CFG_start_day, $lang_calendar, $type;

        for($X=($month-1); $X<=($month+1); $X++)
        {
                        $month_start = mktime(0,0,0,$X,1,$year);
                        $day_in_mth = date("t", $month_start);
                        $monthtext  = date("F", $month_start);
                        $day_text   = date("D", $month_start);
                        $month_year = date("Y", $month_start);
                        $month_no   = date("n", $month_start);

                        $day_of_wk  = start_date(date("w", $month_start));
                        $month_end  = mktime(23,59,59,$X,$day_in_mth,$year);

        //Find the birthdays for "said" day
        $result = $db->query('SELECT DAYOFMONTH(birthday) as day FROM '.$db->prefix.'users WHERE MONTH(birthday) = '.$month_no.' LIMIT 1') or error('Unable to fetch birtday list', __FILE__, __LINE__, $db->error());
        $bdays = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');
        while($bday_list = $db->fetch_assoc($result)){
            $bdays[$bday_list['day']] .="<a href='calendar.php?view=bday&date=".$month_year.".".$month_no.".".$bday_list['day']."'>".$bday_list['day']."</a><br />";
        };

        //Find the events for "said" day
        $result = $db->query('SELECT id, title, DAYOFMONTH(date) as day FROM '.$db->prefix.'calendar WHERE MONTH(date) = '.$month_no.' AND (YEAR(date) = '.$month_year.' OR YEAR(date) = "1900")') or error('Unable to fetch calendar dates', __FILE__, __LINE__, $db->error());
        $dates = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');
        while($dates_list = $db->fetch_assoc($result)){
            $dates[$dates_list['day']] = "<a href='calendar.php?view=event&date=".$month_year.".".$month_no.".".$dates_list['day']."'>".$dates_list['day']."</a><br />";
        };


?>
            <h2><span><a href="calendar.php?date=<?php echo $month_year.".".$month_no ?>"><?php echo $lang_calendar[$monthtext]?></a></span></h2>
            <div class="box">
                <table cellspacing="0">
                <thead>
                    <tr>
<?php 
                if($place == 'main'){
                    if($CFG_start_day == "S")echo "\t\t\t\t\t\t<th>".$lang_calendar['Sun']."</th>\n";
                    echo "\t\t\t\t\t\t<th>".$lang_calendar['Mon']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Tue']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Wed']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Thu']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Fri']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Sat']."</th>\n";
                    if($CFG_start_day == "M")echo "\t\t\t\t\t\t<th>".$lang_calendar['Sun']."</th>\n";
                }
                elseif($place == 'week'){
                                        if($CFG_start_day == "S")echo "\t\t\t\t\t\t<th>".$lang_calendar['Su']."</th>\n";
                                        echo "\t\t\t\t\t\t<th>".$lang_calendar['Mo']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Tu']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['We']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Th']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Fr']."</th>\n\t\t\t\t\t\t<th>".$lang_calendar['Sa']."</th>\n";
                                        if($CFG_start_day == "M")echo "\t\t\t\t\t\t<th>".$lang_calendar['Su']."</th>\n";
                }
?>
                    </tr>
                </thead>
                <tbody>
                    <tr>
<?php 
                if ($day_of_wk <> 0){for ($i=0; $i<$day_of_wk; $i++){ echo "\t\t\t\t\t\t<td class='calendar_no'> </td>\n"; }}

                for ($date_of_mth = 1; $date_of_mth <= $day_in_mth; $date_of_mth++) {

                                        $date_stamp = mktime(0, 0, 0, $X, $date_of_mth, $year);
                                        $date_no = date("j", $date_stamp);
                                        $day_of_wk = start_date(date("w", $date_stamp));

                    $class = ($date_no ==  date("j") &&  $month_no == date("n") && $month_year == date("Y"))? " class='calendar_day'" :'';
                    if (!empty($bdays[$date_no])) {
                           $class =" class='calendar_bday'";
                           echo "\t\t\t\t\t\t<td".$class.">".$bdays[$date_no]."</td>\n";
                    } else
                    if (!empty($dates[$date_no])) {
                           $class =" class='calendar_event'";
                           echo "\t\t\t\t\t\t<td".$class.">".$dates[$date_no]."</td>\n";
                    } else
                        echo "\t\t\t\t\t\t<td".$class.">".$date_no."</td>\n";


                    if ( $day_of_wk == 6 )
                        echo "\t\t\t\t\t</tr>\n\t\t\t\t\t<tr>\n";

                    if ( $day_of_wk < 6 && $date_of_mth == $day_in_mth )
                        for ( $i = $day_of_wk ; $i < 6; $i++ ) echo "\t\t\t\t\t\t<td class='calendar_no'> </td>\n";

                }
?>
                    </tr>
                </tbody>
                </table>
            </div>
<?php 

                if($place == 'main' && $X==($month-1))
                {
                    echo "\t\t</td><td valign='top' style='border:none;padding:0; margin:0'>\n";
                    $X=($month);
                }
                elseif($place == 'week')
                    echo "\t\t<br />\n";
        }
}

And variations of the following can be used to call the result:

<?php
//get current month and year
$month_no = date("n");
$year = date("Y");
//show mini calendar
?>        <div style='float:right; width:215px'><?php mini_cal1($month_no, $year, "week"); ?></div><?php
?>

My ideas were (1 + 2)...
1) catch it right in here:

                if($place == 'main' && $X==($month-1))
                {
                    echo "\t\t</td><td valign='top' style='border:none;padding:0; margin:0'>\n";
                    $X=($month);
                }
                elseif($place == 'week')
                    echo "\t\t<br />\n";

OR

2) Pull it out of here -afterwards-:
mini_cal($month_no, $year, "week");

But alas, all my methods fall short because I don't quite understand it well enough.
Cannot some array values be fetched/parsed from that somehow?  I tried my hand at a few techniques (without a debugger - so I am kind of working  blind |^| ) but I drew blanks.  Obviously I don't have a firm grasp of some pertinent basics. 


Look forward to your replys / direction.
Cheers,
TwoHawks

TwoHawks
Love is the Function
No Form is the Tool