可以通过带有功能参数的URL访问详细信息(Codeigniter)

问题描述:

我正在为具有新闻的博客创建我的第一个Codeigniter应用程序。在主页面中,只有新闻的标题,这也是一个视图的链接,包含新闻的详细信息和正文。我无法访问通过必须通过接收新的ID作为参数的函数的URL的URL。我只能让它工作。有人能帮我吗?可以通过带有功能参数的URL访问详细信息(Codeigniter)

该问题不在函数本身,因为它工作正常时,我分配一个静态值的URL,但由于某种原因,我可以发送$ row-> id对象作为通过URL获取适当的值为每个新闻。

模型

class Post extends CI_Model{ 


    public function getPost(){ 

     $this->load->database('fintech_blog'); 
     $data = $this->db->get('post'); 
     return $data->$result(); 

    } 

控制器

public function getPost($id){ 

    $query = $this->db->query("select * from post where id = '$id' "); 
    $rows = $query->result(); //method for putting into an array format 
    $data=array('result'=>$rows); 

    $this->load->view('view',$data); 

} 

VIEW

foreach ($result as $row): 

$id = $row->id; 
$post = site_url('welcome/getPost/$row->id'); 

?> 

<!-- Main Content --> 
<div class="container"> 
    <div class="row"> 
     <div class="col-lg-8 col-md-10 mx-auto"> 
      <div class="post-preview"> 
       <a href="<?php echo $post; ?>"> 
        <h2 class="post-title"> 
         <?php echo $row->title; ?> 
        </h2> 
        <h3 class="post-subtitle"> 
         <?php echo $row->calling; ?> 
        </h3> 

       </a> 
       <p class="post-meta">Posted on 
        <!-- <a href="#">Start Bootstrap</a> --> 
        <?php echo time_elapsed_string($row->created); ?></p> 

       </div> 
       <hr> 

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

<?php endforeach; ?> 
+0

在View,$交= SITE_URL( '欢迎/的getPost/$行向> ID');这里的变量不会解析,因为使用单引号('')。用双引号(“”)替换单引号(''),然后尝试。 – Rohit

+0

您可以先试试这个'$ post = site_url('welcome/getPost /'。$ row-> id);'确定 – chad

的问题来自于您创建$post字符串的方式。 正如一位评论者所暗示的那样,问题在于使用单引号并改为使用双引号。双引号字符串最重要的特征是变量名称将被扩展。含义是变量符号(在这种情况下为$test)被替换为变量的值。

举例说明:

$test = "blue"; 
//first, a string created with single quotes 
echo 'The sky is $test today.'; //outputs: The sky is $test today. 

但使用双引号创建的字符串...

echo "The sky is $test today."; //outputs: The sky is blue today. 

的VAR $test得到扩展到其持有的价值。

因此,在视图中应该是foreach循环的前两行。

$id = $row->id; 
$post = site_url("welcome/getPost/$row->id"); 

但是,你永远不会使用$id,你只能使用一次$post。这对我说,这两条线是不需要的。删除它们两个。

改变线

<a href="<?php echo $post; ?>"> 

<a href="<?php echo site_url("welcome/getPost/$row->id"); ?>"> 
+0

非常感谢您的帮助@DFriend –