Welcome Guest, Not a member yet? Register   Sign In
query big problem
#11

[eluser]xwero[/eluser]
The method i showed returns a result object not a query object. This means you can use the result method num_rows anymore but you can use count.
#12

[eluser]polish[/eluser]
Xwero cam You lokinng on my controller function what I must change so that it is ok???

Code:
function show()
    {
    $news_id = $this->uri->segment(3);
    $this->response['content'] = '<h1><center><a href="'.site_url('comment/com_add/'.$news_id).'">Comments</a><center></h1>';
    IF(isset($news_id) and is_numeric($news_id))
        {
        $this->load->model('Comments');
        $output = $this->Comments->get_news_item($news_id);
        $content = '';
        $adm = Comment::_check_login();
        if ($output->num_rows() > 0)
            {
            foreach($query->result() as $item)
                    {
                    $item->admin = $adm;
                    $this->response['content'] .= $this->load->view('com/com_loop', $item, True);
                    }
            }
        else
            {
                $content = '<h1><center>No Comments</center></h1>';
            }
        //$this->response['content'] = $content;
        }
    else
        {
        $this->response['content'] = '<h1><center>Bad Link</center></h1>';
        }
    $this->load->view('index', $this->response);
    }

This is My first application and I will be very grateful for the help me!
#13

[eluser]xwero[/eluser]
I find it a bit strange you going to put the method in a model named comments. If you have a comments model you must have a news model so you should call news->by_id($news_id) and comments->for_news_id($news_id).

The idea about splitting up code is to make it simpler.

My method was written to belong to the news model. The controller method would look like this
Code:
function show($news_id = '')
{
    // no id no news item
    if($news_item == '')
    {
       redirect('news');  
    }
    
    $news_item = $this->news->by_id($news_item,true);
    // if there are no properties there is no news item
    if( ! isset($news_item->news_text) )
    {
       redirect('news');
    }

    $content = array();
    
    if(count($news_item->comments) > 0)
    {
        foreach($news_item->comments as $comment)
        {
            $content['comments'] .= $this->load->view('com/com_loop', $comment, true);
        }
    }
    // comments property is no longer needed
    unset($news_item->comments);
    
    $this->load->view('index', array_merge($content,(array)$news_item));
}
#14

[eluser]TheFuzzy0ne[/eluser]
Here's a post I created with several resources to help get people in your position started with CodeIgniter - http://ellislab.com/forums/viewthread/107773/
#15

[eluser]polish[/eluser]
Xwero I have in My news model function called news->by_id($news_id) and comments->for_news_id($news_id) like:

Code:
function get_news_by_id($news_id)
        {
        $this->db->where('news_id', $news_id);
        return $this->db->get('news');
        }

Code:
function are_comments_for_news($news_id)
    {
    return $this->db->query("SELECT COUNT(*) AS comnumber FROM ".$this->db->dbprefix."comments WHERE news_id = '".$news_id."'");
    }

Now when I use Your controller show function and Your model function:

Code:
function get_news_item($id,$comments = false)
{
    $output = $this->db->query('SELECT news_text FROM news WHERE id=?',array($id))->row();

    if($comments == true)
    {
        $output->comments = $this->db->query('SELECT * FROM comments WHERE news_id=?',array($id))->result();
    }
    return $output;
}

I have two errors.

1. Undefined variable: news_item in My controller file and
2. Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\polish\system\libraries\Exceptions.php:164)
#16

[eluser]xwero[/eluser]
If you have a get_news_by_id method you don't need to have the unchanged get_news_item method. In you comments model you just add a for_news_id method
Code:
function for_news_id($id)
{
    return $this->db->query('SELECT * FROM comments WHERE news_id=?',array($id))->result();
}
And your controller then can look like
Code:
function show($news_id = '')
{
    // no id no news item
    if($news_item == '')
    {
       redirect('news');  
    }
    
    $data['news_item'] = $this->news->get_news_by_id($news_id);
    // if there are no properties there is no news item
    if( ! isset($data['news_item']->news_text) )
    {
       redirect('news');
    }

    $data['comments'] = $this->comments->for_news_id($news_id);
    
    $this->load->view('index', $data);
}
Now you see how splitting a method in two parts makes your controller code more readable.

