Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6109839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:26:57+00:00 2026-05-23T14:26:57+00:00

First all, I am still learning CakePHP and I am close to ZERO using

  • 0

First all, I am still learning CakePHP and I am close to ZERO using JQuery.

I would like to get help for the following problem I am having:

  1. I have an Articles_Controller a Comments_Controller and Users_Controller
  2. Currently my articles_controller display an article and its comments are loaded on that page
  3. Whoever user inputted the comment will also appear along with his/her image
  4. On each comment I have added a like/dislike button.

Example: Comment Screenshot

Right now, however, I am only able to display it. What I wanted to accomplish is to use JQuery so that when a user clicks on the Thumbs Up or Thumbs Down image the like and disliked fields are automatically updated in the Db. Also, using JQuery I would like to update those same values in the View. Please my code below and thank you for your help.

articles/view.ctp

<div id="articles_comments">

                    <p>
            Comments
        </p>
        <?php
            foreach($comments as $comment){
        ?>
        <div id="articles_comments_user">
            <img src="<?php echo $comment['User']['image']; ?>">
            <p>
            <?php
                $created = $comment['Comment']['created'];
                echo $comment['User']['first_name'];
                echo "&nbsp;";
                echo $comment['User']['last_name'];
                echo "&nbsp;&nbsp;&nbsp;";
                //echo $comment['Comment']['created'];
                echo $this->Time->timeAgoInWords(
                    $comment['Comment']['created'], 
                    array(
                        'end'=>'+150 years'
                    )
                );

            ?>
            <p>
        </div>
        <div id="articles_comments_comment">
            <table>
                <tr>
                    <td style="width:85%">
                        <?php echo $comment['Comment']['comment'];?>
                    </td>
                    <td style="width:15%;border-left:solid thin teal;">
                        <div style="float:left;">
                            <?php echo $comment['Comment']['liked'];?>
                            <img width="20" src="/img/icons/thumb-up-icon.png"
                        </div>
                        <div style="float:right;margin-left:10px;">
                            <?php echo $comment['Comment']['disliked'];?>
                            <img width="20" src="/img/icons/thumb-down-icon.png">
                        </div>
                    </td>
                </tr>
            </table>
        </div>
        <?php
            }
        ?>
        <div class="articles_add_comment" id="formUpdateID">
            <hr style="width:100%"><br>

            <div style="float:left">
                <h3>Seu Commentario:</h3> 

            <?php
            echo $form->create(
                     'Comment',
                     array(
                           'url'=>array(
                                'controller'=>'articles',
                                'action'=>'view',
                                $article['Article']['id']
                                ),
                           'class' => 'articles_form',
                           'id' => 'loginForm'
                           )
                     );
            echo $form->input(
                'Comment.comment',
                array(
                    'label' => false,
                    'type' => 'textarea',
                    'div' => 'articles_comments_textarea',
                    'cols' => 90,
                    'rows' => 3
                    )
                );
            ?>
            </div>
            <div style="float:right">

            <?php
                echo $this->Form->submit(
                             'Send',
                             array(
                                   'class' => 'articles_comments_button'
                                   )
                            );
                echo $form->end();
            ?>
            </div>
            <div class="ajax-save-message">
                <?php echo $this->Session->flash(); ?>
            </div>
        </div>
    </div>

Comments is generated from the Articles View action

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:26:57+00:00Added an answer on May 23, 2026 at 2:26 pm

    I was able to fix my problem by the following upon some research and try outs:

    View file

    <?php foreach $commments as $commment{ ?>
    
    // .............................................................................
    
    <td style="vertical-align:middle;border-left:solid thin teal;padding-left:5px;">
    <div class="voteup" style="margin-left:10px;float:left;width:55px;">
        <p style="display:none;float:left">
            <?php echo $comment['Comment']['id']; ?>
        </p>
        <div style="float:left" id="article_thumbsUp">
            <?php echo $comment['Comment']'liked'];?>           
            </div>                                                      
            <img width="20" src="/img/icons/thumb-up-icon.png">
    </div>
        <div class="votedown" style="float:left;width:55px;">
        <p style="display:none;float:left">
            <?php echo $comment['Comment']['id']; ?>
        </p>
        <div style="float:left" id="article_thumbsDown">
            <?php echo $comment['Comment']'disliked'];?>                                
        </div>                      
        <img width="20" src="/img/icons/thumb-down-icon.png">
    </div>
    </td>
    
    // ...............................................................................
    
    <?php } ?>
    

    jQuery I used

    <script>
    $(document).ready(function(){
        $(".voteup").click(function() {
            var Id = $(this).children("p").text();
            $(this).children("#article_thumbsUp").load("/comments/voteup/"+Id);
        });
    });
    </script>
    
    <script>
    $(document).ready(function(){
        $(".votedown").click(function() {
            var Id = $(this).children("p").text();
            $(this).children("#article_thumbsDown").load("/comments/votedown/"+Id);
        });
    });
    </script>
    

    And both of my actions in my comments_controller.php

    function voteUp($id = null){    
        $this->autoRender = false; 
        if($this->RequestHandler->isAjax()){
            $this->Comment->id = $id;
            if($this->Comment->saveField('liked',$this->Comment->field('liked')+1)){
            }
        }       
        $newValue =  $this->Comment->findById($id);     
        return $newValue['Comment']['liked'];
    }
    
    function voteDown($id = null){  
        $this->autoRender = false;  
        if($this->RequestHandler->isAjax()){
            $this->Comment->id = $id;
            if($this->Comment->saveField('disliked',$this->Comment->field('disliked')+1)){      
            }
        }       
        $newValue =  $this->Comment->findById($id);     
        return $newValue['Comment']['disliked'];        
    }
    

    This is my entire code in detail and hopefully it can help someone else. This is the way I know how to do, and if you have any better way, I was be glad to know. Thanks a lot. THIS WORKS GREAT. I JUST NEED TO ADD A FEW MORE THINGS TO THIS SYSTEM, SUCH AS ONLY ALLOW REGISTERED USERS TO VOTE AND ONLY ONCE PER COMMENT. THAT WILL INVOLVE CREATING ANOTHER TABLE TO TRACK THAT.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using re in Python, I would like to return all of the characters in
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
First of all, a disclaimer: I have experience in other languages, but am still
I'm still learning Sharepoint and all of its various concepts, so apologies in advance
I am still learning Perl scripting and need help in doing below task. Sample
First of all this is homework. I have been trying to get rid of
Its my first project with PHP, I am still learning phase. Designer is working
First of all, I'm using OpenGL ES 1.1 and not 2.0, mostly because I've
First let me appologize if my description of this is completely retarded, still learning
First of all, this is probably a stupid question, but I'm new and still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.