2017-07-01 103 views
1

我每次刷新頁面時都會嘗試執行的操作$insertedBookings應該添加輸入的數量,以使其成爲總數,但仍然保留在6總共有11項被輸入。頁數不會隨着每次刷新而增加

代碼更新:

$insertedBookings = 0; 
    foreach($json->data as $row) 
     { 
     if (!in_array($row->guestEmail, $emails) && date('Y-m-d', strtotime($row->startDate)) == date('Y-m-d')) 
      { 
$insertedBookings ++; 

      $guests[] = array(
       'FirstName' => $row->guestFirstName, 
       'LastName' => $row->guestLastName, 
       'email' => $row->guestEmail, 
       'country' => $row->guestCountry, 
       'check-in_date' => $row->startDate, 
       'check-out_date' => $row->endDate, 
      ); 
      $emails[] = $row->guestEmail; 
      } 
     } 

    // Insert to IF HERE 

    if (!isset($_SESSION["totalInsertedBookings"])) 
     { 
     $_SESSION["totalInsertedBookings"] = $insertedBookings; 
     } 

    $currentInsertedBooking = $_SESSION["totalInsertedBookings"]; 

**Code:** 

    $insertedBookings = 0; 
    foreach($json->data as $row) 
     { 
     if (!in_array($row->guestEmail, $emails) && date('Y-m-d', strtotime($row->startDate)) == date('Y-m-d')) 
      { 
      $guests[] = array(
       'FirstName' => $row->guestFirstName, 
       'LastName' => $row->guestLastName, 
       'email' => $row->guestEmail, 
       'country' => $row->guestCountry, 
       'check-in_date' => $row->startDate, 
       'check-out_date' => $row->endDate, 
      ); 
      $emails[] = $row->guestEmail; 
      $insertedBookings = count($guests); 
      } 
     } 

    // Insert to IF HERE 

    if (!isset($_SESSION["totalInsertedBookings"])) 
     { 
     $_SESSION["totalInsertedBookings"] = $insertedBookings; 
     } 

    $currentInsertedBooking = $_SESSION["totalInsertedBookings"]; 

回答

0

因爲count($客人)的計數$客人陣列爲6(名字,姓氏,等)中的元素的數量。

+0

ahh謝謝,我該怎麼做才能統計有多少物體? –

+0

計算通過循環的次數。 $ counter = 0; //在foreach循環之前執行此操作。 foreach(....){ $ counter ++; } – Difster

+0

謝謝,我已經將它移到$ guest數組之上,但是我仍然得到6 :) –

相關問題