如何在ci中使用href标签重定向页面?

时间:2017-06-28 06:00:16

标签: codeigniter

控制器:

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Test extends CI_Controller 
    {
        function __construct() 
        {
            parent :: __construct();
            $this->load->helper(array('form', 'url'));
            $this->load->model('Fetch_data');
        }
        public function college()
        {
            $college_name = $this->input->get('college_name');
            $this->load->view('college');
        }
    }

视图:

<ul class="list">
<?php
    foreach ($engineering_priority as $row)
    {
        echo "<li><a href='test/college/".$row['college_name']."(".$row['field'].")' id='coll'>" . $row['college_name'] . "</a></li>";   
    }
?>
</ul>

我是ci的新人。在视图中我创建了一个链接,并希望重定向到controller / view / college_name即(test / college / abc college)。那么,我该怎么办呢?请帮帮我。

谢谢

3 个答案:

答案 0 :(得分:1)

首先,您必须在

中配置base_url

application/config/config.php

然后你可以编辑

$config['base_url'] = 'http://localhost/your_site_folder/';

完成配置后,您可以使用base_url()作为您的网址。

示例:

你想要建立一个链接去你的控制器test和功能college你这样做:

<ul class="list">
 <?php foreach ($engineering_priority as $row) { ?>
    <li>
       <a href='<?php echo base_url(); ?>test/college'>
           <?php echo $row['college_name'];?>
       </a>
    </li>
<?php } ?>
</ul>

你可以这样试试。

答案 1 :(得分:0)

base Url

中设置config.php
<li><a href="<?php echo base_url();?>test/college/<?php echo $row['college_name'].$row['field']; ?>"><?php echo $row['college_name'] ?></a></li>

答案 2 :(得分:0)

首先,您必须在application/config/config.php中设置基本网址。
然后你可以使用你的链接
echo "<li><a href='" . base_url() . "test/college/".$row['college_name']."(".$row['field'].")' id='coll'>" . $row['college_name'] . "</a></li>"; 同样在您的代码中,您有参数$row['college_name']."(".$row['field'],它是college()函数的参数,因此您可以将该参数作为

public function college($college_name_from_link)
    {
        //get data of college 
        //$this->data['college'] = query for data 
        $college_name = $college_name_from_link;
        $this->load->view('college', $this->data);// generate view with passing $this->data['college'] array to view
    }