动态页面 - Codeigniter

发布于 2024-10-28 09:33:37 字数 4529 浏览 1 评论 0原文

更新:

我已经尝试了下面谢尔顿的代码,但遇到了几个问题:

  1. 当使用行 $this->load->view($page->view) 时template, array('content' => $page->content), TRUE);无法加载请求的文件 .php,但如果我将其更改为 $page->content code> 它工作正常。

  2. 当我将索引函数放入其他控制器时,无论 uri 段设置为何,它仍然会加载默认的主控制器。如何调整模型才能使其工作?

原始问题:

我有一个 template.php 文件,其中包含我的主要站点结构。假设我有一个名为“主页”的页面。我想从数据库中设置 $title$content 数据。

我为每个页面设置了一个视图,还设置了一个控制器。我尝试过:

$this->cms_pages->name;

这不会产生任何结果。我知道我需要获取 URI 段,但因为我使用的是 /$sales->name 我不确定是否必须将其更改为 $sales->id > 使其发挥作用。我想通过名字得到它。最佳实践也很重要。

有没有办法可以做类似的事情:$this->cms_pages->id->1->name

模型

function getCMSPages() {

    $query = $this->db->get('pages');
    if($query->num_rows() > 0) return $query->result();
}

模板

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

class Home extends CI_Controller {

public function index()
{
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Ccho';
$data['content'] = $this->load->view('home', NULL, TRUE);
$this->load->view('template', $data);
}
}

模板

 $stylesheetCSS = array('href' => 'includes/css/style.css', 'rel' => 'stylesheet',   'type' => 'text/css', 'media' => 'print');

echo doctype('html5');
?>
<head>
 <title>Housemovers : <?php echo $title ?></title>
  <?php echo link_tag($stylesheetCSS);?>
   </head>
 <body>
<div id ="wrapper">
<header>
<p>content</p>
</header>
<nav>
    <ul>
    <?php if($this->session->userdata('logged_in')): ?>
        <li><?php echo anchor('#','Edit Pages');?>
        <?php if(is_array($cms_pages)): ?>
                <ul>
                    <?php foreach($cms_pages as $page): ?>
                    <li><a href="<?=$page->title?>"><?= $page->title?></a></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
            </li>
        <li><?php echo anchor('#','Gallery');?>
        <ul>
            <li><?php echo anchor('admin/addgallery','Add Gallery');?></li>
            <li><?php echo anchor('admin/uploadimage','Upload Image');?>
            </li>
            <li><?php echo anchor('#','Delete Gallery');?></li>
        </ul>
        </li>
        <li><?php echo anchor('#','Sales');?>
        <ul>
            <li><?php echo anchor('admin/addsale','Add Sale');?></li>
            <li><?php echo anchor('#','Edit Sale');?>
            <?php if(is_array($sales_pages)): ?>
                <ul>
                    <?php foreach($sales_pages as $sale): ?>
                    <li><a href="editsale/index/<?=$sale->id?>"><?= $sale->name?></a></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
            </li>

            <li><?php echo anchor('#','Delete Sale');?></li>
            <li><?php echo anchor('admin/home/logout','Log Out');?></li>
        </ul>
        </li>
    <?php else: ?>
    <ul>
    <li><?php echo anchor('home','Home');?></li>
    <li><?php echo anchor('about','About Us');?></li>
    <li><?php echo anchor('gallery','Gallery');?></li>
    <li><?php echo anchor('testimonials', 'Testimonials');?></li>
    <li><?php echo anchor('sales','Sales');?></li>
    <li><?php echo anchor('contact','Contact Us');?></li>
    </ul>
    <?php endif; ?>
    </ul>
</nav>
 <section id="content">
    <h1><?= $title ?></h1>

    <p><?= $content ?></p>

</section>

<footer>&copy; Houses LTD <?php echo date('Y');?></footer>
</div> <!-- Wrapper Close -->

</body>

Update:

I have tried Sheldon's code below but I have struck a couple of issues:

  1. When using the line $this->load->view($page->template, array('content' => $page->content), TRUE);Unable to load the requested file .php but if I change it to $page->content it works fine.

  2. When I place the index functions into my other controllers it still loads the default home controller despite what the uri segment is set to. How do I adjust the model for this to work?

Original Question:

I have a template.php file which houses my main site structure. Suppose I have a page called 'home'. I would like to set the $title and the $content data from the database.

I have set a view for each page and also a controller. I have tried:

$this->cms_pages->name;

This produces nothing. I know I need to get the URI segment but because I am using /$sales->name I am unsure if I have to change it to $sales->id for this to work. I would like to get it by the name. Best practices are also important.

