在《PHP敏捷开发框架CodeIgniter》中,有一个display模型,下面的代码实现了其中的部分内容。其中的数据库,就是书中所提到的websites, 所操作的表格就是'ci_sessions'。不过,其中的'status'的格式让我给改成了'varchar',宽度不变,还是4位。奇怪的是,在entrypage中,所谓的菜单项向右缩进一位;在mainpage中,菜单项向右缩进了三位,不知道是怎么回事!

控制器start.php:

<?php
class Start extends CI_Controller
{
 function __construct()
 {
  parent::__construct();

 // $this->output->enable_profiler(TRUE);

  $this->load->helper('url');
  $this->load->helper('form');
  $this->load->library('unit_test');
  $this->load->library('menu');
  $this->unit->active(TRUE);
  $this->load->database();
 }

 function index()
 {
  $data['base']=$this->config->item('base_url');
 // $data['css']=$this->config->item('css');
  $data['mytitle']="A website to monitor other websites";
  $data['text']="Please log in here!";
  $mymenu=$this->menu->show_menu("hi");
  $data['menu']=$mymenu;
  $this->load->view('entrypage',$data);
  
 }
 
 function hello($name)
 {
  echo $name;
 }

 function another_function()
 {
  echo "you are here!";
 }

 function assessme()
 { 
  $data['base']=$this->config->item('base_url');
  $data['menu']='';

  $this->load->library('form_validation');
  $rules['username']="required";
  $rules['password']="required";
  $this->form_validation->set_rules($rules);
  $this->load->library('session');
  $name=$_POST['username'];
  $password=$_POST['password'];
  if($this->form_validation->run()==FALSE)
  {
   $this->index();
  }
  else 
  { 
   if($name=='fred' && $password=='12345')
   { 
    
   // $this->benchmark->mark('here_start');
  
    $newdata=array('status'=>'ok');
    $this->session->set_userdata($newdata);
   // echo $this->session->userdata('ip_address')."<br>";
   // echo $this->session->userdata('session_id')."<br>";
   // echo $this->session->userdata('user_agent')."<br>";
   // echo $this->session->userdata('last_activity')."<br><br>";

    $currentsession=array('ip_address'=>$this->session->userdata('ip_address'),
           'session_id'=>$this->session->userdata('session_id'),
           'user_agent'=>$this->session->userdata('user_agent'),
           'last_activity'=>$this->session->userdata('last_activity'),
           'status'=>$this->session->userdata('status'));
    $this->db->insert('ci_sessions',$currentsession);

   // $this->benchmark->mark('here_end');
   // echo $this->benchmark->elapsed_time('here','there');
  
   // $this->output->enable_profiler(TRUE);
    $this->load->model('display');
    $this->display->mainpage($data);
   }
   else
   { 
    $this->index();
   }
  }
 }
}
?>

 

display.php内容为:

<?php
class Display extends CI_Model
{
 var $data=array();
 var $base;
 var $status='';
 
 function __construct()
 {
  $this->load->helper('form');
         $this->load->library('user_agent');
         $this->load->library('menu');
         $this->load->library('session');

         $this->data['css'] = $this->config->item('css');
         $this->data['base'] = $this->config->item('base_url');
         $this->base         = $this->config->item('base_url');
         $this->data['myrobots'] = '<meta name="robots" content="noindex,nofollow"/>';

  $sessionid = $this->session->userdata('session_id');
         $this->db->select('status');
         $this->db->where('session_id', $sessionid);
         $query = $this->db->get('ci_sessions');
         if ($query->num_rows() > 0)
         { 
              $row = $query->row();
              $this->status = $row->status;
         }


 }

 function mainpage($mydata)
 {
  $this->data['mytitle'] = 'Monitoring website';
         $this->data['diagnostic'] = 'i am here!';
         foreach($mydata as $key => $variable)
         {
              $this->data[$key] = $variable;
         }
         $this->load->library('session');
         $mysess = $this->session->userdata('session_id'); 
  if(isset($this->status))// && $this->status > 0)
               { 
    $mymenu=$this->menu->show_menu($this->status);
    $this->data['menu']=$mymenu;
                }
            $this->load->view('mainpage', $this->data);


 }
}
?> 

 

menu.php中的内容:

<?php
class Menu
{
 function show_menu($item)
 {
  $obj=&get_instance();
  $obj->load->helper('url');
  $menu=anchor("start/hello/fred","Say hello to Fred |");
  $menu.=anchor("start/hello/bert","Say hello to Bert|");
  $menu.=anchor("start/another_function",$item);
  return $menu;
 }
}
?>

 

entrypage.php的内容不变,mainpage.php内容如下:

<html>
<head>
<title><?=$mytitle?></title>
</head>
<body>
<?=$menu?><br>
<?=$diagnostic?>
</body>
</html>

posted on 2012-01-22 12:12  vesa3.0  阅读(363)  评论(0编辑  收藏  举报