2013-02-21 24 views
1

headerview.php如何通過一個函數顯示不同的頁面,以及如何在菜單中聲明鏈接?

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title><?php echo $title; ?></title> 
    </head> 
    <body> 
     <div id="topmenu"> 
      <ul> 
       <li><a href="What Should I write Here ?">Home</a></li> 
       <li><a href="What Should I write Here ?">About Us</a></li> 
       <li><a href="What Should I write Here ?">Contact Us</a></li> 
      </ul>  
     </div> 

footerview.php

</body> 
</html> 

控制器/ main.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class main extends CI_Controller { 
function index(){ 
    $this->load->view('headerview'); 
    $this->load->view('homeview'); 
    $this->load->view('footerview'); 
    } 

} 
?> 

如何可以顯示視圖/ about_us_view.php,查看/ contact.php等通過一個函數頁面?

- 謝謝。

回答

1

我假定所有烏爾視圖頁面在根視圖文件夾

控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Main extends CI_Controller { 

function index($page = 'homeview') 
{ 

    if (! file_exists('application/views/'.$page.'.php')){ 

     show_404(); 
    } 
    else{ 

    $this->load->view('headerview'); 
    $this->load->view($page); 
    $this->load->view('footerview'); 
     } 

} 

} 

部首

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title><?php echo $title; ?></title> 
    </head> 
    <body> 
     <div id="topmenu"> 
      <ul> 
       <li><a href="<?php echo base_url('index.php/main');?>">HOME</a></li> 
       <li><a href="<?php echo base_url('index.php/main/index/about_us_view');?>">About Us</a></li> 
       <li><?php echo base_url('index.php/main/index/contact');?>">Contact Us</a></li> 
      </ul>  
     </div> 

基本URL模式如下

http://example.com/[controller-class]/[controller-method]/[arguments] 
給出

在索引函數我們傳遞的頁面名稱作爲參數

要查看聯繫人頁面

<?php echo base_url('index.php/main/index/contact');?> 

這裏

控制器:主

方法:索引

論點:聯繫

還可以在config/autoload.php中自動載入url helper。

$autoload['helper'] = array('url'); 
+0

關於我們和聯繫方式頁面未顯示。和「$ page ='homeview'」是不是隻爲homeview.php修復了?那麼如何顯示其他頁面呢? – user1844626 2013-02-21 13:50:44

+0

抱歉我的壞,那是現在工作。感謝您的答案。 – user1844626 2013-02-21 14:02:48

-1

你需要做到以下幾點:

headerview.php

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title><?php echo $title; ?></title> 
    </head> 
    <body> 

homeview.php

<?php $this->load->view('headerview'); ?> 
<div id="topmenu"> 
      <ul> 
       <li><a href="What Should I write Here ?">Home</a></li> 
       <li><a href="What Should I write Here ?">About Us</a></li> 
       <li><a href="What Should I write Here ?">Contact Us</a></li> 
      </ul>  
     </div> 
<?php $this->load->view('footerview'); ?> 

而且footerview.php

</body> 
</html> 

和控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class main extends CI_Controller { 
function index(){ 
    $this->load->view('homeview'); 
    } 
} 
?> 
+0

對不起,這不是我問的。 – user1844626 2013-02-21 13:51:33

+0

在「homeview」上寫$ this-> load-> view('about_us_view'); ? – Winston 2013-02-21 13:55:35

+0

請嘗試理解我在問題中提出的問題。它關於通過一個函數加載頁面。而不是通過爲每個頁面執行$ this-> load-> view('page_name')。 – user1844626 2013-02-21 13:59:16

相關問題