使用Codeigniter创建相关文章

时间:2017-08-17 15:04:05

标签: php mysql codeigniter

控制器:

$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 
}

?>

代码工作不正常,只是不断重复发帖,所以我需要你的帮助...随时回复谢谢

1 个答案:

答案 0 :(得分:0)

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

从模型中快速浏览一下,您不应该使用&#34; $ this-&gt; uri-&gt; segment(3)&#34;,将其作为模型和输入的输入。它也应该在控制器中。

$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 
     }
?>

我很确定你能找到哪里出错, 别忘了回答我的回答!