Php 动态加载内容codeigniter

Php 动态加载内容codeigniter,php,codeigniter,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>&l

我有这个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();?>

//封闭形式

有可能吗?

我建议如下:

您的控制器:

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>
用这种方法,我想如果我理解正确,你可以达到你想要的


作为旁注:这是一个例子,但你应该能够看到这一点,我建议如下:

您的控制器:

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>
用这种方法,我想如果我理解正确,你可以达到你想要的


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

你可能想考虑把你的视图HTML分解成一个更小的“片段”。CodeIgniter(CI)可以很容易地按照您想要的顺序加载它们,然后一次“显示”它们

下面是一个可能的视图片段细分

header_view.php

<!DOCTYPE HTML>
<html>
  <head>
    <title>
      <?= (isset($page_title)) ? $page_title : "Rapids Riders"; ?>
    </title>
      <div class="clear"></div>
    </div>
    <div class="footer">
      <div>
      </div>
    </div>
  </body>
</html>
footer_view.php

<!DOCTYPE HTML>
<html>
  <head>
    <title>
      <?= (isset($page_title)) ? $page_title : "Rapids Riders"; ?>
    </title>
      <div class="clear"></div>
    </div>
    <div class="footer">
      <div>
      </div>
    </div>
  </body>
</html>
php是一个可以处理多个页面的控制器——faq、帮助、学校、学生

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();
  }

}

我知道我在这里太过火了。希望这是有用的。

你可能想考虑把你的视图HTML分解成一个更小的“片段”。CodeIgniter(CI)可以很容易地按照您想要的顺序加载它们,然后一次“显示”它们

下面是一个可能的视图片段细分

header_view.php

<!DOCTYPE HTML>
<html>
  <head>
    <title>
      <?= (isset($page_title)) ? $page_title : "Rapids Riders"; ?>
    </title>
      <div class="clear"></div>
    </div>
    <div class="footer">
      <div>
      </div>
    </div>
  </body>
</html>
footer_view.php

<!DOCTYPE HTML>
<html>
  <head>
    <title>
      <?= (isset($page_title)) ? $page_title : "Rapids Riders"; ?>
    </title>
      <div class="clear"></div>
    </div>
    <div class="footer">
      <div>
      </div>
    </div>
  </body>
</html>
php是一个可以处理多个页面的控制器——faq、帮助、学校、学生

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();
  }

}

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

我通过嵌套调用$this->load->view($content)的视图解决了这个问题在我的模板视图中,而不是
,然后在控制器函数中,我加载了如下内容

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

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

我通过嵌套调用$this->load->view($content)的视图解决了这个问题在我的模板视图中,而不是
,然后在控制器函数中,我加载了如下内容

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

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

使用
AJAX
将各自的内容加载到ur
div
。。。所有
PHP
代码首先在服务器端进行处理,然后将其结果作为
HTML
reference发送。如果没有
AJAX
使用
AJAX
将各自的内容加载到ur
div
。。。所有
PHP
代码首先在服务器端进行处理,然后将其结果作为
HTML
reference发送。没有AJAX是不可能的。这个看起来不错,让我试试。这个看起来不错,让我试试