0

I have this problem: on the following code i made a form with a condition, where if the "profileid" is in array friends then print the button "add to friends" else "remove to friends" but the second condition don't work it don't prints anything and when i load the page for the first time, there is ever the button "add to friends" either if there's already the "friend id" in the array.

Here is my code:

<?php
        $userid = $_SESSION['userid'];
        $profileid = $_SESSION['profileID'];
        $compressed_friends=mysql_query("SELECT friends FROM users WHERE id LIKE '$userid'");
        $friends = explode (',',$compressed_friends);
        if(isset($_POST['addFriends']))
        {
            $compressed_friends=$profileid.','.$compressed_friends;
            mysql_query("UPDATE users SET friends='$compressed_friends' WHERE id='$userid'");
        }
        elseif(isset($_POST['removeFriends']))
        {
            array_filter($friends,$profileid);
            $compressed_friends=implode(',', $friends);
            mysql_query("UPDATE users SET friends='$compressed_friends' WHERE id='$userid'");
        }

        else
        {
        ?>
        <form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <?php
            if(!in_array($profileid, $friends))
            {
                echo' <button type="submit" name="addFriends" class="btn btn-primary col-lg-3">Add to friends</button>';
            }
            elseif(in_array($profileid, $friends))
            {
                echo '<button type="submit" name="removeFriends" class="btn btn-danger col-lg-3">Remove to Friends</button>';
            }
        ?>
        </form>
        <?php } ?>
7
  • you've commend out the echo, so how could it print anything? As well, your if is redundant if (true) {} else if (false) {} could just be if(true) {} else {}. Commented Feb 12, 2015 at 18:26
  • 1
    Did you start the session? Seeing you're using them. If not, add session_start(); at the top under your opening php tag. It's required when using sessions. Commented Feb 12, 2015 at 18:30
  • yes i started the session, and if you see i removed the commend out istruction , it was just an error while i copy and paste the code Commented Feb 12, 2015 at 20:08
  • yes i started the session it is not in this code , it is in a "core.php" page that i include in this file Commented Feb 12, 2015 at 20:09
  • maybe there is some problem with blank spaces . You can try trim function to remove whitespaces in begining and end of your id,while using in_array Commented Feb 13, 2015 at 9:07

1 Answer 1

1

Let me try to answer as I am a beginner in PHP. I found there is the problem on echo. You used ' to cover the ".

  echo' <button type="submit" name="addFriends" class="btn btn-primary col-lg-3">Add to friends</button>';

You can try this out, perhaps it will solve your problem.

  echo  "<button type='submit' name='addFriends' class='btn btn-primary col-lg-3'>Add to friends</button>";

Thanks!

Sign up to request clarification or add additional context in comments.

2 Comments

The ' inside " and vise versa is just the same. Check this out stackoverflow.com/questions/3446216/…
I try as you say ... but the problem persist

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.