我目前正在开发友谊系统。要接受朋友,我需要获取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 ) )
现在他正在更新,但同时我在我的函数AcceptFriendRequest中的这一行得到一个错误(致命错误:调用非对象上的成员函数fetch_assoc()):
'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(); }
答案 0 :(得分:1)
GetAllFriendRequests函数
您返回的数组的结构是问题所在。而不是
while ($row = mysqli_fetch_array($result)) {
$result_array[]=$row;
}
做:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$result_array[]=$row;
}
另外,仅供参考,你在GetAllFriendRequests函数中不需要这一行:
$result_array=array();
因为此行中的括号(在while循环内)使其自动成为一个数组:
$result_array[]=$row;
主要PHP文件
您的主PHP文件中的表单也需要更改。这打印出所有相关数据,但我改变了一些 - 提交按钮现在都有一个名称,因此它们将出现在POST数组中。此外,还有一个隐藏的输入,您需要运行AcceptFriendRequest函数才能使用friendship_id。如果他们单击Accept,则(1)将数据POST回到同一文件并刷新页面,(2)if(isset)条件检查他们是否已接受请求。 (3)它使用我们存储在隐藏输入中的friendship_id调用AcceptFriendRequest函数。页面上的每个表单都会有一个隐藏的输入。如果您需要来自特定请求的更多变量,则可能需要其他隐藏输入 - 例如,如果您需要其他数据来运行Decline函数。
<?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
}
?>