mysql – php left join with if else to show particular html button tag-ThrowExceptions

Exception or error:

hi i’m working on social media project something similar to facebook and instgram. here i have 2 tables one is Users table and another is Friendlist table where in users table i have all users who ever signup details and in friendlist table i have friend request relationship of one to many. evrything is working fine. now my condition is if the both friends are already friends then in friend list it show send msg else if no friends then it show add friend. (to check wheather friends or not i have column in mysql db as accept)

here is my db structure of friendlist:-

 friend_id | userid | friendId | accept | response |
 +-----------+--------+----------+--------+----------+
 |         1 |      4 |        1 |      1 |        1 |
 |         2 |      5 |        1 |      1 |        1 |
 |         3 |      2 |        4 |      1 |        1 |

and here is Users table:-

id | nickname      | email                 | ph_num  | password                         | 
profile_pic                      | status | Created_at          |
+----+---------------+-----------------------+---------+----------------------------------+- 
---------------------------------+--------+---------------------+
|  1 | prasanna      | prasanna@gmail.com    | 12345   | 0a2b75a016a758e08888d66ba244126f | 
1583493901_2020-02-14-110313.jpg |      1 | 2020-03-05 12:52:37 |
|  2 | demo          | demo@d.d              | 1234    | fe01ce2a7fbac8fafaed7c982a04e229 | 
NULL                             |      0 | 2020-03-05 14:15:51 |
|  4 | test          | test@g.g              | 56456   | 098f6bcd4621d373cade4e832627b4f6 | 
1583494768_pic.png               |      0 | 2020-03-06 16:29:43 |
|  5 | one_more_user | onemoreuser@gmail.com | 9988776 | 57bc707659ecf8998c9251b3aae18bd0 | 
NULL                             |      0 | 2020-03-07 14:00:26 |

tried query :

$result = "SELECT u.*, f.friendId, f.userid, f.accept FROM Users u LEFT JOIN friendlist f ON 
u.id = f.userid";
$userlist = mysqli_query($conn, $result);

in while loop:

 <?php
       while ($list = $userlist->fetch_assoc()):?>
        <?php if ($_SESSION['id'] !== $list['id']):?>
        <li class="list-group-item"  >  
       <img class="onlineimg" src="../uploads/<?php echo $list['profile_pic']; ?>"/>
        <?php echo $list['nickname']; ?>
        <?php if ($_SESSION['id'] == $list['friendId']):?>
            <?php if ($list['accept'] ==  '0'):?>
        <p style="display: none" class="selected"><?php echo $list['id']; ?> </p>
        <button class="btn btn-success float-right"><span style="display: none"><?php echo 
       $list['id'] ?> </span> add friend</button>
       <?php endif ;?>
       <?php endif ;?>
        <?php if ($list['accept'] ==  '1'):?>
        <p style="display: none" class="selected"><?php echo $list['id']; ?> </p>
        <button class="btn btn-success float-right"><span style="display: none"><?php echo 
       $list['id'] ?> </span> send message</button>
       <?php endif ;?>
      </li>
    <?php endif ;?>
      <?php endwhile;?>

demo image

from the above image the user demo is showing send message instead of add friend coz demo is not friend to the prasanna.

please help me to come through this i stuck here from past of some hours. thank you advance and sorry for my english grammer.

How to solve:

As per yor code logic the only help possible is comments added:-

if(session_id == friendID)
{
   if(accept == 0)
  {
    // add friend
  } //you are missing else condition here "what if accept == 1"
} //you are missing else condition here "what if session_id != friendID
//and code below does not belong here at all
 if(accept == 1)
  {
    // send message
  }

Leave a Reply

Your email address will not be published. Required fields are marked *