在codeigniter php中动态显示基于页面的页面标题

时间:2017-04-28 10:27:04

标签: php codeigniter

如何根据显示的页面动态显示页面标题。

您好我有一个在codeigniter php开发的网站,但问题是需要根据页面动态显示页面标题。这些页面标题应该从数据库中提取。任何人都知道如何做到这一点。

控制器:

public function index()
{

    $config = array();
    $config["base_url"] = base_url('testimonial/index');
    $config['total_rows'] =   $this->testimonial_model->record_count();//here we will count all the data from the table
    $config['per_page'] = 6;//number of data to be shown on single page
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
    $data['records7'] = $this->index_model->get_all_banners();
    $data["links"] = $this->pagination->create_links();//create the link for pagination
    $data['mainpage'] = "testimonial";
    $this->load->view('templates/template',$data);
}

型号:

function get_all_testimonials($limit, $start)
{
    $this->db->limit($limit, $start);
    $this->db->select('T.*');
    $this->db->from('testimonials AS T');
    $this->db->where(array('T.status'=>1));
    $q = $this->db->get();
    if($q->num_rows()>0)
    {
        return $q->result();
    }
    else 
    {
        return false;
    }
}

查看:

<div class="container"> 
<div class="row testimonialpage"> 
<div class="col-md-12 testimonialpage">         
    <div class="col-md-9 testimonials" >    
    <div class="testimonialpagetext">       
    </div>  
    <?php if(isset($records2) && is_array($records2)):?>    
    <?php foreach ($records2 as $r):?>          
    <div class="testimonial1">          
    <div class="testimonialtext1">      
    <?php echo $r->description;?>   
    <ul class="founders">
        <li class="founder"><?php echo $r->client_name;?></li>
        <li class="founder"><?php echo $r->founder;?></li>          
    </ul>
    </div>      
    </div>      
    <?php endforeach ;endif;?>  
    <div class="pagination"><?php echo $links; ?></div> 
    </div>  
    </div>      
    </div> 

2 个答案:

答案 0 :(得分:0)

对于Ex ::你的url是www.example.com/controller/register当你得到uri段(2)意味着你将获得(注册)。基于此,您可以确定您在注册页面上。现在,您可以从数据库中找到注册页面的标题。

答案 1 :(得分:0)

每次使用Codeigniter构建应用程序时,我都会使用扩展CI_Controller的类 MY_Controller ,在 application / core 中,如下所示:

<div class="modal fade" id="active-id<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel8" aria-hidden="true">
        <div class="modal-wrapper">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-green">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel8">Article Title: <?php echo $article_title?></h4>
                    </div>
                    <div class="modal-body">
                        <p>Are you sure you want to activate this article?</p>
                    </div>
                    <div class="modal-footer">
                        <div class="btn-group">
                            <form method='post' role='form'>
                                <button type='submit' name='active_yes' class='btn btn-success'>Yes</button>
                                <button type='button' class='btn btn-success' data-dismiss='modal'>No</button>
                            </form>
                        </div>
                    </div>
                    <?php

                    if (isset($_POST['active_yes'])){

                        if ($post_active == "No") {
                            $sql = "UPDATE `articles` SET `post_active` = '1' WHERE `articles`.`id` = $id;";

                            if ($conn->query($sql) === TRUE) {
                                echo "<script type='text/javascript'>alert('Article has been activated successfully and is currently live on the website.')</script>";
                            } else {
                                echo "<script type='text/javascript'>alert('Cannot activate article now. Please Try Again Later!')</script>";
                            }
                        }
                    }

                    ?>
                </div>
            </div>
        </div>
    </div>

应用程序/控制器中的任何控制器都会扩展 MY_Controller 而不是 CI_Controller

现在在 MY_Controller 的构造函数中,您可以执行以下操作:

class MY_Controller extends CI_Controller {
    protected $view_data;

    function __construct() {
        //......
    }
}

加载您的模型,执行查询并将结果添加到 $ this-&gt; view_data 中,如:

$page = $this->uri->segment(2); (2 or 3 or 4 etc, depends on your structure)

模型中getPageTitle的示例:

$pageTitle = $this->your_model->getPageTitle($page);
$this->view_data['page_title'] = $pageTitle['page_title'];

注意我给出的样本名称,你应该改变它们!

要加载视图,请将变量 $ this-&gt; view_data 作为参数传递,并在视图中执行类似

的操作
public function getPageTitle($page) {
    $qry = $this->db->select('page_title')
                ->from('pages')
                ->where('page', $page)
                ->get();
        if ($qry->num_rows() > 0)
            return $qry->row_array();
        return false;
}
相关问题