Taking function() in a variable

Discussion in 'PHP' started by KingCobra, May 4, 2009.

  1. #1
    ---- calendar function code here ----
    showCalendar();


    I want to take this function in variable like bellow

    $calendar = showCalendar();

    Is it possible? How can I take? I used $calendar = showCalendar(); but it prints calendar not placed in the variable $calendar.
     
    KingCobra, May 4, 2009 IP
  2. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hey,

    If I understand this correctly, you want to place the calendar output inside the variable $calendar. So you can either use it somewhere else or print the $calendar var.

    It's all down to the function design, for example -

    
    function showCalendar() {
          //Do all calendar code here,
          //But instead of using Print or Echo
          //Place it in a variable and return it. Like so.
          $retCal = "some code here";
          $retCal .= "<br />some more code here";
          return $retCal;
    }
    
    PHP:
    With the example function above, if you used -

    
    $calendar = showCalendar();
    
    PHP:
    The $calendar variable would contain: "some code here <br /> some more code here" instead of it being displayed on the page.

    I hope I described it properly, it can be a tad confusing when first dealing with functions and return values.

    PS. The best way to get help is to post the actual showCalendar function code.

    Regards,

    Steve
     
    Steve136, May 4, 2009 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    The easy way (even if you use echo):

    
    
    function showCalendar() {
        ob_start();
    
        // Your old code
    
        $htmlCode = ob_get_contents();
        ob_end_clean();
        return $htmlCode;
    }
    
    $calendar = showCalendar();
    
    echo $calendar;
    
    
    PHP:
    Peace,
     
    Barti1987, May 4, 2009 IP
  4. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Steve136
    ========================

    function showCalendar(){
    // Get key day informations.
    // We need the first and last day of the month and the actual day
    $today = getdate();
    $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
    $lastDay = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));


    // Create a table with the necessary header informations
    echo '<table>';
    echo ' <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
    echo '<tr class="days">';
    echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
    echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';


    // Display the first calendar row with correct positioning
    echo '<tr>';
    for($i=1;$i<$firstDay['wday'];$i++){
    echo '<td>&nbsp;</td>';
    }
    $actday = 0;
    for($i=$firstDay['wday'];$i<=7;$i++){
    $actday++;
    if ($actday == $today['mday']) {
    $class = ' class="actday"';
    } else {
    $class = '';
    }
    echo "<td$class>$actday</td>";
    }
    echo '</tr>';

    //Get how many complete weeks are in the actual month
    $fullWeeks = floor(($lastDay['mday']-$actday)/7);

    for ($i=0;$i<$fullWeeks;$i++){
    echo '<tr>';
    for ($j=0;$j<7;$j++){
    $actday++;
    if ($actday == $today['mday']) {
    $class = ' class="actday"';
    } else {
    $class = '';
    }
    echo "<td$class>$actday</td>";
    }
    echo '</tr>';
    }

    //Now display the rest of the month
    if ($actday < $lastDay['mday']){
    echo '<tr>';

    for ($i=0; $i<7;$i++){
    $actday++;
    if ($actday == $today['mday']) {
    $class = ' class="actday"';
    } else {
    $class = '';
    }

    if ($actday <= $lastDay['mday']){
    echo "<td$class>$actday</td>";
    }
    else {
    echo '<td>&nbsp;</td>';
    }
    }


    echo '</tr>';
    }

    echo '</table>';
    }

    // ============= end function ==================

    $month_calendar = "showCalendar()";

    $template->replace("month_calendar", $month_calendar);



    I will use {month_calendar} in my template (.html) file (any where i want)
    But when I declare $month_calendar in my php file the it prints at the top of page without temple calling
     
    KingCobra, May 5, 2009 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Hi,

    It's better using function name, but when you need var, try the following:
    
    $month_calendar = &showCalendar();
    
    PHP:
    Regards
     
    koko5, May 5, 2009 IP
  6. GreenWithEnvy

    GreenWithEnvy Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #6
    It depends on exactly how your template class works... but for my template classes this would fix it for you:
    function showCalendar(){
    // Get key day informations.
    // We need the first and last day of the month and the actual day
    $today = getdate();
    $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
    $lastDay = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));

    $output = '';
    // Create a table with the necessary header informations
    $output .= '<table>';
    $output .= ' <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
    $output .= '<tr class="days">';
    $output .= ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
    $output .= ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';


    // Display the first calendar row with correct positioning
    $output .= '<tr>';
    for($i=1;$i<$firstDay['wday'];$i++){
    $output .= '<td>&nbsp;</td>';
    }
    $actday = 0;
    for($i=$firstDay['wday'];$i<=7;$i++){
    $actday++;
    if ($actday == $today['mday']) {
    $class = ' class="actday"';
    } else {
    $class = '';
    }
    $output .= "<td$class>$actday</td>";
    }
    $output .= '</tr>';

    //Get how many complete weeks are in the actual month
    $fullWeeks = floor(($lastDay['mday']-$actday)/7);

    for ($i=0;$i<$fullWeeks;$i++){
    $output .= '<tr>';
    for ($j=0;$j<7;$j++){
    $actday++;
    if ($actday == $today['mday']) {
    $class = ' class="actday"';
    } else {
    $class = '';
    }
    $output .= "<td$class>$actday</td>";
    }
    $output .= '</tr>';
    }

    //Now display the rest of the month
    if ($actday < $lastDay['mday']){
    $output .= '<tr>';

    for ($i=0; $i<7;$i++){
    $actday++;
    if ($actday == $today['mday']) {
    $class = ' class="actday"';
    } else {
    $class = '';
    }

    if ($actday <= $lastDay['mday']){
    $output .= "<td$class>$actday</td>";
    }
    else {
    $output .= '<td>&nbsp;</td>';
    }
    }


    $output .= '</tr>';
    }

    $output .= '</table>';
    return $output;
    }

    // ============= end function ==================

    $month_calendar = showCalendar();

    $template->replace("month_calendar", $month_calendar);


    Let me know if that helped.

    Matthew
     
    GreenWithEnvy, May 5, 2009 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Based on the showCalendar() function, this will not work as you want it to.

    You need to rewrite the showCalendar() function so that the data is stored in a variable and returned instead of echoing it.
     
    jestep, May 5, 2009 IP
  8. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #8
    koko5 ,

    output is same.
     
    KingCobra, May 5, 2009 IP
  9. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #9
    Can you please share code for template class , function $template->replace()
     
    koko5, May 5, 2009 IP
  10. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #10
    KingCobra, May 5, 2009 IP