0

I'm currently working on a friendshipsystem. To accept a friend I need to get the friendship_id value.

Based on the email (from the session) I can get a lot of information, such as surname, name, photo, ID NUMBER,... from the specific friendshiprequest

I can print the information for each friendship in this way. So I have the FRIENDSHIP ID VALUE TO DISPLAY AS INFORMATION but now I want to use it in a function.

$friendrequests=$friendship->GetAllFriendRequests($email);

<?php
    foreach ($friendrequests as $request) {
    echo "
        <div><p>
            <a href='profile.php?user_id=".$request['friendship_applicant_id'] . "'>
                <img src='uploads/" . $request['friendship_applicant_avatar']  . " " . " ' alt='' />" . $request['friendship_applicant_surname'] . $request['friendship_id'] . " " . $request['friendship_applicant_name'] . "
            </a> has send you a friend request" . "

            <form action='" . $_SERVER['REQUEST_URI'] . "' method='post'>
                <button type='submit' name=''>Accept</button>
                <button type='submit' name=''>Decline</button>
            </form>
        </p></div>";   
}
                    ?>

So i tried to get the specific number with the following code, but it says undefined index for friendship_id: $friendrequestnumber = $friendrequests['friendship_id'];

This is the code the GetAllFriendRequests function. Can I use this code or should I do it in a totally different way?

public function GetAllFriendRequests($email) {
    $db = new Db();

    $select = "SELECT * FROM friendship WHERE friendship_recipient = '" . $email . "' AND friendship_status = 'pending' ORDER BY friendship_id DESC";

    $result = $db -> conn -> query($select);

    $result_array=array();
    while ($row = mysqli_fetch_array($result)) {
         $result_array[]=$row;                                                                                          
    }
    return $result_array;

}

With a var_dump I get this information of the 2 requests:

Array ( [0] => Array ( [0] => 84 [friendship_id] => 84 [1] => ron@hot.com [friendship_applicant] => ron@hot.com [2] => 29 [friendship_applicant_id] => 29 [3] => ron [friendship_applicant_name] => ron [4] => ron [friendship_applicant_surname] => ron [5] => 1394134610fuckyou.jpg [friendship_applicant_avatar] => 1394134610fuckyou.jpg [6] => jan@hot.com [friendship_recipient] => jan@hot.com [7] => 1 [friendship_recipient_id] => 1 [8] => Vandenbergh [friendship_recipient_name] => Vandenbergh [9] => Jan [friendship_recipient_surname] => Jan [10] => 1394041001fuckyou.jpg [friendship_recipient_avatar] => 1394041001fuckyou.jpg [11] => Pending [friendship_status] => Pending [12] => 0000-00-00 00:00:00 [friendship_time] => 0000-00-00 00:00:00 ) [1] => Array ( [0] => 78 [friendship_id] => 78 [1] => Bert@hot.com [friendship_applicant] => Bert@hot.com [2] => 2 [friendship_applicant_id] => 2 [3] => Van Damme [friendship_applicant_name] => Van Damme [4] => Bert [friendship_applicant_surname] => Bert [5] => sdfds.png [friendship_applicant_avatar] => sdfds.png [6] => Jan@hot.com [friendship_recipient] => Jan@hot.com [7] => 1 [friendship_recipient_id] => 1 [8] => Vandenbergh [friendship_recipient_name] => Vandenbergh [9] => Jan [friendship_recipient_surname] => Jan [10] => 1394041001fuckyou.jpg [friendship_recipient_avatar] => 1394041001fuckyou.jpg [11] => Pending [friendship_status] => Pending [12] => 0000-00-00 00:00:00 [friendship_time] => 0000-00-00 00:00:00 ) )

Now he's updating but at the same time I get an error (Fatal error: Call to a member function fetch_assoc() on a non-object) at this line in my function AcceptFriendRequest:

'the return $data=$result->fetch_assoc();'

