Welcome Guest, Not a member yet? Register   Sign In
display db data from to tables on one page
#1

[eluser]matches[/eluser]
Hello,

I am trying to load data from two tables one is being loaded in index function like so:

Code:
function index()
{
    $id = $_GET['id'];
        
    $sql = "SELECT * FROM entries WHERE id = $id";
    $data['query'] = $this->db->query($sql);        
    $this->load->view('permalink/index', $data);        
}

This gives me an error. I am guessing because of of the $this

error:
Quote:Fatal error: Using $this when not in object context in C:\xampp\htdocs\IanSeabock\system\application\controllers\permalink.php on line 35

Code:
function showComments($blogId)
{
    $sql = "SELECT * FROM comments WHERE blogId = $blogId";
    $data['query'] = $this->db->query($sql);        
    $this->load->view('permalink/index', $data2);
            
    foreach($query->result() as $commentRow):
    echo '<div class="blogEntry">';
    echo '    <h2>' . $commentRow->author . '</h2>';
    echo '    <h3>' . $commentRow->comment . '</h3>';
    echo '</div>';
    endforeach;
                            
}

Thanks for any help
#2

[eluser]Jamie Rumbelow[/eluser]
Have you added a constructor, and if so, have you placed the "parent::Controller()" function inside it?
#3

[eluser]matches[/eluser]
Yeah, I have. I guess I should have just pasted the entire controller. I will as soon as I get home. Thanks for you help.
#4

[eluser]matches[/eluser]
Here is the full class.

The function show comments is causing the error.

Code:
&lt;?php

class permalink extends Controller {
    
    function permalink()
    {
        parent::Controller();
        
        $this->load->database();
        $this->load->helper(array('form', 'url', 'date'));
        
        function commentForm($blogId)
        {
            $comment = array(
              'name'    => 'comment',
              'id'        => 'comment',
            );
            $author = array(
              'name'    => 'author',
              'id'        => 'author',
            );
            echo form_open('permalink/addComment');
            echo form_label('Author', 'author'), "<br />";
            echo form_input('author'), "<br />";
            echo form_label('Comment', 'comment'), "<br />";
            echo form_textarea($comment), "<br />";
            echo form_submit('mysubmit', 'Submit Post!');
            echo form_hidden('blogId', $blogId);
            echo form_close();
        }
        
        function showComments($blogId)
        {
            $sql = "SELECT * FROM comments WHERE blogId = $blogId";
            $data['query'] = $this->db->query($sql);        
            
            foreach($query->result() as $commentRow):
            echo '<div class="blogEntry">';
            echo '    <h2>' . $commentRow->author . '</h2>';
            echo '    <h3>' . $commentRow->comment . '</h3>';
            echo '</div>';
            endforeach;
                            
        }    
    }
    
    function index()
    {
        $id = $_GET['id'];
        
        $sql = "SELECT * FROM entries WHERE id = $id";
        $data['query'] = $this->db->query($sql);        
        $this->load->view('permalink/index', $data);        
    }
    
    function addComment()
    {
        $author = $_REQUEST['author'];
        $comment = $_REQUEST['comment'];
        $blogId = $_REQUEST['blogId'];
        
        $sql = "INSERT INTO comments (author, comment, blogId) VALUES (".$this->db->escape($author).", ".$this->db->escape($comment).", ".$this->db->escape($blogId).")";
        $this->db->query($sql);
        
        header('location:' . base_url() . "/index.php/blogEntries/");
    }
    
}    

?&gt;
#5

[eluser]Flemming[/eluser]
I think you need to close your constructor like this:
Code:
function permalink()
    {
        parent::Controller();
        
        $this->load->database();
        $this->load->helper(array('form', 'url', 'date'));
    }

and then start your next function, rather than containing everything within the constructor?
#6

[eluser]matches[/eluser]
Then I get the error

Quote:
Fatal error: Call to undefined function showComments()
#7

[eluser]Jamie Rumbelow[/eluser]
Why not create the comment form in a view?
#8

[eluser]matches[/eluser]
I figured that would be bad practice putting an sql query into the view. Maybe I'm wrong Smile
#9

[eluser]Colin Williams[/eluser]
Your PHP is substantially flawed. Consider reading about classes and functions at http://www.php.net

Also, SQL should typically stay in the Controller, or even better yet, in a Model. You can then pass the results on to the view, or call the model directly from the view. I assume you haven't thoroughly read the Model, Views, and Controllers pages in the User Guide?
#10

[eluser]matches[/eluser]
Thanks for the help Colin! (that's sarcasm if you can't tell Wink )




Theme © iAndrew 2016 - Forum software by © MyBB