0

In my view i have a foreach statement which grabs all the nessesary data from the database which displays correctly.

Here is the view:

<?php

?>

    <div class="cmt-container" >
        <?php

        foreach($results as $row){
            $user = $row->user;
            $comment = $row->comment;
            $date = $row->date;
            $name = $row->name;
            $joke = $row->joke;
            $joke_id = $row->joke_id;

            // Get gravatar Image
            // https://fr.gravatar.com/site/implement/images/php/
            $default = "mm";
            $size = 35;
            $grav_url = "http://www.gravatar.com/avatar/"."?d=".$default."&s=".$size;

            ?>

            <div class="cmt-cnt">
                <img src="<?php echo $grav_url; ?>" />
                <div class="thecom">
                    <h5><?php echo $user; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
                    <br/>
                    <p>
                        <?php echo $comment; ?>
                    </p>
                </div>
            </div><!-- end "cmt-cnt" -->


        <?php

        }
        ?>

<?php

echo form_open('comments/insertComment');

?>
        <div class="new-com-bt">
            <span>Write a comment ...</span>
        </div>

        <div class="new-com-cnt">
            <input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
            <textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
            <input type="hidden" name="joke_id">
            <input class="bt-add-com" type="submit" value="Post comment">
            <div class="bt-cancel-com">Cancel</div>
        </div>
        <div class="clear"></div>
    </div><!-- end of comments container "cmt-container" -->

<?php

echo form_close();

?>

My question is, how can i pass the $joke_id variable to the insertComment function in my comments controller.

I have put the input field as hidden on the joke_id field because i want to assign the ID of a joke to a comment, so the joke will have unique comments.

4
  • you can post form to Controller action.. but $joke_id would be multiple then you should use array type name joke_id[] in form hidden element Commented Aug 13, 2014 at 13:11
  • Assuming that $results are being filtered by $joke_id in your controller, simply pass $joke_id to the view the same way that the $results are being sent. Commented Aug 13, 2014 at 13:14
  • No I have already got that. I want to be able pass the result of the $joke_id to a function in my controller Commented Aug 13, 2014 at 13:15
  • Your stated problem aside, you are closing cmt-container before you close your form, resulting in incorrect html. You should move that div closure to after <?php echo form_close(); ?>. Commented Aug 13, 2014 at 13:21

2 Answers 2

3

If the comments are on the same page as the joke, you can just take the $joke_id and put it in the hidden input:

<input type="hidden" name="joke_id" value="<?php echo $joke_id; ?>">

And when you add a comment (I assume you handle the form datas in the insertComment() function), you can access the joke id by $_POST['joke_id']. (yes, that's not really secure but if your user can comment any joke, you just have to check that a joke with the id equal to $_POST['joke_id'] exists in the DB and if so, you just insert the comment)

Is that what you wanted?

Sign up to request clarification or add additional context in comments.

Comments

0

try this..

<?php echo form_open('comments/insertComment'); ?>
        <div class="new-com-bt">
            <span>Write a comment ...</span>
        </div>

        <div class="new-com-cnt">
            <input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
            <textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
            <input type="hidden" name="jokeid" value="<?= $joke_id; ?>">
            <input class="bt-add-com" type="submit" value="Post comment">
            <div class="bt-cancel-com">Cancel</div>
        </div>
        <div class="clear"></div>
    </div><!-- end of comments container "cmt-container" -->

<?php echo form_close();?>

and in your insert comment controller

    public function __construct() {
    //codes here
    $joke_id = $this->input->post('jokeid'); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.