获取特定的id值以供使用

时间:2014-03-06 21:59:36

标签: php arrays logic

我目前正在开发友谊系统。要接受朋友,我需要获取friendship_id值。

根据电子邮件(来自会话)我可以从特定的friendshiprequest获取大量信息,例如姓氏,姓名,照片,ID号码......

我可以用这种方式打印每个友谊的信息。所以我有FRIENDSHIP ID值显示为信息,但现在我想在一个函数中使用它。

$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>";   
}
                    ?>

所以我尝试使用以下代码获取特定数字,但它表示friendship_id的未定义索引:$friendrequestnumber = $friendrequests['friendship_id'];

这是GetAllFriendRequests函数的代码。我可以使用此代码,还是应该以完全不同的方式执行此操作?

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;

}

使用var_dump我得到2个请求的信息:

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 ) )

1 个答案:

答案 0 :(得分:1)

在你的var_dump中,$friendrequests是一个嵌套数组。因此,您需要使用循环来迭代所有值,例如上面$request['friendship_id']可以使用它的情况,或者如果您只想要第一个值,请使用$friendrequests[0]['friendship_id']