Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Like unlike system

Hello,

i am working on a like / unlike system and i have problems in Volt.

i can delete and insert data in controler, but when i want to display the data, i get it multiple times or if the data are empty i get nothing.

So, If loggedUserId == likes.clickedId i want to display <i class="fa fa-heart-o" aria-hidden="true"></i>, but it displays <i class="fa fa-heart" aria-hidden="true"></i>.

If loggedUserId == likes.clickedId is empty I want to display <i class="fa fa-heart" aria-hidden="true"></i>, but it displays nothing.

{% for likes in user.likes %}

                                        {% if loggedUserId == likes.clickedId %}

                                        {{ tag_html("a", ["href":"likes?usersId=" ~ user.id ~ "&unlike=unlike", "role":"tooltip", "data-toggle":"tooltip","data-placement":"top", "title":"Like me","class": "email-ic unlike"]) }}
                                        <i class="fa fa-heart-o" aria-hidden="true"></i>
                                        <span style="font-size: 10px">
                                                {% for sumLikes in totalLikes %}
                                                    {% if user.id ==  sumLikes.usersId %}
                                                        {{ sumLikes.totalLikesDB }}
                                                    {% endif %}
                                                {% endfor %}
                                            </span>
                                        {{ tag_html_close("a") }}

                                        {% else %}

                                        {{ tag_html("a", ["href":"likes?usersId=" ~ user.id ~ "&like=like", "role":"tooltip", "data-toggle":"tooltip","data-placement":"top", "title":"Like me","class": "email-ic unlike"]) }}
                                        <i class="fa fa-heart" aria-hidden="true"></i>
                                        <span style="font-size: 10px">
                                                {% for sumLikes in totalLikes %}
                                                    {% if user.id ==  sumLikes.usersId %}
                                                        {{ sumLikes.totalLikesDB }}
                                                    {% endif %}
                                                {% endfor %}
                                            </span>
                                        {{ tag_html_close("a") }}

                                        {% endif %}

                                    {% endfor %}

Best wishes and thank you in advance

edited Apr '18

What you mean it displays nothing? There is if/else statement - it always will display something :)

Perhaps there is an error somewhere. Have you dumped user.likes, loggedUserId, likes.clickedId do they have the correct information?



32.1k

Hello Nikolay,

thank you for your answer. Yes i dumped them and they are correct. The problem is, when the data is empty, so when there is no like from logged user, if i have start value it is working.

In my db i have the usersId of the user who get the like and the cickedId who clicked the like

So {% if loggedUserId == likes.clickedId %} -> it is correct, BUT if in db is no entry the loop is not working.

Rgds Stefan

you have a logic problem, I think you need refactorize that code

  1. itemsToLike all items like
  2. itemsLiked items where user like
  3. itemsCount counter by user

Then rewrite the view

edited Apr '18

As @Emilio said, its not a Volt, but a logic problem. user.likes is your relation depending on current user. So yes, if user did not like anyting = no results.

What I would do is select records and make a join to the table where you store the likes with current user id in the join clause. The sql should look something like:

SELECT
    main.field1,
    main.field2,
    main.field3,
    likes.id AS isLikedByCurrentUser
FROM main_table AS main
LEFT JOIN likes_table AS likes ON likes.main_table_id = main.id AND likes.user_id = CURRENT_LOGGED_USER_ID
WHERE ...
LIMIT 10
OFFSET 0


32.1k

Hello Emilio and Nikolay,

i will try that.

In my db i have id, usersId, clickedId, likes ... so my relation is the usersId.

Thx for your help, i will reply.



32.1k

Ok, i think the querybuilder is working, but i still have a problen in Volt.

    $isClicked = $this->modelsManager->createBuilder()
        ->from(["u" => "Vokuro\Models\Users", "l" => "Vokuro\Models\Likes"])
        ->where('l.usersId = u.id ',array('id' => $id))
        ->groupBy("u.id")
        ->getQuery()
        ->execute();

  $this->view->setVar("isClicked", $isClicked);

I get usersId 68 and 172, that is ok, they are stored in db. But what do i have to do in Volt now?

{% for liked in isClicked %}
  {% if user.id == liked.usersId %}
      clicked
  {% else %}
       not clicked
  {% endif %}
{% endfor %}

liked.usersId is empty, isClicked is an object.

Rgds Stefan



32.1k
Accepted
answer

Ok,i found a way, maybe not the best but it works.

I set a start value for example usersId = 68, clickedId = 0, likeClicks = 0, so now the "for loop" will always run.

In Volt i have an if-else statement which checks if the values are 0 or 1, if 0 the like heart is unfilled and if 1 the like heart is red :-)

hope it helps