2012-10-12 74 views
0

我有一個動態形成的多個div。在每個div裏面我都有一個複選框和一個下拉列表。複選框列表包含酒店名稱和類別以及包含酒店評級的單個下拉菜單。用戶可以選擇複選框或從下拉列表中選擇一個選項。默認情況下是複選框。如果用戶沒有選擇複選框,則應考慮特定城市的下拉列表的值。顯示來自帖子的數組php

這是我在控制器中試過的代碼,但我沒有得到想要的結果。有人可以幫我解決這個問題嗎?

 if (isset($_POST['city'])) { 
       $city = $_POST['city']; 
       if (is_array($city)) { 
        foreach ($this->input->post('city') as $city) { 
         $cityid[] = $city; 
         // $category.= $this->input->post('category_' . $city) . ","; 
         // $categorycity.= $city . ","; 
         // $prefsightseeing.= $this->input->post('prefsight_' . $city) . ","; 
         // $prefsightseeingcity.= $city . ","; 
         //$hotel = $_POST['hotel']; 

         if (isset($_POST['hotel'])) { 
          $hotel = $_POST['hotel']; 

         // if (is_array($hotel) && in_array($city, $hotel)) { 
           foreach ($this->input->post('hotel') as $city_id) { 
            $arr = explode('_', $city_id); 
            if (in_array($arr[1], $cityid)) { 
             $hotelcityid.= $arr[1] . ','; 
             $hotelid.= $arr[2] . ','; 
            } 

          //  } 
          } 
         } 

         else{  if (isset($_POST['category'])) { 
           $category = $_POST['category']; 
           foreach ($this->input->post('category') as $category) { 
            $arr = explode('_', $category); 
            // echo $arr[1]; 
            // print_r($cityid); 
            if (in_array($arr[1], $cityid)) { 
             $hotelcategory.= $arr[0] . ','; 
             $categorycity.= $city . ','; 
             } 
           } 
          } 
         } 
         // $hotelid.= $arr[2] . ','; 
        } 
        $categorycity = rtrim($categorycity, ','); 
        $category = rtrim($hotelcategory, ','); 
        $category= implode(',', array_keys(array_flip(explode(',', $category)))); 
$categorycity= implode(',', array_keys(array_flip(explode(',', $categorycity)))); 

        // $prefsightseeing = rtrim($prefsightseeing, ','); 
        // $prefsightseeingcity = rtrim($prefsightseeingcity, ','); 
       } 
      } 
      if ($category != "") { 

       $arrData['HotelInfoByCategory'] = $this->previewpackage_model->get_hotel_info_by_category($category, $categorycity); 
      } 

感謝,

+0

什麼是確切的問題? –

+0

你想要的結果是什麼? –

+0

只要提到如果您使用複選框的不同名稱和下拉框表示您可以通過選中複選框的空值幷包含下拉框的值來獲得結果 – Kalai

回答

0

根據您的問題「顯示,從後PHP數組」我只想說

echo '<pre>'; 
print_r($_POST); 
echo '</pre>'; 

它會幫助您檢查您發佈的數據

+0

我確實收到了數據。我的問題是以符合我上面提到的條件的格式顯示它 – asifa

0

我想你錯過了空變量的條件。嘗試這樣的事情

if (isset($_POST['city']) && !empty($_POST['city'])) 

只是不要對別人:)

2
的複選框輸入標籤

同樣保持名稱定爲「城市[]」,而不僅僅是「城市」,然後在你的控制器作爲檢索$ this-> input-> post ['city []'],它將返回檢查值的數組。否則你只使用「城市」,那麼只有上次選中的值會出現在這裏。所以我會說嘗試使用「城市[]」,希望它可以幫助

相關問題