Welcome Guest, Not a member yet? Register   Sign In
Building a "favorite this" system and totally stuck
#1

[eluser]Kurai[/eluser]
Hi everyone!
I'm trying to build a small message app, in which you can vote for a content.

What I need is a system that enables you to vote each message one time. No star ratings, no other stuff: it's just a "i like this!" feature.

So, I built a table in my db which associates the user_id to the message_id. This way, I can control that the user votes only one time a certain content.

But now I'm stuck!

I would like to show a link if you can vote the content, and don't show anything if you cannot.

I really don't know how to do now. I tried to join the kudos table with the message table, to show which messages were voted (using an outer join), but I obviously end up with a lot of dupes.

Have you got any advice for me?
Thanks!
#2

[eluser]gullah[/eluser]
I have a similar system going and I did something like this.

User Table
id

votes table
id
user_id
item_id

Item table
id
vote_up
vote_down

when someone goes to the page/or submits their vote you can check the votes table to see if they have already voted pretty easily by using the session information you have setup, and the item id from the url or wherever. Hope this helps.
#3

[eluser]Kurai[/eluser]
It's clear for me how to control the items when i click on the vote link.
What I'm trying to do is a bit more omplicated. The structure I have is pretty much the same (I have only up votes). But, say that I want to show a list of items (like blog posts or whatever) and show an active link only for the ones I didn't vote. How can I do it?

In the loop that shows the articles I would like to have something like:
Code:
<?php foreach ($query as $item) :

            if (something that tells that I haven't voted for the content)
                        {
                             <a href="vote">vote this content</a>
                        }
                 ?&gt;
            
            <div class="post">
                <h5>&lt;?=$item->title?&gt;</h5>
                <p>&lt;?=$item->body?&gt;</p>
                <p class="postfoot">&lt;?=$item->timestamp?&gt;</p>
            </div>

        &lt;?php endforeach; ?&gt;
#4

[eluser]Kurai[/eluser]
A possible solution:
I make in the model a function getting all voted posts for a given user ID.
Then, in the loop, for each post I browse the resulting array to see if the current message ID matches with any of the voted posts in the array.

But, I'm a bit of a newbie here.
Can you tell if this solution is efficient?
#5

[eluser]gullah[/eluser]
ok so you are going to be doing a foreach loop to print out the blogs more than likely

This is how I would do it and I don't think it would be a problem with resources unless the users are voting on many many blogs.

or you could work on your query to see if you can single it down more.

so...
foreach(blogs as blog)
{
foreach(votes as vote)
{
$voted = 0;
if(vote->blog_id == blog->id)
{
$voted = 1;
}
}
$if($voted == 0)
{
display vote link
}
display blog
}




Theme © iAndrew 2016 - Forum software by © MyBB