dre26973 2013-08-01 16:41
浏览 41
已采纳

CodeIgniter / PHP错误消息

The goal of this application is to be able to click on a link from one view to get data from another. The first view works just fine and I'm getting the correct PK as well. When I click the link I am having issues.

Getting a 'Message: Invalid argument supplied for foreach()' from this view:

<?php
    echo "<h2>View</h2>";
    foreach ($rows as $r){
        echo '<br /><h3>'; echo $r->tHandle; echo'</h3>';
        echo "<li>Sent at:  "; echo $r->content; echo"</li><br />";
        echo'<li>Created:  '; echo $r->created; echo'</li><br />';
    }
?>

This is the model and function where the DB query is taking place:

<?php
class Tweets_model extends CI_Model{

    public function __construct() {

        $this->load->database();

    }

    public function getTweetDetails($id){
    $this->db->select('*')->from('tweets')->where('tweetId', $id);
    $q = $this->db->get();
        if($q->num_rows > 0){

            foreach($q->result() as $row){

                $data[]=$row;

            }
            return $data;

        }

    }
}
?>

Controller:

<?php

class Tweets extends CI_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->model('tweets_model');
    }

    public function details(){

        $data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(2));
        $this->load->view('header');
        $this->load->view('tweet_details', $data);
        $this->load->view('footer');
    }
}

?

>

Can anyone help me get past my error message?

Thanks.

  • 写回答

2条回答 默认 最新

  • doumaque6551 2013-08-01 17:03
    关注

    You need to do some troubleshooting here. All your errors are because the database isn't returning any data.

    1. Ensure that the tweetId is actually in segment 2, the segments start counting after the base_url so if your base url is example.com then segment 2 would be something like example.com/tweet/2 with 2 being the right segment.

    2. Pass a tweetId you KNOW exists to the model in this line:

      $data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(2));

    So something like:

    $data['rows'] = $this->tweets_model->getTweetDetails(1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测