动态加载内容codeigniter

时间:2016-06-02 11:26:13

标签: php codeigniter

我有这个HTML代码

<body>

<div class="wrapper">
<div class="banner">

</div>
<div class="nav_bar">
    <ul>
        <li><a href="">Home</a></li>
        <li><a  href="">News</a></li>
        <li><a  href="">Registration FAQs</a></li>
        <li><a  href="">How to Register and Rules</a></li>
        <li><a  href="">Register school</a></li>
        <li><a  href="">Register pupil</a></li>
        <li><a   href="">About Us</a></li>
        <li><a   href="">Contact Us</a></li>
    </ul>

</div>

<div class="content_area">
    **<?php echo $content; ?>**
</div>
<div class="clear"></div>



 </div>
<div class="footer">
<div>

</div>
</div>

 </body>

现在,一旦用户点击我想加载内容的链接,取决于他点击了哪个链接,因为你可以看到上面的代码。我可以加载内容,只要它的所有HTML,但现在包括PHP标签,我觉得很难。如何使用以下数据加载内容?

   <div id="form_input">
   <?php    
    echo form_open('form/data_submitted');

    // Show Name Field in View Page
   echo form_label('User Name :', 'u_name');
   $data= array(
   'name' => 'u_name',
    'placeholder' => 'Please Enter User Name',
   'class' => 'input_box'
    );
   echo form_input($data);

    // Show Email Field in View Page
   echo form_label('User email:', 'u_email');
   $data= array(
   'type' => 'email',
   'name' => 'u_email',
   'placeholder' => 'Please Enter Email Address',
   'class' => 'input_box'
    );
   echo form_input($data);
   ?>
  </div>
   // Close Form
   <?php echo form_close();?>

有可能吗?

3 个答案:

答案 0 :(得分:0)

我建议如下:

你的控制器:

class Site extends CI_Controller 
{
    public function home()
    {
        $arrViewData = array
        (
            "content" = $this->load->view("site/home","",true)
        );

        $this->load->view("index", $arrViewData);
    }
}

你的观点叫做索引

<html>
<body>

<div class="wrapper">
<div class="banner">

</div>
<div class="nav_bar">
    <ul>
        <li><a href="<?=base_url('site/home'); ?>">Home</a></li>
        <li><a href="">News</a></li>
        <li><a href="">Registration FAQs</a></li>
        <li><a href="">How to Register and Rules</a></li>
        <li><a href="">Register school</a></li>
        <li><a href="">Register pupil</a></li>
        <li><a href="">About Us</a></li>
        <li><a href="">Contact Us</a></li>
    </ul>

</div>

<div class="content_area">
    <?= $content; ?>
</div>
<div class="clear"></div>



 </div>
<div class="footer">
<div>

</div>
</div>

 </body>
 </html>

您的观点在views / site /

中名为home.php
hello i'm home

使用这种方法,如果我理解正确的话,我认为你可以达到你想要的效果

作为旁注:这是一个例子 - 但你应该能够看到这一点

答案 1 :(得分:0)

您可能需要考虑将视图html分解为较小的&#34;片段&#34;。 CodeIgniter(CI)可以轻松地按照您想要的顺序加载它们,然后显示&#34;显示&#34;他们都是一次性的。

可能会将视图细分为片段。

header_view.php

<!DOCTYPE HTML>
<html>
  <head>
    <title>
      <?= (isset($page_title)) ? $page_title : "Rapids Riders"; ?>
    </title>

在下一个视图片段中,我在链接中放置了一些URL,以使用CI控制器/函数语法作为示例。根据您的需求调整。我的想法&#34; home&#34;,&#34; news&#34;和&#34;注册&#34;是独立的控制器。我将稍微关注Registration控制器。

如果您不熟悉<?=,则<?php echo是简写。