The errors you got were the result of mistakenly typing $news_item instead of $news_id as parameter for the by_id model method. I type mistakes too so check the code yourself if you copy paste it, you learn the best by finding out what went wrong.
#17

[eluser]polish[/eluser]
Now I have no mistakes but working is no good! I check all parametr and function, all names!

With the view news_loop:

Code:
<div class="czas">&lt;?=$data_wpisu ?&gt;</div>
<div class="tytul">&lt;?=$news_title ?&gt;</div>
<div class="text">&lt;?=$news_text ?&gt;</div>
<div class="autorczas">
<span class="newsat"><u>&lt;?=$news_author ?&gt;</u>&nbsp;(&lt;?=$news_time ?&gt;)</span>
<span class="comlink">
&lt;?PHP
IF($comments == 0)
{
echo '<a href="'.site_url('comment/com_add/'.$news_id).'">Skomentuj</a>';
}
else
{
echo '<a href="'.site_url('comment/com_add/'.$news_id).'">Skomentuj</a> | <a href="'.site_url('comment/show/'.$news_id).'">Comments ('.$comments.')</a>';
}
?&gt;
</span>
</div>

I am listing all news and when I click Comments I need to load view com_loop who listing me the only one news and all comments for this news.

This is code from com_loop view:

Code:
&lt;?PHP echo $news_text; ?&gt;<br /><br />
<img src="&lt;?php echo base_url(); ?&gt;images/ico_wtk.gif" alt="Ikonka" />
<div class="comtyt">&lt;?PHP echo $com_title ?&gt;</div><br />
<div class="text">&lt;?PHP echo $com_text; ?&gt;</div>
<div class="comtime"><span class="newsat">~&lt;?PHP echo $com_author; ?&gt;&nbsp;&lt;?PHP echo $com_date; ?&gt;</span>
<span class="comlink" style="padding-right: 10px;"></span>
</div>

I use function show(), for_news_id(), get_news_by_id() with right parameters and the result is not OK! How to connect that's all in a one working result???
:down: :down: :down:
#18

[eluser]xwero[/eluser]
You still loop the news_text in the comments. the for_news_id method i wrote doesn't have news_text anymore.

Also the comments variable that gets passed to the view needs the html of the comments on the news_item page.
#19

[eluser]polish[/eluser]
What can I do now??? Create a new view or controller file??? What I must still change??? I need solution that when I click Comments I would like to open a view with one news and all comments!
#20

[eluser]polish[/eluser]
I have the solution. Maybe it isn't optiumum but he is working ok! I created a new view com_news who listing Me the only one news and the view com_loop who loop Me all comments for this newsSmile!

This is the working controller function:

Code:
function show()
    {
    $this->load->model('Comments');
    $this->load->model('News');
    $news_id = $this->uri->segment(3);
    IF(isset($news_id) and is_numeric($news_id))
        {
        $query = $this->News->news_for_com($news_id);
        $query1 = $this->Comments->get_comments_for_news($news_id);
        $content = '';
        if ($query->num_rows() == 1)
            {
            foreach($query->result() as $item)
                    {
                    $content .= $this->load->view('com/com_news', $item, True);
                    }
            }
        if ($query1->num_rows() > 0)
            {
            foreach($query1->result() as $item)
                    {
                    $content .= $this->load->view('com/com_loop', $item, True);
                    }
            }
        else
            {
                $content = '<h1><center>No Comments</center></h1>';
            }
        }
    else
        {
        $this->response['content'] = '<h1><center>Bad Link</center></h1>';
        }
        $this->response['content'] = $content;
    $this->load->view('index', $this->response);
    }


The topic is SOLVED!!!




Theme © iAndrew 2016 - Forum software by © MyBB