Is there a way that I could do something like: $this->cms_pages->id->1->name?

Model

function getCMSPages() {

    $query = $this->db->get('pages');
    if($query->num_rows() > 0) return $query->result();
}

Template

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

class Home extends CI_Controller {

public function index()
{
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Ccho';
$data['content'] = $this->load->view('home', NULL, TRUE);
$this->load->view('template', $data);
}
}

Template

 $stylesheetCSS = array('href' => 'includes/css/style.css', 'rel' => 'stylesheet',   'type' => 'text/css', 'media' => 'print');

echo doctype('html5');
?>
<head>
 <title>Housemovers : <?php echo $title ?></title>
  <?php echo link_tag($stylesheetCSS);?>
   </head>
 <body>
<div id ="wrapper">
<header>
<p>content</p>
</header>
<nav>
    <ul>
    <?php if($this->session->userdata('logged_in')): ?>
        <li><?php echo anchor('#','Edit Pages');?>
        <?php if(is_array($cms_pages)): ?>
                <ul>
                    <?php foreach($cms_pages as $page): ?>
                    <li><a href="<?=$page->title?>"><?= $page->title?></a></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
            </li>
        <li><?php echo anchor('#','Gallery');?>
        <ul>
            <li><?php echo anchor('admin/addgallery','Add Gallery');?></li>
            <li><?php echo anchor('admin/uploadimage','Upload Image');?>
            </li>
            <li><?php echo anchor('#','Delete Gallery');?></li>
        </ul>
        </li>
        <li><?php echo anchor('#','Sales');?>
        <ul>
            <li><?php echo anchor('admin/addsale','Add Sale');?></li>
            <li><?php echo anchor('#','Edit Sale');?>
            <?php if(is_array($sales_pages)): ?>
                <ul>
                    <?php foreach($sales_pages as $sale): ?>
                    <li><a href="editsale/index/<?=$sale->id?>"><?= $sale->name?></a></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
            </li>

            <li><?php echo anchor('#','Delete Sale');?></li>
            <li><?php echo anchor('admin/home/logout','Log Out');?></li>
        </ul>
        </li>
    <?php else: ?>
    <ul>
    <li><?php echo anchor('home','Home');?></li>
    <li><?php echo anchor('about','About Us');?></li>
    <li><?php echo anchor('gallery','Gallery');?></li>
    <li><?php echo anchor('testimonials', 'Testimonials');?></li>
    <li><?php echo anchor('sales','Sales');?></li>
    <li><?php echo anchor('contact','Contact Us');?></li>
    </ul>
    <?php endif; ?>
    </ul>
</nav>
 <section id="content">
    <h1><?= $title ?></h1>

    <p><?= $content ?></p>

</section>

<footer>© Houses LTD <?php echo date('Y');?></footer>
</div> <!-- Wrapper Close -->

</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2024-11-04 09:33:37

正如@davzie 所说;

模型

function getCMSPage($page_name = '') {

    if(!$page_name) $page_name= 'home'; // home be default.    
    $this->db->where('name', $page_name);
    $query = $this->db->get('pages', 1);
    if($query->num_rows() == 1) return $query->row();
}

控制器

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

class Home extends CI_Controller {

public function index()
{
$page = $this->navigation_model->getCMSPage($this-uri->segment(1)); // pretend the first segment in your URI is the page you want.
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = $page->name;
$data['content'] = $this->load->view($page->template, array('content' => $page->content), TRUE);
$this->load->view('template', $data);
}

}

As @davzie said;

Model

function getCMSPage($page_name = '') {

    if(!$page_name) $page_name= 'home'; // home be default.    
    $this->db->where('name', $page_name);
    $query = $this->db->get('pages', 1);
    if($query->num_rows() == 1) return $query->row();
}

Controller

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

class Home extends CI_Controller {

public function index()
{
$page = $this->navigation_model->getCMSPage($this-uri->segment(1)); // pretend the first segment in your URI is the page you want.
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = $page->name;
$data['content'] = $this->load->view($page->template, array('content' => $page->content), TRUE);
$this->load->view('template', $data);
}

}

勿挽旧人 2024-11-04 09:33:37

您可能需要考虑使用一个函数来通过名称获取页面的详细信息。这反过来会运行一个类似 getIdByName($page_name = false) 的函数,

从那里您可以从数据库中提取 ID 并获取正确的页面。

You might want to consider having a function that gets the details of a page by its name. This in-turn would run a function like getIdByName($page_name = false)

From there you would have the ID to pull from the database and get the correct page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文