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 } ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
lucabodd
  • 65
  • 1
  • 9
  • 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 {}`. – Marc B Feb 12 '15 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. – Funk Forty Niner Feb 12 '15 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 – lucabodd Feb 12 '15 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 – lucabodd Feb 12 '15 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 – Amol Bharmoria Feb 13 '15 at 09:07
  • Your code suggests that you are storing friend ids as a string of comma-separated values. You should properly normalize that, otherwise you are very likely to run into problems later on. – CBroe Feb 13 '15 at 09:45
  • As for your problem, do a `var_dump($profileid, $friends);` above your form, to see what those variables actually contain. – CBroe Feb 13 '15 at 09:46

1 Answers1

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!

Chris.C
  • 297
  • 3
  • 17
  • The ' inside " and vise versa is just the same. Check this out http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Vick Feb 13 '15 at 08:59
  • I try as you say ... but the problem persist – lucabodd Feb 13 '15 at 16:32