Page 1 of 1

Display certain picture according to database

Posted: Mon Jan 23, 2006 1:41 am
by nickman013
Hello,

I have a working database that is used for a voting system. There is one table with two fields, one field is the answer field and the other field is number_of_votes. I want to make a script that displays a picture according to the answer with the most votes.

So for example

if 2 has the most votes = displays 2.jpg
if 3 has the most votes = displays 3.jpg
if 4 has the most votes = displays 4.jpg
...

The form is pretty simple:

Code: Select all

<form action="/pages/vote.php" method=post>
  <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=button value="Vote!">
That form and the database work fine.

The only thing I need help with doing is displaying the picture according to the answer with most votes.

Get what I mean?

Thank You!

I made the database with the help of jshpro2

Posted: Mon Jan 23, 2006 3:40 am
by php3ch0
you could use switch()

I am going to assume that you have got the number of votes as variable $votes

Code: Select all

<?php
switch ($votes) {
case "1":
   $image = "image1.jpg";
   break;
case "2":
   $image = "image2.jpg";
   break;
case "3":
   $image = "image3.jpg";
   break;
default:
   echo "Error";
}
?> 

<img src ="<?php echo $image; ?>">
or you could use if statements

Code: Select all

<?php 

if($votes == '0') { $image = "image1.jpg"; }
if($votes > '10') { $image = "image2.jpg"; }
if($votes > '20') { $image = "image1.jpg"; }

?> 
<img src ="<?php echo $image; ?>">

Posted: Mon Jan 23, 2006 11:37 am
by josh

Code: Select all

$result = mysql_query(
"SELECT `answer` FROM `votes` ORDER BY `number_of_votes` ASC limit 1"
);
echo '<img src="'.(int)mysql_result($result,0,0).'" />';
mysql_free_result($result);

Posted: Mon Jan 23, 2006 10:43 pm
by nickman013
thank you both for responding, it works perfect, thanks jshpro2