Howdy dwds, itΒ΄s like this im coding a voting poll and need to do something to control the votes, so one of my ideias was to create another table and save their the poll_id , author , option so that i could know wich user voted.

But i think this is not the best way since, if I want to know how many votes there is in one option I must do a query if I have 20 options .. 20 querys (see what I mean?)

PS: Didn't use cookies 'cause they dont control nothing if your browser doesnt support cookies or have them disabled.

Tks in advance, tell me some ideias please πŸ™‚

    Not necessarily ... you can always use GROUP BY. This example would be based on a system that has multiple polls. So you will have these tables:

    poll: poll_id, title, author, expiry_date
    poll_options: poll_id, option_id, option_text
    poll_votes: poll_id, option_id, user_id

    To count the results use this:

    SELECT option_id,COUNT(*) FROM poll_votes WHERE poll_id = "{the poll ID} GROUP BY option_id

    That should give you a row for each option along with the count for that option.

      Well, you were right I solved that problem but got into another πŸ˜›

      Have this tables:

      Poll: Id, Author, Topic
      Poll_options: Id, Pollid, Optionx
      Poll_votes: Id, Pollid, Optionid, Author

      The tables are related by the id's, my problem is:

      If I want to list Poll 2 how can I list the options with no votes, I made this query:

      SELECT poll_options.pollid,optionx,count(*) as votes
      FROM poll_votes,poll_options
      WHERE poll_votes.optionid = poll_options.id and poll_options.pollid='2'
      GROUP BY poll_votes.optionid

      This will only list the options with votes (regists on poll_votes)

      Need some help, tks in advance πŸ™‚

        Well, if the option is not present in the result, then there is no votes for it. This logic will require two queries: one to get the list of available options, and the other to get the votes for those options. Any option that's not present in the second query has no votes.

          Howdy, and thanks for your help dwd already solved that issue but here is another πŸ˜›

          I needed to list all the polls on data base, so I did it like this: since the mysql_query only returned the ones that had votes with another query I got the ones with no votes and mixed them in an array and it worked my problem now it's aplying a pagination script on it since the data comes from an array instead of a mysql query, can someone give me some tips on that?

          Tks in advance πŸ˜›

          PS: I know my english is not too good πŸ˜›

            Well the issue is already solved, I updated mysql and made use of not it and union command and solved it all with a couple of querys, anyway tks 4 those who have helped πŸ™‚

              Write a Reply...