Welcome Guest, Not a member yet? Register   Sign In
Select View
#1

[eluser]ealonw[/eluser]
hey guys was trying to do a select view but Im not quite sure of how to dosplay that... Can you look at the class below and tell me what im missing?



class Contact extends Controller {

function index()
{
$this->load->helper(array('form', 'url'));


// Produces: SELECT * FROM mytable
$query = $this->db->get('usercomment');
foreach ($query->result() as $row)
{
echo $row->fname;
echo $row->lname;
echo $row->email;
echo $rom->comment;
}

$this->load->view('contact_view');



}
}

Im getting Fatal error: Call to a member function get() on a non-object in /var/www/html/codeigniter/system/application/controllers/contact.php on line 11
#2

[eluser]mironcho[/eluser]
Is the database library loaded? If not, then load it:
Code:
class Contact extends Controller {

function index()
{
  $this->load->library('database');
  $this->load->helper(array(’form’, ‘url’));

  // Produces: SELECT * FROM mytable
  $query = $this->db->get(’usercomment’);
  foreach ($query->result() as $row)
  {
    echo $row->fname;
    echo $row->lname;
    echo $row->email;
    echo $rom->comment;
  }

  $this->load->view(’contact_view’);

}
}
#3

[eluser]ealonw[/eluser]
Hmmmm... Now im getting a 404.. but the page is there.. Below is the view code.. What am I missing here?

<html>
<head>Comment View</head>
<body>
<?php foreach ($query->result() as $row): ?>

<?=$row->fname?>
<?=$row->lname?>
<?=$row->email?>
<?=$row->comment?>
<?php endforeach;?>
</body>
</html>
#4

[eluser]stuffradio[/eluser]
Well one thing is that you usually wouldn't do the foreach statement in the controller. Also you need to pass the $query variable to the view.

Code:
class Contact extends Controller {

function index()
{
$this->load->helper(array(’form’, ‘url’));

// Produces: SELECT * FROM mytable
$query = $this->db->get(’usercomment’);

$this->load->view(’contact_view’, $query);

}
}

In your view you go:

Code:
<html>
<head><title>Comment View</title></head>
<body>
<?php foreach ($query->result() as $row): ?>

<?=$row->fname?>
<?=$row->lname?>
<?=$row->email?>
<?=$row->comment?>
<?php endforeach;?>
</body>
</html>

It should work Smile
#5

[eluser]ealonw[/eluser]
Wow... I still got a 404... This is so simple yet so confusing... What could be missing?


**********CONTROLLER*********
<?php

class Contact extends Controller {

function Contact()
{
parent::Controller();
$this->load->library('database');
$this->load->helper(array('form', 'url'));
$this->load->scaffolding('usercomment');
}
}

function index()
{


$this->load->helper(array('form', 'url'));

// Produces: SELECT * FROM mytable
$query = $this->db->get('usercomment');

$this->load->view('contact_view', $query);


}



*********VIEW********
<html>
<head>Comment View</head>
<body>
<?php foreach ($query->result() as $row): ?>

<?=$row->fname?>
<?=$row->lname?>
<?=$row->email?>
<?=$row->comment?>
<?php endforeach;?>
</body>
</html>
#6

[eluser]ealonw[/eluser]
Is there anyone who can help...? Tons of smart folks out there... Who knows what Im missing?
#7

[eluser]stuffradio[/eluser]
You closed the Contact class off really early.
Code:
**********CONTROLLER*********
<?php

class Contact extends Controller {

function Contact()
{
parent::Controller();
$this->load->library('database');
$this->load->helper(array('form', 'url'));
$this->load->scaffolding('usercomment');
}

function index()
{

$this->load->helper(array('form', 'url'));

// Produces: SELECT * FROM mytable
$query = $this->db->get('usercomment');

$this->load->view('contact_view', $query);

}

}

Also in the View don't you need semicolons after $row->fname, $row->lname, etc.?
#8

[eluser]ealonw[/eluser]
Hello stuff radio... Okay so you say I closed it off early? Can you explain?
#9

[eluser]ealonw[/eluser]
Okay I moved the curly to the end of the class like you had it.... I can now get to the contact_view file... Wow how specific!! I didnt put (Wink and now Im getting a new error...


Fatal error: Call to a member function result() on a non-object in /var/www/html/codeigniter/system/application/views/contact_view.php on line 4
#10

[eluser]stuffradio[/eluser]
Not too sure why that error comes up... but you don't need to load the helper in each function. Once it's loaded in the function Contacts() you don't need to type it again! Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB