2013-12-20 59 views
0

是否可以使用php調用CodeIgniter的控制器?是否可以使用php調用CodeIgniter的控制器?

我想用php調用控制器。我有一個顯示全年日曆。

<table style="margin-left:-50px;width:1300px;" class="table table-bordered"> 
         <tr class="blue"> 
          <th style="width:67px;"> 
           <div style="position: absolute; margin-top: -5px;"> 
            <select style="width:auto;" name="years" onchange="submit();"> 
            <?php for($i=1;$i<=5;$i++): ?> 
             <option value="<?php echo date('Y', strtotime('+'.$i.' year')); ?>" <?php echo ($dYear==date('Y', strtotime('+'.$i.' year'))?'selected':'');?>><?php echo date('Y', strtotime('+'.$i.' year')); ?></option> 
             <?php endfor; ?> 
            </select> 
           </div>  
          </th> 

          <th>Su</th> 
          <th>Mo</th> 
          <th>Tu</th> 
          <th>We</th> 
          <th>Th</th> 
          <th>Fr</th> 
          <th>Sa</th> 
          <th>Su</th> 
          <th>Mo</th> 
          <th>Tu</th> 
          <th>We</th> 
          <th>Th</th> 
          <th>Fr</th> 
          <th>Sa</th> 
          <th>Su</th> 
          <th>Mo</th> 
          <th>Tu</th> 
          <th>We</th> 
          <th>Th</th> 
          <th>Fr</th> 
          <th>Sa</th> 
          <th>Su</th> 
          <th>Mo</th> 
          <th>Tu</th> 
          <th>We</th> 
          <th>Th</th> 
          <th>Fr</th> 
          <th>Sa</th> 
          <th>Su</th> 
          <th>Mo</th> 
          <th>Tu</th> 
          <th>We</th> 
          <th>Th</th> 
          <th>Fr</th> 
          <th>Sa</th> 
          <th>Su</th> 
          <th>Mo</th> 

         </tr>         
         <?php 
          function FriendlyDayOfWeek($dayNum) { 
          // converts the sunday to 7 
          // This function can be removed in php 5 by - date("N"), 
          // just remove function calls below and replace by swapping date("w" for date("N" 
          if ($dayNum == 0){ return 0; } else { return $dayNum; } 
          } 

          //inserting blank TDs 
          function InsertBlankTd($numberOfTdsToAdd) { 
          for($i=1;$i<=$numberOfTdsToAdd;$i++) { 
          $tdString .= "<td></td>"; 
          } 
          return $tdString; 
          } 


          for ($mC=1;$mC<=12;$mC++) { 
          //for loop for inserting MONTHS 
          /* 
          * $mC = MONTH 
          * $dDay = DAY 
          * $dYear = YEAR 
          */ 
          $currentDT = mktime(0,0,0,$mC,$dDay,$dYear); 
          $daysInMonth = date("t",$currentDT); 
          $dMonth = date("n",$currentDT); 
          //echo "<pre>".$daysInMonth."</pre>"; 
          echo "<tr><td><div>".date("M",$currentDT)."</div></td>"; 


          echo InsertBlankTd(FriendlyDayOfWeek(date("w",$currentDT))-0); 

          //for loop for inserting DAYS. 
          for ($i=1;$i<=$daysInMonth;$i++) { 
          $exactDT = mktime(0,0,0,$mC,$i,$dYear); 
          $rHoliday = date("n-j",$exactDT); // repeating holidays 
          $nHoliday = date("Y-n-j",$exactDT); //non-repeating holidays 
//check if holiday. 
          if ($holiday!=""){ 
           foreach($holiday as $row){ 
            if ($row['repeats_annually'] == "Yes") 
            { 
             $h = explode("-", $row['date_stamp']); 
             $aHoliday = $h[1]."-".$h[2]; //annual holidays 
             //echo "<pre>".$aHoliday."</pre>"; 
             //$class = ($aHoliday == $rHoliday?"green-box":""); 
             if ($aHoliday == $rHoliday){ 
              echo '<pre>'.$aHoliday . ' = ' .$nHoliday.'</pre>'; 
              $class = 'green-box'; 
             } 
            } 
           } 
          } 

          //echo "<pre>".date("Y-n-j",$exactDT)."</pre>"; 
          //if ($i==date("d")&&date("m",$currentDT)==date("m")) { $class="currentDay"; } else { $class = ""; } 
          echo "<td class=''>".$i.$class."</td>"; 
          } 

          echo InsertBlankTd($dDaysOnPage - $daysInMonth - FriendlyDayOfWeek(date("w",$currentDT))-0); 
          echo "</tr>"; 
          } 
          ?> 
        </table> 

現在你會看到我的評論「// for循環插入天」,你會看到另一個註釋「//檢查是否放假。」我會在那個課堂上放一節課,如果是假期,它會變成綠色。問題是我認爲我的算法是錯誤的。所以我在想,我會在$假期中刪除該foreach。並且只會調用控制器函數,在該函數中將比較每個「日期」,並檢查它是否是假期而不是使用foreach,如果可以使用php調用控制器。但有沒有其他方法可以做得更簡單?或者如果沒有。這也是好的。

這裏的控制器

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
class attendance extends CI_Controller { 



public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('fill_in','fill'); //model fill in 
    $this->load->model('mdl_employee','emp'); 
    $id = $this->session->userdata('id');   
} 


public function index() 
{ 
    $account = $this->session->userdata('account_type'); 
    if ($account == '3' || $account == '1') 
    { 
    $id = $this->session->userdata('id'); 
    $data['info'] = $this->emp->get_myinfo($id); 
    $data['holiday'] = $this->emp->get_holidays(); 
    $data['employee_header_menus'] = $this->load->view('employee_header_menus', NULL, TRUE); 
    $data['employee_header_logout'] = $this->load->view('employee_header_logout', $data, TRUE); 
    $this->load->view('employee/attendance', $data); 
    } 
} 
} 

和$數據[「假期」]

public function get_holidays() 
{ 
    $this->db->select('*'); 
    $this->db->from('holidays'); 
    $query = $this->db->get(); 

    if($query->num_rows() > 0) 
    { 
     foreach ($query->result() as $row) 
     { 
      $data[] = array(
        'description' => $row->description, 
        'date_stamp' => $row->date_stamp, 
        'repeats_annually' => $row->repeats_annually 
       ); 
     } 
    } 
    else 
    { 
     $data = ""; 
    } 
    return $data; 
} 
+1

如何使用內置日曆? – tomexsans

+1

你可能會打電話給控制器,但我不明白這一點是mvc結構,在控制器中,你只是調用庫,模型等,可以通過使用'$ CI =&get_instance();' –

+0

@tomexsans輕鬆完成,但內置的codeigniter日曆只顯示月份.. – Vincent

回答

1

你想打電話或者圖書館或輔助特定的功能模型。控制器的主要目的是作爲路由器;這是因爲它也是一個類,很多人「濫用」它(我也這樣做 - 我們都這樣做)包括許多相關功能&用$ this-> myfunction()調用它們

無論這是否完全合適我不會在這裏辯論,但簡短的答案是 - 使用庫或幫助函數來做你以後的事情類型;只能調用控制器,就像調用實際的頁面或api/ajax調用一樣。

+0

ooooh ..現在我明白了。感謝您的建議。 – Vincent

相關問題