I am using a PHP array to gather user input that has been added to my MySQL database. As this is information from user input I am using a variable created by the num_rows in my database to determine the number of times a for loop runs through displaying my array values. The for loop contains a form that displays one value of the array, and a “like” button (type=submit), and repeats this form until all values have been displayed (from newest to oldest), with a “like” button following each.
I want users to be able to click the “like” button, to add a “like” to that post. The problem I’ve run into is that the code I have is adding a “like” to every post (because the code checks to see if the “like” button has been pressed, and since the “like” button was created by the for loop, every “like” button has the same name). I’ve tried to rectify this by giving the “like” button a name based on an incremented variable, but the button does not seem to work if the name is a variable or an array.
Here is my code:
<?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];
$userside = $_SESSION['side'];
echo "<b>Organized posts:</br><hr /></b>";
require("./postconnect.php");
$query = mysql_query("SELECT * FROM original ORDER BY postid ASC");
$numrows = mysql_num_rows($query);
$numrows = $numrows-1;
$sql = "SELECT postername FROM original ORDER BY postid ASC"; // select only the postername field from the table "original"
$result = mysql_query($sql); // process the query
$name_array = array(); // start an array
while($row = mysql_fetch_array($result)){ // cycle through each record returned
$name_array[] = "".$row['postername'].""; // get the postername field and add to the array above
}
$sql = "SELECT post FROM original ORDER BY postid ASC"; // select only the post field from the table "original"
$result = mysql_query($sql); // process the query
$post_array = array(); // start an array
while($row = mysql_fetch_array($result)){ // cycle through each record returned
$post_array[] = "".$row['post'].""; // get the post field and add to the array above
}
$sql = "SELECT posterside FROM original ORDER BY postid ASC"; // select only the posterside field from the table "original"
$result = mysql_query($sql); // process the query
$side_array = array(); // start an array
while($row = mysql_fetch_array($result)){ // cycle through each record returned
$side_array[] = "".$row['posterside'].""; // get the posterside field and add to the array above
}
$sql = "SELECT likes FROM original ORDER BY postid ASC"; // select only the likes field from the table "original"
$result = mysql_query($sql); // process the query
$likes_array = array(); // start an array
while($row = mysql_fetch_array($result)){ // cycle through each record returned
$likes_array[] = "".$row['likes'].""; // get the likes field and add to the array above
}
$i=$numrows;
for($i;$i>=0;$i--) {
if ($side_array[$i]==1) {
$color="red";
}
elseif ($side_array[$i]==2) {
$color="blue";
}
elseif ($side_array[$i]==3) {
$color="green";
}
echo "<form action='./memberhag.php' method='post'>
<table>
<tr>
<td><font color='$color'>$name_array[$i]</font> - $post_array[$i]</td>
</tr>
<tr>
<td><input type='submit' name='likebtn' value='Like' /> <font color=$color>$name_array[$i]</font> has $likes_array[$i] likes!</td>
</tr>
</table>
</form>";
if ($_POST['likebtn']) {
$numlikes = $likes_array[$i];
$numlikes = $numlikes + 1;
mysql_query("UPDATE original SET likes = '$numlikes' WHERE postername = '$name_array[$i]'");
}
}
?>
This has had me stumped for quite a while… I’ve even tried using a while loop instead of a for loop.
Having lots of forms to do this is one possible solution, but I’d be inclined to have just one form, and several buttons. The primary problem seems to be that, as you say, you’ve called the buttons the same thing, which means you have no way to determine which one has been pressed.
A very basic fix is to do this:
That will at least give you an ordinal number you can use to differentiate between buttons. However it is probably better for you to loop through your
postsin this loop, so you can do this (assuming you have a primary key calledidin this table:Ah yes, and when you fix your
POSTsection (i.e. the bit when someone pushes a ‘like’ button) you’ll need to read what’s in $_POST and parse out what’s been pushed. To help debug this, temporarily add this into your post handler:That will give you the output you need to decode (check that it is different for each post like). You’ll also need to change your
ifstatement to detect posts; change this:to:
This is necessary as you now don’t have a single name to detect, so we just now detect whether the array
$_POSTcontains anything (if it does, we know this is a post operation). Inside this, put theprint_r()above, to see what’s inside it.Now, here are some things you can do to improve your code:
originaldoes, so I’d probably rename that.SELECT * FROM original– so there are too many queries hereuser)forloop, switch that to a while loop over your posts table.echo "x"statements, as it gets difficult to debug after a while. It is probably better to break out of PHP mode, and use simple loops and variable outputs in HTML mode.Edit, in response to your comment:
Great! OK, try something like this:
Above all, try to understand why this works. Do some reading around it if necessary. I’ve essentially looped through the associative array of the post operation, and tested each key (i.e. element name) to see if it contains the string “likebtn_”. If it does, I convert the remainder to an integer, which you can then use in an
UPDATEstatement.