0

I have a problem here that Ive described in the unanswered question mysql query returning false even when values DO exist in table? Trying to find if not in table?

Basically, I am trying to echo a special image along with a link IF a username string is in an array that means a conversation has been started with them. This all happens when a search bar is entered in.

I have succeeded in storing the users who either SHOULD NOT BE PRINTED or PRINTED WITH A SPECIAL IMAGE here:

$convoArray = array();

                echo '<form class="usersearchpm" method="post"><input class="searchbarpm" name="searchbarpm"></input></form>';
                $con = mysqli_connect("localhost","username","password","sqlserver");

                    //$num = mysqli_query($con, "SELECT * FROM `pm_messages` WHERE user_from=".$account['id']."");
                $numCon = mysqli_query($con, "SELECT * FROM `conversation` WHERE user_one=".$account['id']."");
                    $numrows = mysqli_num_rows($numCon);
                while ($u = mysqli_fetch_assoc($numCon))
                    {
                    //get other users usernames to echo link
    $getUserTwo = mysqli_query($con, "SELECT * FROM `accounts` WHERE id=".$u['user_two']."");
        $s = mysqli_fetch_assoc($getUserTwo); //s[username] is a user that DOES have convo



                    array_push($convoArray, $s['username']);
                    //echo "<a href='message.php?id={$s['id']}'><li><img class = 'dmCircle' src = '../images/chatCircle.png'/>{$s['username']} </li></a>";
                }

This works. Problem comes with the search - this code prints each user the the size of the convo array because the echo is called each time a for loop circles through -

if(isset($_POST['searchbarpm'])){
//  foreach($convoArray as $name)
//  {
//      echo $name;
//  }
//$sess->getUsers();
    $dbh = mysqli_connect("localhost","username","password","sqlserver");
                    $query = $_POST['searchbarpm'];
                    $q = mysqli_query($dbh, "SELECT * FROM sqlserver.accounts WHERE username LIKE '%".$query."%'");
                    //display all the results
                    while($row = mysqli_fetch_assoc($q)){

//                      $checkConvo = mysqli_query($dbh, "SELECT * FROM sqlserver.conversation WHERE user_one=".$user_id." AND user_two=".$row['id']."");

                        if($row['id']!= $user_id) { //only output users they dont have convo going with because theyre already printed!!!

                        for($i=0;$i<sizeof($convoArray);$i++)
                        {
                            if($row['username']==$convoArray[$i])
                            {
                                echo 'match!!';
                                echo $row['username'];
                                echo "<a href='message.php?id={$row['id']}'><li><img class = 'dmCircle' src = '../images/chatCircle.png'/> {$row['username']}</li></a>";
                            }
                            else 
                            {
                                echo "<a href='message.php?id={$row['id']}'><li><img class = 'dmCircle' src = '../images/noChatCircle.png'/> {$row['username']}</li></a>";
                            }
                        }

                        }
                    }
}//

This code does recognize when the user searched for is in the array and DOES print a special image, however again it does this multiple times.

I need help and cannot finish my project without this. Been struggling for a week here.

How can I print the users in the array with a special image? Or have the search not turn them up at all?

What am I doing wrong?

4
  • So the problem is with the 2nd code in if else condition, it is echoing match everytime for you? Commented Jul 1, 2016 at 0:24
  • do a break after you find the match. break; the loop Commented Jul 1, 2016 at 0:27
  • @Dharam Ok that works for the ones with a match but how do I avoid echoing the ones without a match multiple times? Commented Jul 1, 2016 at 0:31
  • don't run that for loop it is wrong, do an array search, posting an answer for you. But you may need to modify it as per your array values Commented Jul 1, 2016 at 0:33

1 Answer 1

1

Use this [Assuming that $convoArray contains the usernames are their array values]

if(in_array($row['username'], $convoArray) {
    echo 'match!!';
    echo $row['username'];
    echo "<a href='message.php?id={$row['id']}'><li><img class = 'dmCircle' src = '../images/chatCircle.png'/> {$row['username']}</li></a>";
}
else {
    echo "<a href='message.php?id={$row['id']}'><li><img class = 'dmCircle' src = '../images/noChatCircle.png'/> {$row['username']}</li></a>";
}

Instead of the following code

for($i=0;$i<sizeof($convoArray);$i++)
{
    if($row['username']==$convoArray[$i])
    {
        echo 'match!!';
        echo $row['username'];
        echo "<a href='message.php?id={$row['id']}'><li><img class = 'dmCircle' src = '../images/chatCircle.png'/> {$row['username']}</li></a>";
    }
    else 
    {
        echo "<a href='message.php?id={$row['id']}'><li><img class = 'dmCircle' src = '../images/noChatCircle.png'/> {$row['username']}</li></a>";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.