[eluser]Miguel Diaz[/eluser]
Hi Can someone help me please I am new working with models and also databases.
I have my articles in my front page which count the comments for each article that is working perfect I also create a view for each article but when I go to my article view if the article dont have a comment send me an error of Invalid argument supplied for foreach(),
Can someone tell me why and show me an example here is my code:
this is my function
function redirect()
{
$tit_post = $this->uri->segment(2);
$op['title']= 'Maik Diaz Diseñador Web y Desarrollador en México - Consultoria de Paginas Web.';
$op['description']= 'Maik Diaz diseñaor web y consultor de paginas web en mexico.';
$op['keywords']= 'Diseño Web, Desarrollador Web, Paginas web en mexico, Paginas web en cancun, Diseñadores en mexico, desarrolladores de paginas mexico.';
//Conecta a los modelos de la bd//
$op['query'] = $this->data_model->cargarPagina($tit_post);
$op['querytwo'] = $this->data_model->cargarComentarios($tit_post);
//tags de scrypt extra por pagina//
$op['tweet']= '';
$op['js']= '';
// Vista de Template
$op['content'] = 'articulos-view';
$this->load->view('includes/template-content', $op);
}
}
and this are my models
class Data_model extends Model {
function cargarArticulo(){
$q = $this->db->query("select
f.entry_id as 'entry',
a.title as 'Title',
a.url as 'Url',
a.shortdescription as 'Shortdescription',
a.date as 'Fecha',
a.graphics as 'Graphics',
count(entry_id) as Comments
from articles a left join
forocomments f
on a.url=f.entry_id
group by idArticles
desc limit 3
");
if($q->num_rows() > 0) {
foreach($q->result() as $row){
$data[] = $row;
}
return $data;
}
}
function cargarPagina($tit_post){
$q = $this->db->query("select * from articles where url='$tit_post'");
if($q->num_rows() > 0) {
foreach($q->result() as $row){
$data[] = $row;
}
return $data;
}
}
function cargarComentarios($tit_post){
$q = $this->db->query("select * from forocomments where entry_id='$tit_post'");
if($q->num_rows() > 0) {
foreach($q->result() as $row){
$data[] = $row;
}
return $data;
}
}
}
and my article view
<dl>
<?php foreach($query as $articulo) :?>
<dt><?=anchor('blog/'.$articulo->url, $articulo->title);?></dt>
<dd class="pict"><img >graphics; ?>" alt="ultimas noticias" /></dd>
<dd class="text"><p><?php echo $articulo->shortdescription; ?></p>
<em><?php echo $articulo->date; ?></em>
<?=anchor('blog/'.$articulo->url, 'read more', 'class="readmore"');?>
</dd>
<?php endforeach; ?>
</dl>
</div>
<?php foreach($querytwo as $comentario) :?>
<div class="post">
<p><?php echo $comentario->Nombre; ?></p>
<span><?php echo $comentario->Comentario; ?></span>
</div>
<?php endforeach; ?>