Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Php mysql easy voting system.

Posted on 2006-06-25
4
Medium Priority
?
439 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
  • 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 1600 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

963 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