Welcome Guest, Not a member yet? Register   Sign In
Problem with view db query
#1

[eluser]finord[/eluser]
Hello, first sorry for my english, i dont speak very good Sad

I try to load a mysql, i dont know why dont work this script. I check the forum and documents but dont know why dont work.

Here is the code:

Controller:
Code:
<?

class noticias extends Controller
   {

    function index()
        {
    

       $this->load->model('newstransit');

        $data['query'] = $this->newstransit->allview();
        $this->load->view('blogview', $data);    
         }
}

Model:
Code:
class newstransit extends Model

{
            var $idnews ='';
            var $titulo ='';
  function notSend()
     {
       parent::Model();
     }
    
    function allview ()
        {


        
      $query = $this->db->get('news');
           return $query->result();
      
      // Para llamar ->model(alview, $var)
      
       }


}

And View:
Code:
<?
foreach ($data as $row) {
  echo $row->idnews;
}
?>

This is the error:
Quote: A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/blogview.php

Line Number: 12
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/blogview.php

Line Number: 12
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/blogview.php

Line Number: 12

If anyone can help me, thanks.
#2

[eluser]Sean Murphy[/eluser]
[quote author="finord" date="1202583242"]
Code:
$data['query'] = $this->newstransit->allview();
[/quote]

When you pass an array to a view, indexes from that array are imported into the current symbol table.

http://ellislab.com/codeigniter/user-gui...views.html
http://us2.php.net/manual/en/function.extract.php

Your view, instead of being:
[quote author="finord" date="1202583242"]
Code:
foreach ($data as $row) {
[/quote]

Should be:
Code:
foreach ($query->result() as $row) {

And the allview() method in your model should return just $query, not $query->result()
#3

[eluser]snurb[/eluser]
$data should be $query in the blogview file.
#4

[eluser]finord[/eluser]
Thanks, now work ^^
#5

[eluser]Edemilson Lima[/eluser]
Quote:class noticias extends Controller
class newstransit extends Model

Class names must start with an uppercase:

Code:
class Noticias extends Controller

class Newstransit extends Model

Also, if you use a constructor and want it to work with PHP4, the function name must be the same of the class and start with an uppercase too:

Code:
function Noticias() {
}

funtion Newstransit() {
}

http://ellislab.com/codeigniter/user-gui...llers.html
#6

[eluser]finord[/eluser]
[quote author="Edemilson Lima" date="1202678492"]
Quote:class noticias extends Controller
class newstransit extends Model

Class names must start with an uppercase:

Code:
class Noticias extends Controller

class Newstransit extends Model

Also, if you use a constructor and want it to work with PHP4, the function name must be the same of the class and start with an uppercase too:

Code:
function Noticias() {
}

funtion Newstransit() {
}

http://ellislab.com/codeigniter/user-gui...llers.html[/quote]

Ahh ok thanks, im a newbie Programming with framework ^^




Theme © iAndrew 2016 - Forum software by © MyBB