0

table: request

id          friend_id           candidate_id
==          ==============      ==============
1           20180928115958      20180925112428

2           20181008181142      20180925112428

3           20180928115958      20181008181142

4           20181010181207      20180928115958

5           20181008181142      20181010181207

6           20181010181207      20180925112428

Query

<?php
    $_SESSION['candidate_id']=20181010181207;

    $sql_ac = mysqli_query($con,"select friend_id,candidate_id from request where candidate_id='".$_SESSION['candidate_id']."' or friend_id='".$_SESSION['candidate_id']."'");
    while($row_ac = mysqli_fetch_array($sql_ac))
    {
        if($row_ac['friend_id']==$_SESSION['candidate_id'] && $row_ac['candidate_id']==$_SESSION['candidate_id'])
        {
            echo "";
        }
        else
        {
            echo $row_ac['friend_id']."<br/><br/>";
            echo $row_ac['candidate_id']."<br/><br/>";
        }
    }
?>

Unexpected output:

20181010181207

20180928115958

20181008181142

20181010181207

20181010181207

20180925112428

Expected Output

20180928115958

20181008181142

20180925112428

In this code I have table request which have two key field i.e. friend_id and candidate_id. Now, I $_SESSION['candidate_id']='20181010181207'. So, How can I get expected output as I have mention above? Please help me.

Thank You

5
  • Still it showing 20181010181207 20180928115958 20181010181207 20180925112428 @MickaelLeger Commented Oct 11, 2018 at 12:43
  • It is doing exactly what you're asking it to do. It is returning 3 rows which match candidate or friend. Then, for each one of those rows, you're outputting both the candidate ID and also the friend ID. Maybe, if you tell us what you're trying to do we could help. Commented Oct 11, 2018 at 12:43
  • Do you want to output friend_id when candidate_id matches and vice versa when friend_id matches? Commented Oct 11, 2018 at 12:46
  • have two ifs: if(($row_ac['friend_id'] != $_SESSION['candidate_id'] { echo $row_ac['friend_id'] } - same for conadidate_id Commented Oct 11, 2018 at 12:46
  • I want if this id 20181010181207 present in both friend_id and candidate_id. it will not show Commented Oct 11, 2018 at 12:49

3 Answers 3

1

I think you want:

while($row_ac = mysqli_fetch_array($sql_ac))
{
    if($row_ac['friend_id'] != $_SESSION['candidate_id']
        echo $row_ac['friend_id']."<br/><br/>";
    }
    if($row_ac['candidate_id'] != $_SESSION['candidate_id']
        echo $row_ac['candidate_id']."<br/><br/>";
    }
}

This will display the values, that are not the same as the 'input', in other words remove the 20181010181207 entries.

Or as a oneliner:

echo $row_ac['candidate_id']==$_SESSION['candidate_id'] ? $row_ac['friend_id] : $row_ac['candidate_id'];
Sign up to request clarification or add additional context in comments.

3 Comments

exactly I need this. Thanks @Jeff
You are welcome! There might be a better way in achieving this already on sql level. But it's hard to tell if it would fit your needs.
See @MickaelLeger s answer for a sql version!
1

Your request is :

select 
  friend_id,
  candidate_id
from request 
where candidate_id=:candidate_id or friend_id=:candidate_id

So it's normal that you will return ALL row where :candidate_id is in column friend_id or candidate_id :

4           20181010181207      20180928115958

5           20181008181142      20181010181207

6           20181010181207      20180925112428

Now what you want is :

20180928115958 // this is candidate_id of row 4

20181008181142 // this is friend_id of row 5

20180925112428 // this is candidate_id of row 6

If you only want the value of the column "XXX_id" that is not equal to :candidate_id, try this :

select 
  IF(friend_id = :candidate_id, '', friend_id) as friend_id,
  IF(candidate_id = :candidate_id, '', candidate_id) as candidate_id
from request 
where candidate_id=:candidate_id or friend_id=:candidate_id

This way you will return empty value for column that match your :candidate_id.

Now just add a test before displaying it to avoid empty row :

if (!empty($row_ac['friend_id'])
    echo $row_ac['friend_id']."<br/><br/>";
if (!empty($row_ac['candidate_id'])
    echo $row_ac['candidate_id']."<br/><br/>";

You can do this too :

select 
  IF(friend_id = :candidate_id, candidate_id, friend_id) as result_id
from request 
where candidate_id=:candidate_id or friend_id=:candidate_id

This way you will return only ONE result : the one that is not equal to :candidate_id.

Then just do echo $row_ac['result_id']."<br/><br/>"; to get what you want

Comments

0

try this one

i hope it will working

if($row_ac['friend_id']==$_SESSION['candidate_id']){
    echo $row_ac['candidate_id']."<br/><br/>";
else{
    echo $row_ac['friend_id']."<br/><br/>";
}

Because And condition always false in your case and else part will execute so try with if else condition and check

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.