Why is the condition true for this and not that? |
Why is the condition true for this and not that? |
masonh928 |
Nov 18 2015, 09:30 PM
Post
#1
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Ok so I have a friending system, like Facebook's or other social networking functions. My table is set up with the values.
Friend1, Friend2, Accepted, and Date… Friend1 is the user who sent the friend request. Friend2 is the user receiving the request of friendship. Accepted: 0 = Not Accepted Yet, 1 = Accepted; You're friends now Date (pretty-selfexplainatory) the date the request was sent. Now the problem I'm having is I have a function which has 3 values. 0 = Not Friends 1 = Pending 2 = Friends 3 = Confirm Request Here's the code: CODE <?php error_reporting(E_ALL | E_STRICT); require_once($_SERVER['DOCUMENT_ROOT'] . "/Scripts/PHP/DB.inc.php"); $Connect = ConnectDB(); function isFriend($User1, $User2){ //0 = Not Friends //1 = Pending //2 = Friends //3 = Accept From User $ReturnVal = 0; // Default is Not Friends $Query = $GLOBALS['Connect']->prepare("SELECT * FROM Friends WHERE Friend1 = :F1 AND Friend2 = :F2 OR Friend1 = :F1 AND Friend2 = :F2"); $Query->bindValue(":F1", $User1); $Query->bindValue(":F2", $User2); if(!$Query->execute()){ $ReturnVal = 0; } $Rows = $Query->fetchAll(); foreach($Rows as $Results){ if($User1 == $Results['Friend2'] && $Results['Accepted'] == 0){ $ReturnVal = 3; } if($User1 == $Results['Friend1'] && $Results['Accepted'] == 0){ $ReturnVal = 1; }elseif($Results['Accepted'] == 1){ $ReturnVal = 2; }else{ throw new exception("Err: Error In Script, Contact Admin."); exit(); } } return $ReturnVal; } $User1 = the user who is viewing the profile, and $User2 = the user who's profile it belongs too. Profile Code: Where function is implemented… CODE <?php session_start(); if(!$_SESSION['Logged_in']){ die("<b><u>You must be logged in to view this profile. Click <a href='/Login'>here</a> to Login, or <a href='/Register'>here </a>to be registered for free!</u></b>"); }; require_once("./Scripts/PHP/Class/Profile.class.php"); require_once("./Scripts/PHP/Class/Board.class.php"); require_once("./Scripts/PHP/Library.php"); require_once("./Scripts/PHP/UserFunctions.php"); $ProfileID = $_GET['ID']; $Profile = new Profile($ProfileID); try{ $FullName = $Profile->getUserData("FullName"); $About = $Profile->getUserData("About"); $Age = $Profile->getUserData("Age"); $Religion = $Profile->getUserData("Religion"); $About = $Profile->getUserData("About"); $SexualOrientation = $FullName = $Profile->getUserData("SexualOrientation"); $PoliticalParty = $Profile->getUserData("PoliticalParty"); $PoliticalIdeology = $Profile->getUserData("PoliticalIdeology"); $Phone = $Profile->getUserData("Phone"); $Email = $Profile->getUserData("Email"); $Fax = $Profile->getUserData("Fax"); $Country = $Profile->getUserData("Country"); $Education = $Profile->getUserData("Education"); $Career = $Profile->getUserData("Career"); $Ethnicity = $Profile->getUserData("Ethnicity"); $Website = $Profile->getUserData("Website"); $Personality = $Profile->getUserData("Personality"); $Weight = $Profile->getUserData("Weight"); $Height = $Profile->getUserData("Height"); $Languages = $Profile->getUserData("Languages"); $DateJoined = AgeDate($Profile->getUserData("DateJoined")); $ProfilePicture = $Profile->getUserData("ProfilePicture"); $Status = $Profile->getUserData("Status"); $Username = $Profile->getUserData("Username"); $PID = $Profile->getUserData("PID"); $Filter = ($Profile->getUserData("CurseFilter") == 0) ? false : true; }catch(exception $Error){ echo("<h1>" . $Error->getMessage() . "</h1>"); } $isOwner = (strcasecmp($_SESSION['Logged_in'], $Username) == 0) ? true : false; try{ $IsFriend = isFriend($_SESSION['ID'], $ProfileID); }catch(exception $E){ echo("Error: " . $E->getMessage()); $isFriend = 100; } global $isFriend; $FriendAction = [ 0 => "Add Friend", 1 => "Request Pending", 2 => "Friends <b>✔</b>", 3 => "Confirm Friend Request", 100 => "Error: Contact Support" ]; ?> <div class="container"> <h1 class="Title"><?php echo($Profile->getUserData("FullName")); ?></h1> <br> <form enctype="multipart/form-data" method="post" action="/Upload"> <div class="img-upload"> <label for="file"> <img src="http://www.social-board.xyz/Community/Avatars/<?php echo($ProfilePicture) ?>" width="450px" height="500px" title="Update Profile Photo" style="margin-bottom: 20px;float:left;"> </label> <input type="hidden" name="ID" value="<?php echo($_GET['ID']); ?>"> <input type="file" name="Avatar" id="file" onchange="this.form.submit()"> </div> </form> <p><u><b>Status:</b></u><?php echo(convertHashtags($Status)); ?> <br> <?php if(!$isOwner){ if($IsFriend == 0){ echo("<button class='btn btn-success'><a href='Action?do=Friend&ID=" . $_GET['ID'] ."'><u>{$FriendAction[$IsFriend]}</u></a></button>"); }else { echo("<button class='btn btn-success'><a><u>{$FriendAction[$IsFriend]}</u></a></button>"); } $Update = ""; }else{ $Update = <<<Btn <button id="Update" class="btn btn-success">Update Profile</button> Btn; //die(var_dump($isOwner)); } ?> </p> <section id="Information"> <h2 class="Title2">Basic Info</h2> <p><u><b>Age: </b></u><?php echo($Age); ?></p> <p><u><b>Country: </b></u><?php echo($Country); ?></p> <p><u><b>Language(s): </b></u><?php echo($Languages); ?></p> <p><u><b>Education: </b></u><?php echo($Education); ?></p> <p><u><b>Date Joined: </b></u><?php echo($DateJoined); ?></p> <p><u><b>About: </b></u><br><?php echo(convertHashtags($About)); ?></p> <?php if(empty($_GET['Page'])){ echo <<<this <div id="slideshow"> <div class="slides"> <ul> <li id="Personal"> <h2 class="Title2">Personal Information</h2> <ol id="Number"> <li><u><b>Personality Type:</b></u> {$Personality} </li> <li><u><b>Religion: </b></u>{$Religion}</li> <li><u><b>Politcal Ideology: </b></u>{$PoliticalIdeology}</li> <li><u><b>Political Party: </b></u>{$PoliticalParty}</li> <li><u><b>Ethnicity: </b></u>{$Ethnicity}</li> </ol> </li> <li id="Contact"> <h2 class="Title2">Contact Information</h2> <ol id="Number"> <li><u><b>Phone: </b></u>{$Phone}</li> <li><u><b>Email: </b></u>{$Email}</li> <li><u><b>Fax: </b></u>{$Fax}</li> <li><u><b>Address: </b></u></li> <li><u><b>ZIP: </b></u></li> </ol> </li> <li id="Physical"> <h2 class="Title2">Physical Description</h2> <ol id="Number"> <li><u><b>Height: </b></u>{$Height}</li> <li><u><b>Weight: </b></u>{$Weight}</li> <li><u><b>Ethnicity: </b></u>{$Ethnicity}</li> <li><u><b>Hair Color: </b></u></li> <li><u><b>Shoe Size: </b></u></li> </ol> </li> </ul> </div> <ul class="slides-nav"> <li><a href="#Personal">Personal</a></li> <li><a href="#Contact">Contact</a></li> <li><a href="#Physical">Physical</a></li> <li><a href="#Update">{$Update}</a></li> <li><a href="Profile?ID={$ProfileID}&Page=Photo&PID={$PID}#Photos">Photos/Files</a></li> </ul> </div> this; }else{ if($_SESSION['ID'] == $ProfileID){ echo <<<This <script> $("#file").change(function(){ $("#FileForm").submit(); }); </script> <form enctype="multipart/form-data" method="post" action="/FUpload?ID={$_GET['ID']}" id="FileForm"> <div class="img-upload"> <label for="file"> <img src="https://d2gg9evh47fn9z.cloudfront.net/thumb_COLOURBOX10413282.jpg" width="50" height="50"> </label> <input type="hidden" name="PID" value="{$_GET['PID']}"> <input type="file" name="FileUP" id="file"> </div> </form> This; } echo "<a id='#Photos'></a>"; include("SlideShow/PhotoDisplay.php"); } ?> <hr> </section> <section id="Board"> <h2 class="Title"><?php echo $FullName; ?>'s Board of Posts</h3><br> <form action="/Action?do=Post&ID=<?php echo($_GET['ID']); ?>" method="Post"> <textarea cols="100" rows="10" class="Post" name="Post"></textarea> <input type="hidden" name="Username" value="<?php echo($Username); ?>"> <input type="file"> <br> <input type="Submit" value="Post to Board ≫" class="btn btn-success"> </form> <?php $Board = new Board(htmlentities($_GET['ID']), $Filter); $Board->DisplayPosts($_GET['ID']); ?> </section> <footer> <?php include("./INC/footer.php"); ?> </footer> </div> <!-- /container --> <!-- Bootstrap core JavaScript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="../../dist/js/bootstrap.min.js"></script> <!-- IE10 viewport hack for Surface/desktop Windows 8 bug --> <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script> Here's the friends table data… CODE Array ( [0] => Array ( [Friend1] => 48 [0] => 48 [Friend2] => 5 [1] => 5 [Accepted] => 0 [2] => 0 [DateSent] => 2015-11-17 22:26:55 [3] => 2015-11-17 22:26:55 ) [1] => Array ( [Friend1] => 57 [0] => 57 [Friend2] => 5 [1] => 5 [Accepted] => 0 [2] => 0 [DateSent] => 2015-11-17 22:25:29 [3] => 2015-11-17 22:25:29 ) [2] => Array ( [Friend1] => 5 [0] => 5 [Friend2] => 63 [1] => 63 [Accepted] => 0 [2] => 0 [DateSent] => 2015-11-17 22:24:29 [3] => 2015-11-17 22:24:29 ) [3] => Array ( [Friend1] => 5 [0] => 5 [Friend2] => 48 [1] => 48 [Accepted] => 0 [2] => 0 [DateSent] => 2015-11-18 02:35:13 [3] => 2015-11-18 02:35:13 ) ) Now the bottom line is why is '1' returned when 5 is $User1 and 48 is $User (Friend1 = 5 and Friend2 = 48) so shouldn't 3 be returned instead? I get no errors with error_reporting(E_ALL | E_STRICT); Thanks in advance! |
CharlesEF |
Nov 19 2015, 01:04 AM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Your code is really hard to read since you don't format it at all. I wish you would because it would be a lot easier to read, both for me and you (especially when you have to go back to it after a period of time). Before I get too deep into trying to make sense of it I have spotted a few problems which might affect the code operation.
CODE $Query = $GLOBALS['Connect']->prepare("SELECT * FROM Friends WHERE Friend1 = :F1 AND Friend2 = :F2 OR Friend1 = :F1 AND Friend2 = :F2"); Why the duplicate conditions? You don't need the OR condition at all since it is exactly the same as the first condition. CODE if(!$_SESSION['Logged_in']){ die("<b><u>You must be logged in to view this profile. Click <a href='/Login'>here</a> to Login, or <a href='/Register'>here </a>to be registered for free!</u></b>"); }; Why the ; after the closing curly brace? CODE if($_SESSION['ID'] == $ProfileID){ echo <<<This <script> $("#file").change(function(){ $("#FileForm").submit(); }); Why the closing ), shouldn't it be a closing curly brace? I think you get a '1' returned from the isFriend function because of your if statements. Based on the if statement the only time a '3' is returned is when $User1 = 5 and $Results['Friend2'] = 5. Even if that statement is true it could be changed when the code falls through to the next if statement. Right now you have if, if, elseif, else when I think you need if, elseif, elseif, else. Right now the 2nd if statement is true, $User1 = 5 and $Results['Friend1'] = 5 so a '1' is returned. This post has been edited by CharlesEF: Nov 19 2015, 01:07 AM |
masonh928 |
Nov 19 2015, 05:51 AM
Post
#3
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
How is friend1 5?
|
CharlesEF |
Nov 19 2015, 06:47 AM
Post
#4
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
How is friend1 5? Well, because you said it was: "Now the bottom line is why is '1' returned when 5 is $User1 and 48 is $User (Friend1 = 5 and Friend2 = 48) so shouldn't 3 be returned instead? I get no errors with error_reporting(E_ALL | E_STRICT);" Your quote did say "48 is $User" but I assumed you meant "48 is $User2". Also, I assumed your Array sample data came from the SQL query in the isFriend($User1, $User2) function. But that array doesn't really make sense to me. If you look at your SQL query you are only returning data that meets the "WHERE Friend1 = $User1 AND Friend2 = $User2" condition. So, only 1 record should be returned. I mean you do have a UNIQUE CONSTRAINT defined for Friend1 and Friend2 right? This means the only if tests you really need are to check the $Results['Accepted'] code. Have I misunderstood your question? |
masonh928 |
Nov 19 2015, 07:04 AM
Post
#5
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
According to the array, it's 48. In this case…
|
masonh928 |
Nov 19 2015, 07:06 AM
Post
#6
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
QUOTE(Masonh928) Friend1 = 5 and Friend2 = 48 |
CharlesEF |
Nov 19 2015, 07:19 AM
Post
#7
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I don't understand your last 2 posts. Where are you expecting the '3' to be returned from? The isFriend($User1, $User2) function, right?
|
Darin McGrew |
Nov 19 2015, 02:27 PM
Post
#8
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
Just a guess, but perhaps this:
QUOTE $Query = $GLOBALS['Connect']->prepare("SELECT * FROM Friends WHERE Friend1 = :F1 AND Friend2 = :F2 OR Friend1 = :F1 AND Friend2 = :F2"); should really be this: QUOTE $Query = $GLOBALS['Connect']->prepare("SELECT * FROM Friends WHERE Friend1 = :F1 AND Friend2 = :F2 OR Friend1 = :F2 AND Friend2 = :F1"); |
masonh928 |
Nov 19 2015, 06:36 PM
Post
#9
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Yes charles, I found the issue to this problem. I had two rows where 48 and 5 were together. I deleted the one where 5 was Friend1 and 48 was Friend2. So it's now:
CODE Array ( [0] => Array ( [Friend1] => 48 [0] => 48 [Friend2] => 5 [1] => 5 [Accepted] => 0 [2] => 0 [DateSent] => 2015-11-17 22:26:55 [3] => 2015-11-17 22:26:55 ) [1] => Array ( [Friend1] => 57 [0] => 57 [Friend2] => 5 [1] => 5 [Accepted] => 0 [2] => 0 [DateSent] => 2015-11-17 22:25:29 [3] => 2015-11-17 22:25:29 ) [2] => Array ( [Friend1] => 5 [0] => 5 [Friend2] => 63 [1] => 63 [Accepted] => 0 [2] => 0 [DateSent] => 2015-11-17 22:24:29 [3] => 2015-11-17 22:24:29 ) ) Now isFriend() returns 0. 0 = Not Friends |
masonh928 |
Nov 19 2015, 06:36 PM
Post
#10
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
I want 3 to be returned by isFriend();
|
CharlesEF |
Nov 20 2015, 03:22 AM
Post
#11
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
|
masonh928 |
Nov 20 2015, 05:20 PM
Post
#12
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
I fixed it. The query had an issue that to me I couldn't see. Plus the if and elseif needed fixing.
|
CharlesEF |
Nov 20 2015, 06:30 PM
Post
#13
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
|
masonh928 |
Nov 20 2015, 10:32 PM
Post
#14
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Mind you, the syntax issue you pointed out were due to the fact that I copied it from another forum in which I posted it.
|
Lo-Fi Version | Time is now: 26th April 2024 - 11:42 PM |