Solved

Php mysql easy voting system.

Posted on 2006-06-25
4
426 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 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now