我对以下代码有一个非常奇怪的问题。出于某种原因,除非将其放在 if 和 else...if 语句之外,否则我无法让 $id1 或 $id2 保存值。变量根本不会注册。有人看到我做错了吗?我很茫然。。
public function addCommentToStartpageWall() {
$userid = $this->session->userdata("userid");
$postingUserId = $this->input->post("postinguserid");
$query = $this->db->query("SELECT * FROM churchmembers");
$row = $query->row();
foreach ($query->result() as $row) {
if ($postingUserId == $row->cMuserId) { // check to see what church the posting user is a member of
$id1 = $row->cMchurchId; // if posting user is a member of a church set it to var id1
} if ($userid == $row->cMuserId) { // check to see what church myuserid is a member of
$id2 = $row->cMchurchId; // if myuserid is a member of a church set it to var2
} if ($id1 == $id2) { // if posting user and myuserid are a member of the same church process the following
echo json_encode(array('id1' => $id1, 'isMembershipSame' => true));
} elseif ($id1 != $id2) { // if posting user and myuserid are not a member of the same user process the following
echo json_encode(array('id1' => $id1, 'isMembershipSame' => false));
}
}
}