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

  • SEARCH
  • Home
  • 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 9199579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:33:54+00:00 2026-06-17T22:33:54+00:00

I’d like users to be able to distinguish if they’ve voted for something (by

  • 0

I’d like users to be able to distinguish if they’ve voted for something (by bolding it) or haven’t voted for it yet (not bolded).

For example:

       Voted For Post     Unvoted for
Votes:  77 ↑ ↓             12 ↑ ↓

Here is how my database is set up:

intro

Contains messages

    message_id    intro            user_id      up_vote
     10            Voted For Post   5            77
     11            Unvoted for      5            12

voting

Contains votes

    Voting_id     message_id_fk      user_id
     18             10               5   
     19             10               3     

users

Contains user names

    first_name    user_id
     BOB           5

I don’t know how to properly query the database then make the proper if statement that will distinguish between voted for and unvoted for posts.

Here’s what I have so far:

$sql = mysql_query("SELECT * FROM intro
         INNER JOIN users ON intro.user_id = users.user_id
         ORDER BY `up` DESC ");

echo $row['first_name'] . " " .  $row['intro'];

if( ??? ) {
  echo "<strong>" . $row['up_vote'] . "</strong>";
} else {
  echo $row['up_vote'];
}

Any ideas?

  • 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-06-17T22:33:56+00:00Added an answer on June 17, 2026 at 10:33 pm

    Left join the Voting table and check if any corresponding items were found:

    SELECT intro.message_id, intro.intro, intro.user_id, intro.up_vote,
        IF(Voting.user_id IS NULL, 0, 1) AS has_voted
    FROM intro
    INNER JOIN users ON intro.user_id = users.user_id
    LEFT JOIN Voting ON Voting.message_id_fk=intro.message_id
        AND Voting.user_id = 5
    ORDER BY `up` DESC 
    

    Then in PHP:

    if($row['has_voted'){
        echo "<strong>".$row['up_vote']."</strong>";
    }else {
        echo $row['up_vote'];
    }
    

    Some explanations:

    • Columns from a LEFT JOINed table are NULL if no matching rows were found
    • IF() is a function that returns the second parameter if the first parameter evaluates to true, otherwise returns the third parameter. As a function it can be easily used in the SELECT clause
    • I replaced SELECT * by explicit selection of the needed columns, which is considered a best practise and in this case necessary because of ambiguous column names
    • Of course you will have to replace the literal 5 with your current user id. Use prepared statements or concatenate the query like this: "...Voting.user_id = " . intval($current_user_id) . "..."
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT

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.