Reputation: 245
Ok I know this is something stupid I'm doing wrong. But i've been stuck on this for about an hour and cannot figure it out. I am really just starting to get into PHP so excuse the newbyness. Basically I am displaying a table of data from a MySQL database. But I want it so that users can essentially "Join" a "Campaign" and then only have the "Join" button display for campaigns that they have NOT joined.
<?php
//include db connect
include ("/db_con.php");
//campaign list function
function listCampaigns() {
//make connection global
global $con;
//set campaign variables
$campaignquery = mysqli_query($con, "SELECT * FROM campaigns");
while($row = mysqli_fetch_array($campaignquery )) {
$ccampaignid = $row['campaignid'];
$creator = $row['creator'];
$name= $row['name'];
$startdate = $row['startdate'];
//set campaign_bookmark variables
$userid = $_SESSION['userid'];
$bmquery = mysqli_query($con, "SELECT * FROM campaign_bookmarks WHERE userid = $userid");
while($bmrow = mysqli_fetch_array($bmquery)) {
$bmuserid = $bmrow['userid'];
$bmcampaignid = $bmrow['campaignid'];
}
//echo list in table
echo "<table border='1'>";
echo "<tr>";
//display join button if userid in campaign bookmarks does not match session userid
if($_SESSION['userid'] !== $bmuserid) {
echo "<form action='functions/campaign_bookmark.php' name='bookmarkCampaign' method='post'>
<input type='hidden' name='campaignid' value=" . $row['campaignid'] . ">
<input type='submit' value='Join' onclick='clicked();'>
</form>";
}
echo "</tr> <tr>";
echo "<td> Dungeon Master: </td>";
echo "<td>" . $creator . "</td>"; //campaign creator/DM name
echo "</tr> <tr>";
echo "<td> Campaign Name:</td>";
echo "<td>" . $name . "</td>"; //campaign name
echo "</tr> <tr>";
echo "<td> Start Date:</td>";
echo "<td>" . $startdate . "</td>"; //campaign start date
echo "</tr>";
echo "</table>";
} //end of outer while loop
} //end of listCampaigns
?>
The Join button does show up if a user has not joined any campaigns. When the Join button does show up, it does properly submit the data into the database via campaign_bookmark.php
But once you have joined one campaign, then the Join button is gone for all of them. If any other info is needed please let me know what. Any help will be VERY appreciated.
Upvotes: 0
Views: 84
Reputation: 42899
The reason the button always disappears is because the last test is ALWAYS false except one condition, when the previous query returns no records so the while loop isn't entered.
At first you set
$userid = $_SESSION['userid'];
Then the query
$bmquery = mysqli_query($con, "SELECT * FROM campaign_bookmarks WHERE userid = $userid");
Notice now the new userid
= $_SESSION['userid']
Then inside the loop
$bmuserid = $bmrow['userid'];
so now $bmuserid
== $bmrow['userid']
== $_SESSION['userid']
which would mean that this is false
if($_SESSION['userid'] !== $bmuserid)
So the button will not appear no matter what, unless this query returns nothing
$bmquery = mysqli_query($con, "SELECT * FROM campaign_bookmarks WHERE userid = $userid");
So the following loop is never entered ( the one that makes the 3 variables equal each other )
I don't know how you should fix it, you just need to rethink the logic behind this, I just pointed out where it goes wrong.
PS: your php code really needs some cleaning, when the html code is more than php then consider closing the php tag and embedding php inside normal html, your table for example could become something like this
<table border='1'>
<tr>
<?php if($_SESSION['userid'] !== $bmuserid): ?>
<?# notice you forgot to add a td here, this is invalid #?>
<form action='functions/campaign_bookmark.php' name='bookmarkCampaign' method='post'>
<input type='hidden' name='campaignid' value='<?= $row['campaignid']?>'>
<input type='submit' value='Join' onclick='clicked();'>
</form>
<? endif; ?>
</tr>
<tr>
<td> Dungeon Master: </td>
<td><?= $creator ?></td>
</tr>
<tr>
<td> Campaign Name:</td>
<td><?= $name ?></td>
</tr>
<tr>
<td> Start Date:</td>
<td><?= $startdate ?></td>
</tr>
</table>
In my opinion this is a lot more readable
Upvotes: 1
Reputation: 739
The only reason i can see why the buttons wont show up is that your userid is getting overwritten in the session once your submit the new campaign join.
Upvotes: 0
Reputation: 2473
Well I might be wrong, but you are performing a SQL request that selects ALL the campaigns based on the userId so :
$bmuserid = $bmrow['userid'];
Will give you the userId of the current user. so you will always have :
$bmuserid = $bmtow['userid'];
I would advise you to change your database structure and change the request to directly select all the campaign that are not joined.
Upvotes: 0
Reputation: 33945
Here's something to think about...
SELECT *
FROM campaigns c
LEFT
JOIN campaign_bookmarks b
ON b.campaignid = c.campaignid
AND b.userid = $userid;
Upvotes: 0