Jump to content

Showing vote results on refreshed page


Mike Smith

Recommended Posts

Hi everyone,

 

Below is the index.php and voted.php file I am using. The user votes on the images, it passes the votes to the voted.php file and adds the tally to the database columns, and sends the user back to the index.php page. My question is this:

 

Once the person votes and is taken back to the index.php file, how can I have it display the results from the previous three images?

 

 

Here's my index.php code

 

<?php 
// Connects to your Database 
$link = mysql_connect('localhost', 'username', 'password'); 
mysql_select_db("database") or die(mysql_error()); 

?>

<?php include('header.php'); ?>

<div class="contentwide">
<div class="contentwrap">
<form method="post" action="voted.php"> 
<?php 
$query = "SELECT id, name, url FROM photos WHERE category!= 'Xrated' ORDER BY RAND() LIMIT 3";
$result = mysql_query($query); 
while($row = mysql_fetch_assoc($result)) 
{ 
?> 
    <input type="hidden" name="id[]" value="<?php echo $row['id']; ?>">
    <input type="hidden" name="page" value="index.php"> 
    <div class="picturewrap"> 
        <img src="<?php echo $row['url']; ?>" alt="<?php echo $row['name']; ?>" /> 
        <div class="formwrapper"> 
	     <div class="formspoon">
	     <img src="images/form_spoon.jpg" alt="" /><br />
	     <input type="radio" name="images[<?php echo $row['id']; ?>]" value="1"> 
	     </div>
        </div> 
        <div class="formwrapper"> 
	     <div class="formspoon">
	     <img src="images/form_knife.jpg" alt="" /><br />
	     <input type="radio" name="images[<?php echo $row['id']; ?>]" value="3"> 
	     </div>
        </div> 
        <div class="formwrapper"> 
                 <div class="formspoon">
	     <img src="images/form_fork.jpg" alt="" /><br />
	     <input type="radio" name="images[<?php echo $row['id']; ?>]" value="2"> 
	     </div>
        </div> 
    </div> 
<?php 
} 
?> 
    <div class="submitform"> 
        <input type="image" src="images/button_submit.jpg" class="button_submit" value="Submit"><br/ > 
    </div> 
</form> 
</div>
</div>
<?php mysql_close($link); ?>
<?php include('footer.php'); ?>

 

Here is my voted.php file

 

<?php 

// Connects to your Database 
$link = mysql_connect('localhost', 'user', 'password'); 
mysql_select_db("database") or die(mysql_error()); 

    $page = $_POST['page']; 

if ( !empty( $_POST['images'] ) ) { 
    $images = $_POST['images']; 
    // Loop over each item in the $images array. 
    foreach ( $images as $id => $vote ) { 
        if ( $vote == 1 ) { 
            $update = sprintf( "UPDATE photos SET spoon=spoon+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" );
	header("Location: $page");  
        } elseif ( $vote == 2 ) { 
            $update = sprintf( "UPDATE photos SET fork=fork+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" ); 
	header("Location: $page"); 
        } elseif ( $vote == 3 ) { 
            $update = sprintf( "UPDATE photos SET knife=knife+1 WHERE id=$id", $vote , $id ); 
            mysql_query( $update ) OR DIE( "error in query" ); 
	header("Location: $page"); 
        } else 
            die( "Invalid value passed as a vote." ); 
    } 
} 

?>

 

Thanks for any help I can receive

Link to comment
Share on other sites

do you limit how many times users can vote ?  Is this limited to only registered users ?

 

what you can do is use a cookie or a session variable to say "userA has entered a vote".  after the redirect, on the index.php page you check if this value is true. If it is, you show the results, otherwise you display the voting form

Link to comment
Share on other sites

I do not think I mentioned this before, but the voting is done over and over again. the index.php pulls three random db results (out of hundreds) and displays three random each time. Not sure if that makes a difference in how this would be done or not.

Link to comment
Share on other sites

Do you only want to show the results directly after someone has voted, and any other time they access index.php they can vote again?

 

Well if so, there are multiple ways you can do that. Off the top of my head, an easy way would be to have your voted page pass a get variable to the index page (IE instead of going to index.php go to index.php?voted=yes or something) and on the index page check if the get variable has the correct value and then show the results. if not show the form

Link to comment
Share on other sites

Hi Mikesta707, yeah, I only want the results to show AFTER they vote - this way, they can vote and see what everyone else thinks.

 

So the index.php?voted=yes would be done by adding a variable to the index page, like this?

 

<?php

$voted = yes;

if ($voted == yes)

echo "RESULTS HERE";

?>

 

right?

Link to comment
Share on other sites

just send it as another part of the URL.

 

if they can only vote on one at a time this is much easier

 

vote.php

<?php
header("Location: index.php?voted=yes&image=5");

 

index.php

<?php
if(isset($_GET['voted']) && $_GET['voted']=='yes') {
   echo "You have voted on image #".$_GET['image']." Congratulations!";
}

Link to comment
Share on other sites

I just added in the code, and it does display things when I pull it back in, but I need to pass all three image id's back. Is there an easy solution for this?

 

I need to pass all three image id's and also pull the database information for all three image id's so I can show people the overall results for what they just voted for.

Link to comment
Share on other sites

Use hidden fields with the ids.

<input type="hidden" name="pic[0]" value="xxx">

or you can set the key element of the array

<input type="radio" name="vote[xxx]" value="0">Option 1<br />
<input type="radio" name="vote[xxx]" value="1">Option 2<br />
<input type="radio" name="vote[xxx]" value="2">Option 2<br />

replace xxx with the pics id

I think I wud use 2nd method

as than ya can pull the information with foreach with the key element :)

 

Link to comment
Share on other sites

I have the three random images in an "images" array.

 

images[<?php echo $row['id']; ?>]

 

that is the code I currently use in the index.php file. My problem is passing the id's back from the voted.php file. Not sure where to put them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.