Welcome Guest, Not a member yet? Register   Sign In
Help with a model
#1

[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>
&lt;?php foreach($query as $articulo) :?&gt;
<dt>&lt;?=anchor('blog/'.$articulo->url, $articulo->title);?&gt;</dt>
<dd class="pict"><img >graphics; ?&gt;" alt="ultimas noticias" /></dd>
<dd class="text"><p>&lt;?php echo $articulo->shortdescription; ?&gt;</p>
<em>&lt;?php echo $articulo->date; ?&gt;</em>
&lt;?=anchor('blog/'.$articulo->url, 'read more', 'class="readmore"');?&gt;
</dd>
&lt;?php endforeach; ?&gt;
</dl>
</div>
&lt;?php foreach($querytwo as $comentario) :?&gt;
<div class="post">
<p>&lt;?php echo $comentario->Nombre; ?&gt;</p>
<span>&lt;?php echo $comentario->Comentario; ?&gt;</span>
</div>
&lt;?php endforeach; ?&gt;
#2

[eluser]theprodigy[/eluser]
A couple of things:
1. please wrap your code in the code tags. It makes it much easier to read.

2. in your models, initialize your arrays:
Code:
function cargarPagina($tit_post){
      $data = array();   // <---- Add this to each of your functions that return a data array.
      $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;
      }
  }
#3

[eluser]Miguel Diaz[/eluser]
Hi thank you for your answer and sorry for the code tag I already add the $data = array(); and the page view is still sending me the error.

Here is the code

My Controller
Code:
function redirect()
    {
        $tit_post = $this->uri->segment(2);
        $op['title']= '';
        $op['description']= '';
        $op['keywords']= '';
        
        //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);
    }
    
}

database model
Code:
class Data_model extends Model {

    function cargarArticulo(){
        $data = array();
        $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){
        $data = array();
        $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){
        $data = array();
        $q = $this->db->query("select * from forocomments where entry_id='$tit_post'");
        if($q->num_rows() > 0) {
            foreach($q->result() as $row){
                $data[] = $row;
            }
            $q->free_result();  
            return $data;
        }
    }

}
and my view
Code:
&lt;?php foreach($query as $articulo) :?&gt;
              <dt>&lt;?=anchor('blog/'.$articulo->url, $articulo->title);?&gt;</dt>
              <dd class="pict"><img >graphics; ?&gt;" alt="ultimas noticias" /></dd>
              <dd class="text"><p>&lt;?php echo $articulo->shortdescription; ?&gt;</p>
              <em>&lt;?php echo $articulo->date; ?&gt;</em>
              &lt;?=anchor('blog/'.$articulo->url, 'read more', 'class="readmore"');?&gt;
              </dd>
              &lt;?php endforeach; ?&gt;
            </dl>
            </div>
            &lt;?php foreach($querytwo as $comentario) :?&gt;
        <div class="post">
        <p>&lt;?php echo $comentario->Nombre; ?&gt;</p>
        <span>&lt;?php echo $comentario->Comentario; ?&gt;</span>
        </div>
    &lt;?php endforeach; ?&gt;
#4

[eluser]theprodigy[/eluser]
I think I see why:
Code:
function cargarComentarios($tit_post){
    $data = array();
    $q = $this->db->query("select * from forocomments where entry_id='$tit_post'");
    if($q->num_rows() > 0) {
        foreach($q->result() as $row){
            $data[] = $row;
        }
        $q->free_result();  
        return $data; // <---- You are only returning $data if num_rows() > 0
    }
}

Move your return statement outside of the if statement (in all your $data functions). You want to ALWAYS return $data (so you are always returning an array)
#5

[eluser]tonanbarbarian[/eluser]
change your model code to work like this
Code:
function cargarComentarios($tit_post){

    $q = $this->db->query("select * from forocomments where entry_id='$tit_post'");
    return $q->result();
}

the problem is that your model code is only returning something if there are results i.e.
Code:
if($q->num_rows() > 0) {
there is nothing returned if that evaluates to false

Code:
$q->result()
returns an array or records, or an empty array if no records found
so just returning that in the model will return an empty array and the foreach will not have a problem
the foreach inside your modell processing is just extra work that is not needed since ->result() is returning an array or records anyway
#6

[eluser]Miguel Diaz[/eluser]
Thank you very much prodigy that was a great help.




Theme © iAndrew 2016 - Forum software by © MyBB