Welcome Guest, Not a member yet? Register   Sign In
why i get blank page ?
#1

[eluser]Evollution[/eluser]
Model

Code:
function get_article_by_id($article_id)
{
    $data = array();
    
    $this->db->select('title,news');
    $this->db->from('news');
    $this->db->where('id', $article_id);
    $query = $this->db->get();

    if ($query->num_rows() > 0)
    {
        $data = $query->row_array();
    }

    $query->free_result();

    return $data;
}

Controller
Code:
private function _show_article()
  {
     $article_id=(int)$this->uri->segment(2);
     $this->load->model('news_model');
if(!$query=$this->news_model->get_article_by_id($article_id))
{
   redirect('news');
}
else
  {
      $data['news'] = $query;
      $this->load->view('news/news_content', $data);    

}
  }
Code:
public function _remap($method)
  {
     if(method_exists($this,$method))
     {
       $this->$method();
     }
     elseif(is_numeric($method) && $method > 0)
     {
       $this->_show_article();
     }
     else
     {
       $this->index();
     }
  }

View

Code:
<!DOCTYPE html>

&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
    &lt;title&gt;untitled&lt;/title&gt;
    &lt;style type="text/css" media="screen"&gt;
        label {display: block;}
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
     $article_id=(int)$this->uri->segment(2);
?&gt;
&lt;?php echo $article_id; ?&gt;

    &lt;?php if(isset($news)) : foreach($news as $row) : ?&gt;
    
    <h2>&lt;?php echo  $row->title; ?&gt; </h2>
    <div>&lt;?php echo $row->news; ?&gt;</div>        

    &lt;?php endforeach; ?&gt;

    &lt;?php else : ?&gt;    
    <h2>No records were returned.</h2>
    &lt;?php endif; ?&gt;

    
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]d1a8lo24[/eluser]
Try this.

Model
Code:
function _get_article($article_id)
{    
    $query = $this->db
                  ->select('title,news');
                  ->where('id', $article_id);
                  ->get('news');

    if ($query->num_rows() > 0)
    {
        foreach ($query->result() as $article)
        {
           $news[] = $article;

        }
        return $news;
    }
    return redirect('news');
}

controller must be public
Code:
function show_article($article_id)
{
   $this->load->model('news_model');
  
   $data['news'] = $this->model->_get_article_by_id($article_id);
  
   $this->load->view('news/news_content', $data);
}
Don't know why would you need the remap function specially to do an integer check, you don't need the extra code but thats up to you.
Code:
<!DOCTYPE html>

&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
    &lt;title&gt;untitled&lt;/title&gt;
    &lt;style type="text/css" media="screen"&gt;
        label {display: block;}
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
This part I also don't understand??? Just doing the echo should work you will still get the same result.
Code:
&lt;?php
     $article_id=(int)$this->uri->segment(2);
?&gt;
&lt;?php echo $article_id; ?&gt;
Code:
&lt;?php if($news): ?&gt;

    &lt;?php foreach ($news as $row) : ?&gt;
    
    <h2>&lt;?php echo  $row->title; ?&gt; </h2>
    <div>&lt;?php echo $row->news; ?&gt;</div>        

    &lt;?php endforeach; ?&gt;

    &lt;?php else: ?&gt;    
    
    <h2>No records were returned.</h2>
    
    &lt;?php endif; ?&gt;
    
&lt;/body&gt;
&lt;/html&gt;

Because there was a redirect on your first example you really don't need the if statment since it will never get trigger if you decide to chage the redirect to show the page even when there is no record then the if statement will be trigger and show a no records return.

And also make sure there is something on your database.

On your code if there was nothing return you where just rerouting to the news controller/function.

Hope this helps.
#3

[eluser]Twisted1919[/eluser]
@d1a8lo24
He needs the _remap function because his articles are accessible at something like /news/1 (controller/id) avoiding the method call. I know because i gave him the advice to do so, so it is correct for his situation.
Also, the model function is almost correct, don't teach him bad stuff(the return redirect('news') part that you suggested is also wrong. The redirect() method just redirects, you can't return it.)

He can write the model function more clear and correct like:
Code:
function get_article_by_id($article_id)
{
    $query = $this->db->select('title,news')->from('news')->where('id', $article_id)->get();
    return $query->num_rows() > 0 ? $query->row() : FALSE;
}

Now, the part that triggers the error,
In his model function, he returns an array, but in the views he calls for an object and therefore the error(but with the method proposed by me, this error will be gone).
Also, the foreach() loop doesn't belongs there because the model returns only an object, therefore he doesn't have more records to loop through .

Do the fixes i recommend and you will be error free, i assure you .
#4

[eluser]d1a8lo24[/eluser]
I see what you mean and you're right that should be the prefer way of doing it, you also have to take into account that everyone wants different results in this case you have previously work with him so you know what he was trying to do.

I was just trying to follow his code.

Now the return case was just a small shortcut is not bad since it was doing the same thing hes code was triggering. The return does 2 things 1 ends the script at that point and the other is to return a string, int, an array or a TRUE or FALSE.

I can now understand why the remap but I still think is a waste of code but again is a case of what you really want to achieve. To me I will rather have new/article/id for friendly specific urls than to have news/id. But like I said it all depends on what you want.

as far as your new function which is better the only thing I would change is the from since its extra code. But again is all about preference.

Code:
function get_article_by_id($article_id)
{
    $query = $this->db->select('title,news')->where('id', $article_id)->get('news');
    return $query->num_rows() > 0 ? $query->row() : FALSE;
}
#5

[eluser]Evollution[/eluser]
ok i changed news_model code but i still get blank page
http://www.fifago.com/go/news/42/fifago-...urile.html ...
#6

[eluser]Evollution[/eluser]
Quote:new/article/id
do you mean news/$article-name/$article-id ?




Theme © iAndrew 2016 - Forum software by © MyBB