0

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.

3

2 Answers 2

1

Try this:

VIEW:

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

MODEL:

<?php
class Tweets_model extends CI_Model{

    public function __construct() {

        $this->load->database();

    }

    public function getTweetDetails($id){
    $this->db->select('*')
    $this->db->from('tweets')
    $this->db->where('tweetId', $id);
    return $this->db->get()->result_array();

    }
}
?>

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');
    }
}

?>

UPDATE:

Segments are numbered from left to right. For example, if your full URL is this:

http://example.com/index.php/news/local/metro/crime_is_up

The segment numbers would be this:

$this->uri->segment(1) // with return -> news
$this->uri->segment(2) // with return -> local
$this->uri->segment(3) // with return -> metro
$this->uri->segment(4) // with return -> crime_is_up

In your case $this->uri->segment(2) will return details. And than the query will return 0 rows. Just for testing, you can do like this:

public function details(){
            $tweet_id = 1 // for example
            $data['rows'] = $this->tweets_model->getTweetDetails($tweet_id);
            $this->load->view('header');
            $this->load->view('tweet_details', $data);
            $this->load->view('footer');
        }

Or you can take it from url, for example:

public function details($id){ // Here you will have the $tweet_id

                $data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(3));
                $this->load->view('header');
                $this->load->view('tweet_details', $data);
                $this->load->view('footer');
            }
8
  • Error message is gone but instead of any of the results I am just getting 'Array()' in the browser. Aug 1, 2013 at 16:51
  • @user2642958, please check the view again... I make one edit please, and tell me the result Aug 1, 2013 at 16:53
  • Do you have data in DB? Aug 1, 2013 at 16:56
  • Yes. My first view pulls and displays the same data except for one field without a problem. Aug 1, 2013 at 16:59
  • Because $this->uri->segment(2) in your case is null. So when you do the query, you didn't receive results! Aug 1, 2013 at 16:59
0

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);
3
  • right on - I changed it to: $data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(3)); and it worked!! Thanks!! Aug 1, 2013 at 17:05
  • lol no worries, the uri segments are almost always the issue with stuff like this. Aug 1, 2013 at 17:07
  • I am brand new to PHP and ci so I'm still making newbie mistakes. Thanks again. Aug 1, 2013 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.