public function AcceptFriendRequest($requestnumber){ $db = new Db(); 
$select = "UPDATE friendship SET friendship_status = 'Accepted' WHERE 
friendship_id ='" . $requestnumber . "'"; $result = $db->conn->query($select); 
return $data=$result->fetch_assoc(); }
Marnix Verhulst
  • 161
  • 1
  • 2
  • 9
  • Are you sure the column in the database is "friendship_id"? – larsAnders Mar 06 '14 at 19:11
  • yes it's friendship_id. It says: Notice: Undefined index: friendship_id – Marnix Verhulst Mar 06 '14 at 19:13
  • Just after this line: $friendrequests=$friendship->GetAllFriendRequests($email); can you add print_r($friendrequests); and take a look at the array your function is returning? Go ahead and post those results if it doesn't make things immediately clear. – larsAnders Mar 06 '14 at 19:15
  • It is printing the whole array from friendship_id 78? Array ( [0] => 78 [friendship_id] => 78 [1] => Bert@hot.com [friendship_applicant] => Bert@hot.com [2] => 2 [friendship_applicant_id] => 2 [3] => Van Damme [friendship_applicant_name] => Van Damme [4] => Bert [friendship_applicant_surname] => Bert [5] => sdfds.png [friendship_applicant_avatar] => sdfds.png [6] => Jan@hot.com [friendship_recipient] => Jan@hot.com [7] => 1 [friendship_recipient_id] => 1 [8] => Vandenbergh [friendship_recipient_name] => Vandenbergh [9] => Jan [friendship_recipient_surname] => Jan [10] => 139404100.jpg.... – Marnix Verhulst Mar 06 '14 at 19:18
  • Try while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) and then print_r and we'll see what we have. – larsAnders Mar 06 '14 at 19:27
  • Do you mean this? It only gives "array()" public function GetAllFriendRequests2($email) { $db = new Db(); $select = "SELECT * FROM friendship WHERE friendship_recipient = '" . $email . "' AND friendship_status = 'pending' ORDER BY friendship_id DESC"; $result = $db -> conn -> query($select); $result_array=array(); while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) ; return $result_array; } – Marnix Verhulst Mar 06 '14 at 19:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49176/discussion-between-marnix-verhulst-and-larsanders) – Marnix Verhulst Mar 06 '14 at 19:47

1 Answers1

1

GetAllFriendRequests function

The structure of the array you're returning is the problem. Instead of

while ($row = mysqli_fetch_array($result)) {
    $result_array[]=$row;
}

Do:

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $result_array[]=$row;
}

Also, FYI, you don't need this line in the GetAllFriendRequests function:

$result_array=array();

Because the brackets in this line (inside the while loop) make it an array automatically:

 $result_array[]=$row;  

Main PHP file

Your form in your main PHP file needs to change as well. This prints out all the relevant data, but I've changed a few things - both submit buttons now have a name, so they will appear in the POST array. Also, there is a hidden input with the friendship_id you'll need to run your AcceptFriendRequest function. If they click Accept, then (1) the data is POSTed back to the same file and the pages refreshes, (2) the if(isset) condition checks to see if they have accepted the request. (3) It calls the AcceptFriendRequest function using the friendship_id we stored in the hidden input. Each form on the page will have a hidden input like this. You may need other hidden inputs if you need more variables from the specific request - for instance if you need other data to run the Decline function.

<?php
foreach ($friendrequests as $request) {
echo "
    <div><p>
        <a href='profile.php?user_id=".$request['friendship_applicant_id'] . "'>
            <img src='uploads/" . $request['friendship_applicant_avatar']  . " " . " ' alt='' />" . $request['friendship_applicant_surname'] . $request['friendship_id'] . " " . $request['friendship_applicant_name'] . "
        </a> has send you a friend request" . "

        <form action='" . $_SERVER['REQUEST_URI'] . "' method='post'>
            <button type='submit' name='Accept'>Accept</button>
            <button type='submit' name='Decline'>Decline</button>
            <input type='hidden' name='acceptID' value='".$request['friendship_id']."' />
        </form>
    </p></div>";   
}//foreach

if (isset($_POST['Accept'])){
    $accepted = AcceptFriendRequest($_POST['acceptid']);
    //for debugging, you may want to uncomment this and watch what your function returns
    //echo $accepted;
}

if (isset($_POST['Decline'])){
    //some other function is the request is declined
}

?>
larsAnders
  • 3,813
  • 1
  • 15
  • 19
  • Now he's updating but at the same time I get an error at this line 'the return $data=$result->fetch_assoc();' Fatal error: Call to a member function fetch_assoc() on a non-object larsAnders larsAnders does it tell you what file and line? Marnix Verhulst public function AcceptFriendRequest($requestnumber){ $db = new Db(); $select = "UPDATE friendship SET friendship_status = 'Accepted' WHERE friendship_id ='" . $requestnumber . "'"; $result = $db->conn->query($select); return $data=$result->fetch_assoc(); } – Marnix Verhulst Mar 06 '14 at 23:40