• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Show result but i got a php error..why?

#1
[eluser]Asinox[/eluser]
Hi, i want to display all articles and group_by and sort_by, well i get all articles but y get too php error...why?

errors :
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: views/front_view.php

Line Number: 19
----------------------------------------------
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/front_view.php

Line Number: 19


my controller function (function articulos)
Code:
class Front extends Controller{
        
            function Front(){
                parent::Controller();
                $this->load->model("");  // header
                $this->load->model("");  // columna derecha
                $this->load->model("");  // columna central
                $this->load->model("");     // columna izquierda
                $this->load->model("");  // pie    
                $this->load->helper('url');            
                $data['SITE_TITULO'] = 'Paso a Paso Online';    
                $this->load->view('front_view', $data);                                        
            }
            
            function index(){
                $this->load->helper('url');
                $this->articulos(); // por defecto muestra todos los articulos
            }
            
         function articulos() { //listar todos los articulos
            $this->load->database();
            $this->load->model('front_model','', TRUE);
            $data['query'] = $this->front_model->get_articulos();
            $this->load->view('front_view',$data);
              }
        
        
        
        /*function leer($id=false){ //lee articulo particular
            if (!$id) redirect('front/articulos');
            $this->load->model('articulos/articulos_model','', TRUE);
            $data['articulo'] = $this->front_model->get_articulo($id);
            $this->load->view('leer_view',$data);            
        }*/

            
    }

my model
Code:
class Front_model extends Model{
    
        function Front_model(){
            parent::Model();
            
        }
    
        function get_articulos(){
            $this->db->select('*');
            $this->db->from('articles',0,30);
            $this->db->join('comments', 'comments.id_comment = articles.id');
            $this->db->join('categories', 'categories.id = articles.category_id');
            $this->db->group_by('categoria');
            $this->db->order_by('articles.id','desc');
            $query = $this->db->get();
            //return $query->result();
            
            foreach($query->result_array() as $row){
                $result[] = $row;
            }
            
        return $result;
        }
        
    }
my view
Code:
<?php foreach ($query as $row): ?>
    <span>&lt;?php echo anchor('front/leer/'.$row['id'],$row['titulo']);?&gt;</span>
    <span>&lt;?php echo $row['intro'];?&gt;</span>
    <span>&lt;?php echo $row['categoria'];?&gt;</span>

&lt;?php endforeach; ?&gt;

Please where is my problem?

#2
[eluser]Amzad Hossain[/eluser]
Try This :

Code:
function get_articulos(){
            $this->db->select('*');
            $this->db->from('articles',0,30);
            $this->db->join('comments', 'comments.id_comment = articles.id');
            $this->db->join('categories', 'categories.id = articles.category_id');
            $this->db->group_by('categoria');
            $this->db->order_by('articles.id','desc');
            $query = $this->db->get();
            //return $query->result();

            // Modification ....
            $result = array();

            foreach($query->result_array() as $row){
                $result[] = $row;
            }
            
        return $result;
        }

#3
[eluser]xwero[/eluser]
In your controller you load the url helper in your constructor and in the index method and you don't have to load the database explicit if you set the third parameter of the load->model method to TRUE.
If you want to redirect the index method you better do it with the url helper redirect function instead of calling the function in the index method.

In the model method get_articulos the foreach loop isn't necessary you can do
Code:
return $query->result_array();


I don't think one of those things is related to your problem but i don't see anything in you code that doesn't pass the query variable.

A readability tip: it's better to use specific names instead of generic.
Code:
$data['articulos'] = $this->front_model->get_articulos();
// and
&lt;?php foreach ($articulos as $articulo): ?&gt;
    <span>&lt;?php echo anchor('front/leer/'.$articulo['id'],$articulo['titulo']);?&gt;</span>
    <span>&lt;?php echo $articulo['intro'];?&gt;</span>
    <span>&lt;?php echo $articulo['categoria'];?&gt;</span>

&lt;?php endforeach; ?&gt;

#4
[eluser]Pascal Kriete[/eluser]
EDIT: xwero beat me to it

The result_array function returns an array so why are you copying it over again?
Code:
$result = $query->result_array();

Or even better
Code:
return $query->result_array();

That should sort out your error as well.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.