skyguy skyguy - 5 months ago 18
PHP Question

PHP - echo link ONLY ONCE if string is not in array? link echoed multiple times?

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?

Answer

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>";
    }
}