Mike Smith Posted May 29, 2009 Share Posted May 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/ Share on other sites More sharing options...
lonewolf217 Posted May 29, 2009 Share Posted May 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845033 Share on other sites More sharing options...
Mike Smith Posted May 29, 2009 Author Share Posted May 29, 2009 The voting isn't limited and it doesn't require users to be registered. Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845036 Share on other sites More sharing options...
Mike Smith Posted May 29, 2009 Author Share Posted May 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845074 Share on other sites More sharing options...
mikesta707 Posted May 29, 2009 Share Posted May 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845076 Share on other sites More sharing options...
Mike Smith Posted May 29, 2009 Author Share Posted May 29, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845243 Share on other sites More sharing options...
lonewolf217 Posted May 29, 2009 Share Posted May 29, 2009 index.php?voted=yes <?php if(isset($_GET['voted']) && $_GET['voted']=='yes') { echo "You have voted. Congratulations!"; } Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845244 Share on other sites More sharing options...
Mike Smith Posted May 29, 2009 Author Share Posted May 29, 2009 thanks lonewolf217 Is there a specific way that I can pull the image id's from the voted.php file as well? I need to know which image id's were voted for so I can add the code in to display their db contents. Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845249 Share on other sites More sharing options...
lonewolf217 Posted May 29, 2009 Share Posted May 29, 2009 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!"; } Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845252 Share on other sites More sharing options...
Mike Smith Posted May 29, 2009 Author Share Posted May 29, 2009 thanks lonewolf217 I think I can check this one as resolved. I'm still having trouble with my other post, if you've got a second I love this place and appreciate all of the help and input. Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845261 Share on other sites More sharing options...
Mike Smith Posted May 29, 2009 Author Share Posted May 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845285 Share on other sites More sharing options...
laffin Posted May 29, 2009 Share Posted May 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845315 Share on other sites More sharing options...
Mike Smith Posted May 29, 2009 Author Share Posted May 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160163-showing-vote-results-on-refreshed-page/#findComment-845324 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.