Solved

Php mysql easy voting system.

Posted on 2006-06-25
4
433 Views
Last Modified: 2013-12-12
Hi,

I've made website that stores all my movies, games, apps and so on.

For example here's a the code for my dvd movies:

$amount = mysql_query("SELECT * FROM data WHERE category='dvd'");
$num_rows = mysql_num_rows($amount);

$querylatest = "SELECT date_format(max(timestamp), '%d.%m.%Y  %H:%i:%s') as date FROM data GROUP BY TIMESTAMP desc limit 1";
$resultlatest = mysql_query($querylatest) or die ("Error in query: $query. " . mysql_error());

while ($get_info = mysql_fetch_row($resultlatest))
{
     foreach ($get_info as $field)
     {
             echo ("<p>Database last updated at:&nbsp;<br/><b>$field</b></p>\n");

     }

}

echo "Total amount of Dvd:s: <b>$num_rows</b> pcs\n";


echo ("<table width=\"480\" bgcolor=\"#715e5e\" cellpadding=\"3\" cellspacing=\"2\" border=\"3\"\n>");
echo  ("<tr bgcolor=\"#723939\">");


echo ("<th style=\"color: white;\">Name</th>\n");
echo ("<th style=\"color: white;\">Category</th>");
echo ("<th style=\"color: white;\">Level</th>\n");
echo ("<th style=\"color: white;\">Status</th>\n");
echo ("<th style=\"color: white;\">Rating</th>\n");
echo ("<th style=\"color: white;\">Votes</th>\n");
echo ("<th style=\"color: white;\">Rate</th>\n");

echo  "</tr>";

$result = mysql_query("SELECT name, category, l1_number, status, who FROM data d, level_1 l WHERE  d.shelf_id=l.level_1_id and category='dvd' order by name limit $offset, $limit ");

      while ($row = mysql_fetch_assoc($result))
      {
            echo ("<tr>");

            echo ("<td style=\"color: #ffffff;\"><a href=\"http://www.imdb.com/find?q=$row[name]\" target=\"_blank\"><font face=\"sans-serif, verdana, helvetica, arial;\" align=\"center\"></font>$row[name]</a></td>");
            echo ("<td style=\"color: #ffffff;\">$row[category]</td>");
            echo ("<td style=\"color: #ffffff;\">$row[l1_number]</td>");
            echo ("<td style=\"color: #ffffff;\">$row[status]</td>");
            echo ("<td style=\"color: #ffffff;\">$row[rating]</td>");
            echo ("<td style=\"color: #ffffff;\">$row[num_votes]</td>");
            echo ("<td style=\"color: #ffffff;\">
            <a href=\"main.php?id=content&page=rateit\">Rate</a></td>");

          echo ("</tr>");
      }

      echo ("</table>");
      echo ("<br/>");


$endtime = microtime();

$diff = number_format(((substr($endtime,0,9)) + (substr($endtime,-10)) - (substr($starttime,0,9)) - (substr($starttime,-10))),4);

//Display links for pages
if ($page == 1)
      echo "<img style=\"border: none;\" src=\"pics/arrowp.gif\" alt=\"Previous page\"/>";

      else
      echo "<a href=\"main.php?id=content&page=dvd&dvd_page=" . ($page - 1) . "\"><img style=\"border: none;\" src=\"pics/arrowp.gif\" alt=\"Previous page\"/></a>";

      for ($i = 1; $i <= $pager->numPages; $i++)
      {
            echo " | ";
          if ($i == $pager->page)
                echo " $i";
          else
                echo "<a href=\"main.php?id=content&page=dvd&dvd_page=$i\"> $i</a>";
    }
      echo " | ";

      if ($page == $pager->numPages)
      echo "<img style=\"border: none;\" src=\"pics/arrow.gif\" alt=\"Next page\" />";

      else
      echo "<a href=\"main.php?id=content&page=dvd&dvd_page=" . ($page + 1) . "\"><img style=\"border: none;\" src=\"pics/arrow.gif\" alt=\"Next page\" /></a>";

echo"<br><small><small>Search took $diff s </small></small>";

mysql_close($link);
?>

To have a look at it in realtime, here's the address:

http://www.toubes-page.net

user: testabc
pass: test123

What I would like to create is a voting system so that users can give their votes on various titles and then show the current voting points and the number of votes
in the same table where the movietitle is being shown.

It's been a while sins I played around with php.. and I never been good with it :(

Thanks for your help.

-Toby
0
Comment
Question by:Toube
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16979029
You will have to create another table : vote
table will be like this :
id | productid | vote

when user vote a product, it will insert into table by query
mysql_query("INSERT INTO vote (productid, vote) VALUES ('$productid', '$vote');");

where $productid is id of your product, you can get this value by using $_GET or $_POST

To caculate the average vote :

$query = mysql_query("SELECT * FROM vote WHERE productid='$productid'");
$nums = mysql_num_rows($query);

while ($vote = mysql_fetch_array($query))
{
     $total = $total + $vote['vote'];
}

$average_vote = $total / $num;
// To show number of vote, you only need echo $nums

Bonmat86.
0
 

Author Comment

by:Toube
ID: 16979064
Thanks for your answer,

Hmm...don't know if I'm following you here.. so if a user votes for a product, do I have to create a form or how will the voting work?

Thanks.
0
 
LVL 10

Accepted Solution

by:
Khanh Doan earned 400 total points
ID: 16979111
Yes, you will have to create a form.

<?php
$vote = $_POST['vote'];
$productid = $_POST['pid'];
mysql_query("INSERT INTO vote (productid, vote) VALUES ('$productid', '$vote');");
?>

<form name="vote" action="vote.php", method="post">
<select name="vote">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="hidden" name="pid" value="1">
</form>

Bonmat86.
0
 

Author Comment

by:Toube
ID: 16979126
Thanks will try it later:)

-Toby
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question