2013-12-10 66 views
-2
//let my controller be C and method be: 
    function X($array){} 
//And my url to call it is: 
    localhost/mysite/C/X/array 

那麼我嘗試過,但它返回400錯誤的請求響應。 有誰知道該怎麼做?快速反應會幫助我很多//感謝名單在笨如何傳遞陣列類型參數到控制器從URL

+1

http://stackoverflow.com/questions/1763508/passing-arrays-as-url-parameter –

回答

2

本地主機/ mysite的/ C/X /?陣列= 1 & &?陣列= 2 ....

$陣列= $這個 - >輸入 - >的get( '陣列' );

本地主機/ mysite的/ C/X/1,2,3,4,5

$array = explode(',' $this->uri->segment(n)); 

// in app/config/config.php 

$config['permitted_uri_chars'] = 'a-z 0-9~%.:,_-'; 

我的變體在URL陣列(/旅遊/ novogodniye-圖裏/簽證=是;持續時間=小;運輸= 4,7,6,2 /)

if (! function_exists('filter_parse_segment')) 
{ 
    function filter_parse_segment($segment, $merge_to_get = FALSE) 
    { 
     if(empty($segment)) 
     { 
      return FALSE; 
     } 

     $parameters = explode(";", (string)$segment); 

     if(empty($parameters)) 
     { 
      return FALSE; 
     } 

     $parameters_array = array(); 

     foreach($parameters as $parameter) 
     { 
      if(empty($parameter)) 
      { 
       continue; 
      } 

      $parameter = explode("=", $parameter); 

      if(! isset($parameter[0], $parameter[1]) or empty($parameter[0])) 
      { 
       continue; 
      } 

      if(strpos($parameter[1], ',')) 
      { 
       $parameter[1] = explode(",", $parameter[1]); 
      } 

      $parameters_array[$parameter[0]] = $parameter[1];  
     } 

     if($merge_to_get === TRUE) 
     { 
      $_GET = array_merge($_GET, $parameters_array); 
     } 

     return $parameters_array; 
    } 
} 

// -------------------------------------------------------------------- 

if (! function_exists('filter_collect_segment')) 
{ 
    function filter_collect_segment($array, $suffix = '', $remove = array()) 
    { 
     if(empty($array) || ! is_array($array)) 
     { 
      return ''; 
     } 

     $segment_str = ''; 

     foreach ($array as $key => $value) 
     { 

      if(empty($key) || in_array($key, (array)$remove)) 
      { 
       continue; 
      } 

      if(! $segment_str == '') 
      { 
       $segment_str = $segment_str.';'; 
      } 

      if(! is_array($value)) 
      { 
       $segment_str = $segment_str.$key.'='.$value; 

       continue; 
      } 

      if(empty($value)) 
      { 
       continue; 
      } 

      $parsed_value = ''; 

      foreach ($value as $item) 
      { 
       if(! $parsed_value == '') 
       { 
        $parsed_value = $parsed_value.','; 
       } 

       if(is_array($item) || empty($item)) 
       { 
        continue; 
       } 

       $parsed_value = $parsed_value.$item; 
      } 

      $segment_str = $segment_str.$key.'='.$parsed_value; 
     } 

     if($segment_str != '') 
     { 
      $segment_str = $segment_str.$suffix; 
     } 

     return $segment_str; 
    } 
} 


// -------------------------------------------------------------------- 

if (! function_exists('filter_key')) 
{ 
    function filter_key($filter_array, $key, $value = NULL) 
    { 
     if(! isset($filter_array[$key])) 
     { 
      return; 
     } 

     if($value == NULL) 
     { 
      return $filter_array[$key]; 
     } 

     if(! is_array($filter_array[$key]) && $filter_array[$key] == (string)$value) 
     { 
      return $value; 
     } 

     if(is_array($filter_array[$key]) && in_array($value, $filter_array[$key])) 
     { 
      return $value; 
     } 

     return FALSE; 
    } 
} 
+0

這可能是一種方法..如果我使它與_(下劃線)的字符串,那麼我可以將其分割爲數組在我的控制器..我會嘗試一下..你明白了。 Thanx很多 – user1891480

+0

下劃線版本 - $ array = explode('_'$ this-> uri-> segment(n)); –

0

試試你的網址這樣

if value you have to pass is 
[1,2,3,2] 
then 
localhost/mysite/index.php/C/X/1,2,3,2 
0

在一個簡單的方法可以使數組字符串數組中的值之間的一些特殊字符。

然後在登錄頁面中,您可以用特殊字符拆分字符串並再次獲取數組。

如果值爲[1,2,3,4],則使用循環「1,2,3,4」。

然後傳遞字符串。

在登陸頁面用「,」拆分字符串,你會再次獲得數組。

希望它可以幫助你。

由於

0

爲什麼不使用uri段數組?你可以計算uri段並可以使用它們。即使你可以檢查已經有多少個陣列。

url.com/1/2/3

http://ellislab.com/codeigniter/user-guide/libraries/uri.html

注:主控制器指數函數URI段不工作,你必須定義比指數,例如其他功能:我不知道它發生了什麼,但我認爲,因爲我使用的htaccess文件刪除index.php。

url.com/uri_segment_controller/go/1/2/3

0

如果你想在相當不錯的URL的解決方案,那麼你必須循環數組,然後再連接具有一些元素 - 破折號或+加上這樣的跡象;

$array = array(1,2,3,4); 
$string = ""; 

foreach($array as $value){ 
    $string .= $value."-"; 
} 

$string = rtrim($string, "-"); 

redirect(base_url()."get_products?param=".$string); 

然後在下一頁獲取參數並使用帶有 - 破折號的explode()函數重新創建數組。

相關問題