Help - Search - Members - Calendar
Full Version: What should I do to fix this SQL?
HTMLHelp Forums > Programming > Server-side Scripting
masonh928
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'>&nbsp; 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!
masonh928
Thanks
CharlesEF
Please explain a little more, I don't understand your question.
masonh928
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
Does that make sense?
CharlesEF
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
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
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
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
Ok you're the best, it works! tongue.gif
CharlesEF
Glad it worked out for you.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.