【问题标题】:Creating related articles using Codeigniter使用 Codeigniter 创建相关文章
【发布时间】:2017-08-17 15:04:04
【问题描述】:

控制器:

$data['similartopstories']=$this->frontend_model->homeallgallerypicssimilar();
$this->load->view('Moviestory',$data);

型号:

public function homeallgallerypicssimilar()
 {
    $query = $this->db->get_where('tbl_topstories',array('topstory_id' => $this->uri->segment(3))) ;
    foreach($query->result() as $row){ $tags = $row->meta_tag; }
    $match =  explode(",",$tags);
    $result = [];
    for($i = 0; $i < count($match); $i++)
    {
        $this->db->like('meta_tag',$match[$i]); 
        $this->db->from('tbl_topstories');
        $query = $this->db->get();
        if($query->num_rows()>0)
        $result[] = $query->result();       
    }   
    $sql="select * from tbl_topstories where topstory_id order by topstory_id desc limit 6";
    $query=$this->db->query($sql);
    return $query->result_array();

 }

查看:

<?php
foreach($similartopstories as $rec_article)
{

?>
    <a href="<?php echo base_url(); ?>topstories/<?php echo $this->frontend_model->clean($rec_article['article_url']); ?>"><div style="width:100px; height:140px; float:left; margin:10px;">
     <img src="<?php echo base_url(); ?>Topstoriespics/<?php echo $rec_article['topstory_pic']; ?>" alt="" width="100" height="100"  />
     <br>
    <h1 style="font: bold 11px Arial, Helvetica, sans-serif;color: #006699;" class="nopadding" ><?php echo $rec_article['topstory_title']; ?></h1>
    </div></a>
    <?php 
}

?>

代码无法正常工作,只有不断重复的帖子,所以我需要你的帮助...感谢随时回复

【问题讨论】:

  • 问题/问题/错误是?您需要我们提供什么?只是为了查看您的代码?
  • 代码不能正常工作,只有不断重复的帖子,所以我需要你的帮助

标签: php mysql codeigniter


【解决方案1】:

很明显,您遇到了问题,但您必须更具体地了解您的问题,以便快速获得具体的答案。

快速浏览一下,在您的模型中,您不应该使用“$this->uri->segment(3)”,将其作为模型的输入,并且它也应该在控制器中。

$data['similartopstories']=$this->frontend_model->homeallgallerypicssimilar($this->uri->segment(3));

public function homeallgallerypicssimilar($topStoryID='') {
  $query = $this->db->get_where('tbl_topstories',array('topstory_id' => $topStoryID )) ;
  ...
}

其余的看起来还可以。

@Moulali ,我无法为你完成任务,你必须自己完成。我可以帮你指导。

首先是模型

// MODEL
public function homeallgallerypicssimilar($topStoryID = '')
{
    $query = $this->db->get_where('tbl_topstories',array('topstory_id' => $topStoryID )) ;
    echo "<pre>";print_r($query);die; // print_r $query and check whether it suits yr needs....  IF STILL FAILS, tryout on yr localhost phpadmin SQL and check the output
    foreach($query->result() as $row){ $tags = $row->meta_tag; }
    // print_r $tags and check whether it suits yr needs
    $match =  explode(",",$tags);
    // print_r $match and check whether it suits yr needs
    $result = [];
    for($i = 0; $i < count($match); $i++)
    {
        $this->db->like('meta_tag',$match[$i]); 
        $this->db->from('tbl_topstories');
        $query = $this->db->get();
        if($query->num_rows()>0)
        $result[] = $query->result();       
        // AT THE END OF FIRST LOOP, BEFORE THE SECOND ONE START , print_r the $result and check whether it suits yr needs....  IF STILL FAILS, tryout on yr localhost phpadmin SQL and check the output
    }   
    $sql="select * from tbl_topstories where topstory_id order by topstory_id desc limit 6";
    $query=$this->db->query($sql);
    // FIX THE $sql AND THEN print_r the $query and check whether it suits yr needs... IF STILL FAILS, tryout on yr localhost phpadmin SQL and check the output
    return $query->result_array();

 }

控制器

// CONTROLLER
$data['similartopstories']=$this->frontend_model->homeallgallerypicssimilar($this->uri->segment(3));
echo "<pre>";print_r($data['similartopstories']);die; // print_r the $data['similartopstories'] and check whether it suits yr needs
$this->load->view('Moviestory',$data);

风景

// VIEW
<?php
echo "<pre>";print_r($similartopstories);die; // CHECK THE OUTPUTS whether it suits yr needs
foreach($similartopstories as $rec_article){
?>
    <a href="<?php echo base_url(); ?>topstories/<?php echo $this->frontend_model->clean($rec_article['article_url']); ?>"><div style="width:100px; height:140px; float:left; margin:10px;">
     <img src="<?php echo base_url(); ?>Topstoriespics/<?php echo $rec_article['topstory_pic']; ?>" alt="" width="100" height="100"  />
     <br>
    <h1 style="font: bold 11px Arial, Helvetica, sans-serif;color: #006699;" class="nopadding" ><?php echo $rec_article['topstory_title']; ?></h1>
    </div></a>
    <?php 
     }
?>

我很确定你能找到哪里出了问题, 别忘了给我的答案点赞!

【讨论】:

  • 当我点击文章链接时,我没有得到相关的帖子,只有重复相同的文章
  • 您是否将 $this->uri->segment(3) 移至控制器?如果你这样做了,仍然重复相同的文章,那么我建议你一个一个调试,一步一步检查$topStoryID是否准确检查你的sql代码是否有你想要的正确输出
  • 我刚刚意识到你的模型接近尾声function homeallgallerypicssimilar($topStoryID=''),行:$sql="select * from tbl_topstories where topstory_id order by topstory_id desc limit 6";为什么代码where topstory_id不完整?
  • 我不知道请告诉我如何完成任务
  • @MoulaliDeshamoni 请在上面找到更多答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多