mysql - PHP if statement only half works -
ok know stupid i'm doing wrong. i've been stuck on hour , cannot figure out. starting php excuse newbyness. displaying table of data mysql database. want users can "join" "campaign" , have "join" button display campaigns have not joined.
here code:
<?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 * 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 * campaign_bookmarks 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 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 show if user has not joined campaigns. when join button show up, submit data database via campaign_bookmark.php once have joined 1 campaign, join button gone of them. if other info needed please let me know what. appreciated.
the reason button disappears because last test false except 1 condition, when previous query returns no records while loop isn't entered.
at first set
$userid = $_session['userid']; then query
$bmquery = mysqli_query($con, "select * campaign_bookmarks userid = $userid"); notice new userid = $_session['userid'] inside loop
$bmuserid = $bmrow['userid']; so $bmuserid == $bmrow['userid'] == $_session['userid']
which mean false
if($_session['userid'] !== $bmuserid) so button not appear no matter what, unless query returns nothing
$bmquery = mysqli_query($con, "select * campaign_bookmarks userid = $userid"); so following loop never entered ( 1 makes 3 variables equal each other )
i don't know how should fix it, need rethink logic behind this, pointed out goes wrong.
ps: php code needs cleaning, when html code more php consider closing php tag , embedding php inside normal html, table example become this
<table border='1'> <tr> <?php if($_session['userid'] !== $bmuserid): ?> <?# notice forgot add td here, 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 opinion lot more readable
Comments
Post a Comment