body_start_view.php

  <body>
    <div class="wrapper">
      <div class="banner">

      </div>
      <div class="nav_bar">
        <ul>
          <li><a href=<?= base_url("home"); ?>>Home</a></li>
          <li><a  href=<?= base_url("news"); ?>>News</a></li>
          <li><a  href=<?= base_url("register/faq"); ?>>Registration FAQs</a></li>
          <li><a  href=<?= base_url("register/help"); ?>>How to Register and Rules</a></li>
          <li><a  href=<?= base_url("register/school"); ?>>Register school</a></li>
          <li><a  href=<?= base_url("register/pupil"); ?>>Register pupil</a></li>
          <li><a  href=<?= base_url("home/about"); ?>>About Us</a></li>
          <li><a  href=<?= base_url("home/contact"); ?>>Contact Us</a></li>
        </ul>
      </div>

footer_view.php

      <div class="clear"></div>
    </div>
    <div class="footer">
      <div>
      </div>
    </div>
  </body>
</html>

这三个视图片段很容易被网站上的任何控制器重用。一个基于控制器&#34;帮助&#34;功能如下所示(在注册控制器中),以演示如何实现一种模板&#34;方案

示例表单html将位于名为pupil_form_view.php的文件中,并将在register/pupil页面中使用(在我的示例中)。

使用MVC应用程序结构时,您通常需要设置尽可能多的数据&#34;尽可能在控制器中,然后将其传递给视图。将在调用此视图的控制器中定义和设置此文件中的各种变量。

pupil_form_view.php

<div id = "form_input">
<?php
echo form_open($form_action);
// Show Name Field in View Page
echo form_label($name_field['label'], $name_field['name']);
echo form_input($name_field);
// Show Email Field in View Page
echo form_label($email_field['label'], $email_field['name']);
echo form_input($email_field);
?>
</div>
// Close Form
<?php echo form_close(); ?>

Register.php是一个处理多个页面的控制器 - 常见问题解答,帮助,学校,学生

class Register extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
  }

  //pupil will show the pupil registration form
  public function pupil()
  {
    $view_file = "pupil_form_view"; //the file name of a view to load
    $data['page_title'] = "Pupil Registration";
    $data['form_action'] = 'form/data_submitted';
    //form fields info
    $data['name_field'] = array(
        'label' => 'User Name: ',
        'name' => 'u_name',
        'placeholder' => 'Please Enter User Name',
        'class' => 'input_box'
    );
    $data['email_field'] = array(
        'type' => 'email',
        'name' => 'u_email',
        'placeholder' => 'Please Enter Email Address',
        'class' => 'input_box'
    );
    //output a page
    $this->render_page($view_file, $data);
  }

  //This function handles all the repetitive view loading code.
  //Just pass it the name of a view file and some data to use in the views
  //The default value for $view_data means you don't HAVE TO pass this argument if not needed
  protected function render_page($content, $view_data = NULL)
  {
    //notice the use of 'method chaining' for load functions
    $this->load
        ->view('header_view', $view_data)
        ->view('body_start_view')
        ->view($content) //$view_data vars will be visible to this view file
        ->view('footer_view');
  }

  public function faq()
  {
    $data['page_title'] = "Frequently Asked Questions";
    $view_file = "faq_view";
    //other data setting code if needed
    $this->render_page($view_file, $data);
  }

  public function school()
  {
    $data['page_title'] = "School Registration";
    $view_file = "school_form_view";
    //other data setting code
    $this->render_page($view_file, $data);
  }

  public function help()
  {
    $data['page_title'] = "How to Register";
    $view_file = "help_view";
    //other data setting code
    $this->render_page($view_file, $data);
  }

  function index()
  {
    //just load the help page when users navigate to example.com/register
    $this->help();
  }

}

我知道我在这里过火了。希望这很有用。

答案 2 :(得分:0)

我通过嵌套在我的模板视图中调用 $ this-&gt; load-&gt; view($ content); 的视图而不是<?php echo $content;?>然后在控制器函数上解决了这个问题我加载了这样的内容

 $data['content']='view_name_here'; // this loads content with a view

 $this->load->view('my_template',$data); //loaded my template with $content