0

the code i have below basically behaves as a friend search engine where you can add someone if you don't have them on you account. So i am trying to do the following: If exists it should just display the friend and if not then it should say add friend.

I got it to work but the tail end of the query (please see this comment "//Problem code - when i use this echo below this where it repeats 3 times") is giving me trouble where it repeats Add 3 times.

<?php
$num_rows1 = mysql_num_rows($result);
if ($result == "") {
    echo "";
}
echo "";

$rows = mysql_num_rows($result);
if ($rows == 0) {
    print("<div id=norequests>No results for <strong>$q
                 </strong></div>");

}
elseif ($rows > 0) {
    while ($row = mysql_fetch_array($query))
    {

        $person  = htmlspecialchars($row['full_name']);
        $linksys = htmlspecialchars($row['name']);
        $pid     = htmlspecialchars($row['system_id']);
    }

    print("");
}

}


else{
    echo '<div id="error">No results.</div>';
}

$sql    = "SELECT `Friend_id` from `friends_container` 
                 WHERE `System_id` = '$sid'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die("Error: " . mysql_error());

if ($result == "") {
    echo "";
}
echo "";

$rows = mysql_num_rows($result);
if ($rows == 0) {
    print("");

}
elseif ($rows > 0)
{
    while ($row = mysql_fetch_array($query))
    {
        $existing = htmlspecialchars($row['Friend_id']);
        if ($existing == $pid) {
            echo("<img src=$linksys />$person - Already Existing");
        }

        else
            //Problem code - when i use this echo below this where it repeats 3 times
        {
            echo("Add $person");
        }
    }
?>
ariel
  • 2,962
  • 7
  • 27
  • 31
  • You code needs a good tidy up... indenting within control structures (if, while etc) would be a good start. – Ben Everard May 02 '12 at 15:45
  • Please format your code properly. A code block is inserted by indenting 4 spaces before any line of code. I've formatted the code for you this time, but please format it properly next time. For further help, see the [Editing FAQ](http://stackoverflow.com/editing-help#code) – Madara's Ghost May 02 '12 at 15:49
  • You're probably getting three rows back that's why. What are you expecting to get back ? – aziz punjani May 02 '12 at 15:49
  • You have an extra closing curly bracket, you're also printing a lot of empty strings for some reason. – Madara's Ghost May 02 '12 at 15:50
  • @truth thanks but you might have added that by mistake while editing because the code i posted only had two at the end. – ariel May 02 '12 at 15:52
  • @interstellar_coder the test account i am using has 3 friends so i am guessing it repeats the "Add" 3 times because of that.. – ariel May 02 '12 at 15:53
  • @truth you left out some of my code as well from the top. – ariel May 02 '12 at 16:01
  • @ariel: I didn't mean at the end. In the middle, right after your first `elseif`. Also, I didn't add anything, I had my IDE auto tidy your code. In addition, should the document only contain PHP code (with no HTML), the last closing PHP tag can be omitted. – Madara's Ghost May 02 '12 at 16:03
  • I fixed the problem. instead of using echo("$person - Already Existing"); it was suppose to be die(""); – ariel May 02 '12 at 16:42

1 Answers1

1

You must have 3 images stored for each account.

Your query will always result in a multiple result. what you should do is use php to convert it to something which will result in a better option or say convert the result in an array for convenience.

Vipin Jain
  • 1,382
  • 1
  • 10
  • 19
  • I think its because i only have 3 friends in the test account thats why it repeats 3 times..? – ariel May 02 '12 at 15:50
  • you have changed your code after my reply. you were using left join in the query!!!!! Now i have to re read your question – Vipin Jain May 03 '12 at 09:36