What should I do to fix this SQL? |
What should I do to fix this SQL? |
masonh928 |
Nov 9 2015, 08:31 PM
Post
#1
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Ok, so basically my table is set up for friends:
--------------------------- | Friends | --------------------------- | Friend1 | Friend2 | | 6 | 78 | | 6 | 7 | | 23 | 6 | | | --------------------------- My code: CODE <?php include_once($_SERVER['DOCUMENT_ROOT'] . "/Scripts/PHP/DB.inc.php"); include_once($_SERVER['DOCUMENT_ROOT'] . "/Scripts/PHP/Class/Profile.class.php"); error_reporting(E_ALL); $Connect = ConnectDB(); $Connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $Query = $Connect->prepare("SELECT * FROM Friends WHERE Friend1 = :F1 OR Friend2 = :F1 AND Accepted = :ID"); $Query->bindValue(":ID", "1"); $Query->bindValue(":F1", $_SESSION['ID']); $Query->execute() or die("FAILED"); $Results = $Query->fetchAll(); $Pending = $Connect->prepare("SELECT * FROM Friends WHERE Accepted = :ID AND Friend1 = :F1"); $Pending->bindValue(":F1", $_SESSION['ID']); $Pending->bindValue(":ID", "0"); $Pending->execute() or die("Error"); $Results2 = $Pending->fetchAll(); echo("<div id='Content'>". PHP_EOL . "<h2><b>Friends (". count($Results) .")</b></h2><br>"); foreach($Results as $Rows){ $Class = new Profile($Rows['Friend2']); $FullName = $Class->getUserData("FullName"); $Class = new Profile($Rows['Friend1']); $FullName2 = $Class->getUserData("FullName"); echo <<<HTML <a href="/Profile?ID={$Rows['Friend2']}">{$FullName}</a><br> <a href="/Profile?ID={$Rows['Friend1']}">{$FullName2}</a><br> HTML; } echo("<br><b>Pending Friends (". count($Results2) .")</b><a href='/Notifications'> Manage Requests here</a>"); echo("</div>"); Basically Friend1 is the sender. In this case the current user is '6', 6 isn't always the sender sometimes he's the receiver. Now to include those friends, in the case where he is the receiver, it will echo his name along with the others. How should I fix this? Thanks! This post has been edited by masonh928: Nov 9 2015, 08:48 PM |
masonh928 |
Nov 9 2015, 08:45 PM
Post
#2
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Thanks
This post has been edited by masonh928: Nov 9 2015, 08:49 PM |
CharlesEF |
Nov 9 2015, 10:30 PM
Post
#3
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Please explain a little more, I don't understand your question.
|
masonh928 |
Nov 9 2015, 10:34 PM
Post
#4
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
so basically the code prints out:
6 6 78 6 23 7 I don't want 6 to be displayed. It's like facebook or other sites with contacts. Basically I want the code to list all the users who are friends with this user. But it's a 2 way system. Friend 1 - sends friend request Friend 2 - receives request the problem is the user doesn't always receive nor does he send all the time. |
masonh928 |
Nov 9 2015, 10:41 PM
Post
#5
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Does that make sense?
|
CharlesEF |
Nov 10 2015, 12:02 AM
Post
#6
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
So, does this mean you only want this print out:
78 23 7 I don't do Facebook or any other social media crap. Any friends I want I know in person. |
masonh928 |
Nov 10 2015, 06:33 AM
Post
#7
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
yeah, I only want 78, 23, and 7 to print. I don't really do social media either a lot, but the concept is what I'm talking about. It's similar to this forum. You can add contacts.
|
CharlesEF |
Nov 10 2015, 12:56 PM
Post
#8
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Well, I think you need to change your code a little. Since you need the $_SESSION['ID'] in the SQL query I think you will have to exclude that value in an if statement inside the foreach loop. Maybe something like this:
CODE foreach($Results as $Rows){ if($Rows['Friend1'] != $_SESSION['ID'] || $Rows['Friend2'] != $_SESSION['ID']){ $Class = new Profile($Rows['Friend2']); $FullName = $Class->getUserData("FullName"); $Class = new Profile($Rows['Friend1']); $FullName2 = $Class->getUserData("FullName"); echo <<<HTML <a href="/Profile?ID={$Rows['Friend2']}">{$FullName}</a><br> <a href="/Profile?ID={$Rows['Friend1']}">{$FullName2}</a><br> HTML; } } |
CharlesEF |
Nov 10 2015, 03:01 PM
Post
#9
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Something I just thought of is that you might need to break up the if statement into 2 if statements. This way if $Rows['Friend1'] is equal to $_SESSION['ID'] then you only process the $Rows['Friend2'] code. The same logic will hold for $Rows['Friend2'] but you only process $Rows['Friend1'] code.
|
masonh928 |
Nov 10 2015, 03:37 PM
Post
#10
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Ok you're the best, it works!
|
CharlesEF |
Nov 10 2015, 04:47 PM
Post
#11
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Glad it worked out for you.
|
Lo-Fi Version | Time is now: 25th April 2024 - 07:13 AM |