Jakebert Posted June 17, 2012 Share Posted June 17, 2012 Hi everyone, I'm having a lot of trouble writing this particular page, which is the voting page. Essentially, I want everyone to vote once, but they can change their votes. The page is set up like this currently. I've tried to include comments so that it makes sense. I'm having particular trouble with the javascript controlling the changing of votes. I'd also appreciate any general coding suggestions so that this code isn't as much of a monstrosity. I know this is a killer - I hope I'm not wasting anyone's time! <?php //when they vote using the form further down the page, this triggers // If they voted yes if (isset($_POST['aye'])){ //check that they haven't voted yes before $numrows = get_rows("votes WHERE `bid`=".$bid . " AND `uid`=" . $user . " AND `vote`=1"); //if they've voted yes before if($numrows > 0) { echo "<script type='text/javascript'> alert('You can only vote for bill once'); window.location='home.php'</script>"; } else { //check to see if they've voted at all $votedbefore = get_rows("votes WHERE `bid` = '$bid' AND `uid` = '$user'"); //if they've voted the other way before if ($votedbefore > 0) { echo "<script language=\"Javascript\" type=\"text/javascript\"> var r=confirm('Change your vote?'); if (r==false){ document.location.href='vote.php';}"; echo "else {"; } //now, change the DB information to match their vote $votes = $oldayes + 1; $edit = mysql_query("UPDATE bills SET ayes = '$votes' WHERE status = 'at vote'"); $record = mysql_query("INSERT INTO votes (bid, uid, vote) VALUES ('$bid', '$user', 1);"); echo "</script><script language=\"Javascript\" type=\"text/javascript\"> alert(\"Your vote has been recorded\");document.location.href='home.php'; </script>}"; } } // do the whole damn thing over again if they clicked the no button if (isset($_POST['nay'])){ $numrows = get_rows("votes WHERE `bid`=".$bid . " AND `uid`=" . $user . " AND `vote`=0"); if($numrows > 0) { echo "<script type='text/javascript'> alert('You can only vote for bill once'); window.location='home.php'</script>"; } else { $votes = $oldnays + 1; $edit = mysql_query("UPDATE bills SET nays = '$votes' WHERE status = 'at vote'"); $record = mysql_query("INSERT INTO votes (bid, uid, vote) VALUES ('$bid', '$user', 0);"); echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"Your vote has been recorded\");document.location.href='home.php'; </script>"; } } // first we need to make sure that the user is logged in if (isset($_SESSION['uid'])) { //display that info echo "<strong>" . $row['title'] . "</strong><br/>"; echo $row['subtitle']; echo "<br/>Tabled by: " . $row2['first'] . " " . $row2['last']; echo "<br/><br/>"; echo $row['body']; echo "<table><tr><td style= 'color:green'>AYES: <b>" . $oldayes . "</b></style></td>"; echo "<td style= 'color:red'>NAYS: <b>" . $oldnays . "</b></style></td></tr></table>"; ?> <form action="vote.php" method="post"> <input type="submit" name="aye" value="AYE" /> <input type="submit" name="nay" value="NAY" /> </form> <?php } else // they aren't logged in { echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"You are not logged in!\");document.location.href='index.php'; </script>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264354-spaghetti/ Share on other sites More sharing options...
btherl Posted June 18, 2012 Share Posted June 18, 2012 The first de-monstrifying step is to make your indentation consistent. Choose a rule and stick to it. The second is to seperate logic (php code) from display (html/javascript). Instead of echoing javascript directly from your code, you can set a variable like "$vote_recorded = true". Then your code becomes neater, and you can display the actual HTML / Javascript later by checking if $vote_recorded is true. Those two steps will give you much neater code. Neater code is easier to understand and easier to get to do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/264354-spaghetti/#findComment-1354758 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.