1

$id1と$id2が未定義であるというエラーが表示されます。私はそれらに正しくアクセスしていませんか?そうでない場合、どうすれば正しくアクセスできますか?

$query = $this->db->query("SELECT * FROM churchMembers");
$row = $query->row();
    if ($query->num_rows() != 0) {
      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('loggedIn' => true, '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('loggedIn' => true, 'isMembershipSame' => false));
      }
    }
4

1 に答える 1

3

どちらも定義しないか$id1$id2対応するif条件が満たされない限り、前述の条件のいずれかがfalseで実行されない場合、でそれらを比較しようとすると、どちらの変数も存在しませんif ($id1 == $id2)

ifチェーンに入る前に、それらを空の文字列に初期化する必要があります。次に、それらを比較するときに、それらが空でないことも確認します。

// ADDENDUM after comments:
// If you put this into a loop to fetch rows,
// the following must be INSIDE the loop to reinitialize
// the two vars on each iteration.

// Initialize them as empty strings
$id1 = "";
$id2 = "";

// If you are in a loop, you should check num_rows() once outside the loop, rather than inside
if ($query->num_rows() != 0) {
  if ($postingUserId == $row->cMuserId) {
    $id1 = $row->cMchurchId;
  } 
  if ($userid == $row->cMuserId) {
    $id2 = $row->cMchurchId;
  } 
  // Test if they are non-empty (conditions matched above) and equal:
  if (!empty($id1) && !empty($id2) && $id1 == $id2) {
    echo json_encode(array('loggedIn' => true, 'isMembershipSame' => true));
  } 
  // No need for else if here, just a plain else clause
  else {
    echo json_encode(array('loggedIn' => true, 'isMembershipSame' => false));
  }
}
于 2012-08-19T02:56:15.697 に答える