当前位置: 动力学知识库 > 问答 > 编程问答 >

codeigniter - How can display different pages by one function and how should declare links in menu?

问题描述:

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>

controllers/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');

}

}

?>

how can I show view/about_us_view.php, view/contact.php etc pages by one function ?

-Thanks.

网友答案:

I assume all ur view pages are in root views folder

Controller

<?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');
        }

}

}

Header

<!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> 

the basic url pattern is given below

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

In the index function we are passing the page name as an argument

To view contact page

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

Here

controller:main

method:index

argument:contact

Also autoload the url helper in config/autoload.php.

$autoload['helper'] = array('url');
网友答案:

You need to do the following:

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'); ?>

And footerview.php

</body>
</html>

And controller

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

class main extends CI_Controller {
 function index(){
     $this->load->view('homeview');
  } 
}
?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: