是否可以使用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 for for for DAYS。”,你会看到另一个评论'//检查假期。'其中我将在该td上放一个课程,如果是假期,它会变成绿色。问题是我觉得我的算法错了。所以我想我会在$ holiday上删除那个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);
}
}
}
和$ data ['holiday']的模型
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;
}
答案 0 :(得分:1)
您想要在库或帮助程序上调用特定函数。控制器的主要目的是充当路由器;这是因为它也是一个很多人“滥用”它的类(我也这样做 - 我们都这样做)包括许多相关的功能&amp;用$ this-&gt; myfunction()
打电话给他们这是否完全合适我不会在这里辩论,但简短的回答是 - 使用库或帮助函数来做你想要的事情;只调用控制器,就像调用实际页面或api / ajax调用一样。