Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Output is wrong , i Got three the same comment instead only one. Can anyone help to make only one comment.

Here my view php code:

$modelid = "5" ;

$query = $this->db->query('SELECT message FROM message WHERE modelid = '.$modelid.' ');

$row = $query->row(); 

echo $row->message;

and my Table:

http://dev.interactive-creation-works.net/Stack/table.png

The controller:

        class Comment extends CI_Controller
{
    function index()
    {
        $data['result'] = $this->db->get('message')->result();
        $this->load->view('commentView',$data);

    }
    function insert()
    {

     $this->load->model('commentjquery');
     echo $this->commentjquery->inserttodb();
    }
}
share|improve this question
    
what does this query do when you use in mysql? What is your db structure? What is in db? –  Robert May 30 '13 at 21:11
    
sry couldnt posted an image here the link to tghe structure it should only list the comment message where the modelid 5 is –  dA_uNknOwN May 30 '13 at 21:13
    
How are you calling this code? –  Rocket Hazmat May 30 '13 at 21:15
    
@RocketHazmat: This is apparently a CodeIgniter view, so presumably it's being loaded and used by a controller function. The controller could be the problem, though - mind posting it, dA? –  user1618143 May 30 '13 at 21:17
2  
Wait. A view?! NO! Don't call the database in the view! Use the models for that. –  Rocket Hazmat May 30 '13 at 21:19

2 Answers 2

up vote 0 down vote accepted

This view should only show one message, even if you do - for whatever reason, correctly or not - get three messages from the query. You may be loading the view three times in your controller. Try adding some static text to the top and bottom of your view, and see if it shows up multiple times.

Edit:

Now that you've posted the controller, I can see that's not the issue. But I notice you're trying to grab the comments from the database twice - once in the controller and once in the model. The one in the controller is actually the wrong way to do it unless you've implemented an ActiveRecord model for your comments, and you're not using the results anyway. Another thing I noticed just now is that you're calling $query->row() instead of running a foreach over $query->result(). Try this:

foreach($query->result() as $row) {
    echo $row->message;
}

Edit 2: Or maybe it is the problem, if the code snippet you posted isn't the entire view, which seems to be the case.

share|improve this answer
    
ok , i will try add some text tu check it out –  dA_uNknOwN May 30 '13 at 21:24
    
Ok the echo of textsample is threetimes theire too –  dA_uNknOwN May 30 '13 at 21:27
    
thank you i found the problem i forget to remove a foreach and the end of the foreach –  dA_uNknOwN May 30 '13 at 21:30
    
ok i will try. at the moment the output is only one time but it gives not all rows out whch have the modelid 5 –  dA_uNknOwN May 30 '13 at 21:37
    
Thank your very much , Your Code works i see now all rows with the right modelid. thank you for your time =) –  dA_uNknOwN May 30 '13 at 21:39

It's really weird that you aren't getting error. It gets weirder when you get three outputs.

Change your query to this.

$query = $this->db->query('SELECT message FROM message WHERE modelid = "'.$modelid.'"');
share|improve this answer
    
ty for your anwser but i got the solution from user1618143 –  dA_uNknOwN May 30 '13 at 21:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.