Display certain picture according to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Display certain picture according to database

Post 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
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post 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; ?>">
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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);
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

thank you both for responding, it works perfect, thanks jshpro2
Post Reply