我在这里遇到了一个问题,我在未回答的问题mysql query returning false even when values DO exist in table? Trying to find if not in table?
中进行了描述基本上,我试图回显一个特殊的图像以及链接IF如果用户名字符串在数组中,这意味着已经开始与它们进行对话。输入搜索栏时会发生这种情况。
我已经成功存储了那些不应该打印或打印特殊图像的用户:
$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>";
}
这很有效。搜索出现问题 - 此代码会为每个用户打印convo数组的大小,因为每次for循环都会调用echo -
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>";
}
}
}
}
}//
此代码确实识别搜索到的用户何时在阵列中并且DOES打印特殊图像,但是它再次执行此操作多次。
我需要帮助,没有这个就无法完成我的项目。在这里挣扎了一个星期。
如何使用特殊图像打印阵列中的用户?或者让搜索完全没有打开它们?
我做错了什么?
答案 0 :(得分:1)
使用此[假设$ convoArray包含用户名是其数组值]
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>";
}
而不是以下代码
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>";
}
}