Welcome Guest, Not a member yet? Register   Sign In
Code Wont Run
#1

[eluser]hangbill[/eluser]
Hi
Need help please. Have sat two days trying to get this seemingly simple code to run but not happening. Will post my model, controller and view; if you can see an error in the code please let me know. Thank you very much.

url =
Code:
http://localhost/CodeIgniter1.7.2/topic

model
Code:
<?php
    class Mtopic extends Model {

    function Mtopic() {
        parent::Model();
    }

    function get_records() {
        $q = this->db->get('tbltopic');
        return $q->result();
        }
    }
?>


controller
Code:
<?php
    class Topic extends Controller {

    function index()
    {
        $this->load->model('mtopic');
        $data['result'] = $this->mtopic->get_records();
        $this->load->view('topic_view', $data);
    }
}
?>


view
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    &lt;html &gt;

&lt;head&gt;
  &lt;title&gt;Topics&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;


&lt;?php
    foreach($result() as $row)
    {
      echo $row->Topic  . "<br />";
    }
?&gt;

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]stormlead[/eluser]
hey i suppose ou did no write
$q = $this->db->get('tbltopic');
the $ is missing there. check it out it shud work.
#3

[eluser]jedd[/eluser]
I'm unfamiliar with the error 'not happening' - perhaps you could use more words to describe what happens.

Controllers like a constructor, too.

Unrelated to your problem - the manual indicates you should load models using their ucfirst() name (not just lower case as you've done here) and subsequently refer to them with their ucfirst() name too.
#4

[eluser]markup2go[/eluser]
Enable error reporting in your php.ini or use ini_set() to show errors while you're in development. I believe stormlead already answered your question however Wink
#5

[eluser]khagendra[/eluser]
There is two mistaken you have done.
1. In model function, you forget to use $ sign.

here is ur code
Code:
function get_records() {
        $q = this->db->get('tbltopic');
        return $q->result();
        }
    }

--&gt; Correct it like this
Code:
function get_records() {
        $q = $this->db->get('tbltopic');
        return $q->result();
        }
    }

2. In the view file, the argument in foreach loop.
here is ur code
Code:
&lt;?php
    foreach($result() as $row) // should not be $result()
    {
      echo $row->Topic  . "<br />";
    }
?&gt;

--&gt; It should be like this
Code:
&lt;?php
    foreach($result as $row)
    {
      echo $row->Topic  . "<br />";
    }
?&gt;
#6

[eluser]hangbill[/eluser]
Yes that's it, two errors. I just could not see them.
About the result() error I guess I turned it into function, instead of a key from the $data array. Think I'm getting it slowly. Thanks very much for all the feedback.




Theme © iAndrew 2016 - Forum software by © MyBB