Show result but i got a php error..why? - El Forum - 06-30-2008
[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><?php echo anchor('front/leer/'.$row['id'],$row['titulo']);?></span>
<span><?php echo $row['intro'];?></span>
<span><?php echo $row['categoria'];?></span>
<?php endforeach; ?>
Please where is my problem?
Show result but i got a php error..why? - El Forum - 06-30-2008
[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;
}
Show result but i got a php error..why? - El Forum - 06-30-2008
[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
<?php foreach ($articulos as $articulo): ?>
<span><?php echo anchor('front/leer/'.$articulo['id'],$articulo['titulo']);?></span>
<span><?php echo $articulo['intro'];?></span>
<span><?php echo $articulo['categoria'];?></span>
<?php endforeach; ?>
Show result but i got a php error..why? - El Forum - 06-30-2008
[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.
|