chaseman Posted February 21, 2011 Share Posted February 21, 2011 I've built a database based rating system, and till now everything works as expected except of one minor thing that breaks the while rating system. When you press thumbs up or down it's only adding the vote to the LAST printed table in the while loop, it does NOT add it to the current table where the button was pressed. This is the while loop: <?php function knuffix_list ($query, $user_name, $avatar_path, $dbc) { $data = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); //Loop through the array of data while ($row = mysqli_fetch_array ($data)) { global $con_id; $con_id = $row['con_id']; echo "<table padding='0' margin='0' class='knuffixTable'>"; echo "<tr><td width='65px' height='64px' class='avatar_bg' rowspan='2' colpan='2'><img src='$avatar_path' alt='avatar' /></td>"; echo "<td class='knuffix_username'><strong>" . $user_name . " ___ "; echo "</strong><br />" . $row['category'] . " | " . date('M d, Y', strtotime($row['contributed_date'])) . "</td></tr><tr><td>"; echo "<form action='' method='post'> <button type='submit' name='plusVote' value='like'>Y</button> <button type='submit' name='minusVote' value='dislike'>N</button> <input type='hidden' name='hidden_con_id' value='<?= " . $con_id . " ?>' /> </form></td><td class='votes'>Y[ - ] | N[ - ]</td></tr>"; echo "<tr><td class='knuffix_name' colspan='3'><strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>"; echo "<tr><td colspan='2' class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>"; echo "</table>"; } mysqli_close($dbc); } ?> The con_id is the value how I wanted to determine which table it is, when I echo $con_id out (inside the while loop) it's showcasing the numbers correctly, but it doesn't work with the buttons, the buttons simply correspond to the last printed table in the while loop. I think it's because the while loop goes through each row until it's finished and it stops. This is the rating script: // RATING SYSTEM $plus_vote = $_POST['plusVote']; $minus_vote = $_POST['minusVote']; if ($plus_vote || $minus_vote) { $query = "SELECT * FROM con WHERE con_id = '$con_id'"; $query = mysqli_query ($dbc, $query) or die (mysqli_error($dbc)); $assoc_vote = mysqli_fetch_assoc ($query); if ($plus_vote) { $vote_up = $assoc_vote['likes'] + 1; $query = "UPDATE con SET likes = '$vote_up' WHERE con_id = '$con_id'"; $query_run = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); mysqli_close($dbc); echo "test " . $con_id; } elseif ($minus_vote) { $vote_down = $assoc_vote['dislikes'] - 1; $query = "UPDATE con SET dislikes = '$vote_down' WHERE con_id = '$con_id'"; $query_run = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); mysqli_close($dbc); echo "test " . $con_id; } } If I would be able to make the buttons in the while loop to RECOGNIZE the correct con_id of the table the button was being pressed then the script would work as intended and it would add the vote to the correct row in the table. How could I solve this problem, because I really have no idea? Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 21, 2011 Author Share Posted February 21, 2011 I figured it out now, this is a small test script that I wrote, you can try it out yourself, every time you click the vote button it will print out the correct ID of the corresponding table. function while_test (){ $a = 1; $b = 3; $t_id = 1; global $current_id; while ($a++ <= $b){ echo "<center>"; echo "<table><tr><td>Table ID: " . $t_id . "</td></tr>"; echo "<tr><td> This is a table test </td></tr>"; echo "<tr><td><form action='' method='post'>"; echo "<button type='submit' name='submit' value='" . $t_id . "'>Vote</button></form></td></tr></table>"; echo "</center><br /><br />"; $t_id++; $current_id = $_POST['submit']; } } while_test(); if ($current_id) { echo "test " . $current_id; } All I have to do is implement the same thing into the actual script. I had to take the submit buttons and put them into a variable INSIDE the while loop at the same time I had to put the con_id variable inside the value of the button. And that was the simple solution to the problem, but because of a bunch of code I simply wasn't seeing it. That's why I love doing experiments, it helps you see things clearer and concentrate on the necessary. Quote Link to comment 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.