2013-05-26 32 views
0

我有一個問題清單的國家和我解釋不了,, 第一,這是我的函數奇怪的使用功能,從TXT

function list_countries($id,$name=null,$result=null){ 
    $countries = 'countries.txt'; 
    $selected = ''; 
    echo '<select name="'.$name.'" id="'.$id.'">'; 
    echo '<option disabled>طالب الغد</option>'; 
    if(file_exists($countries)){ 
     if(is_readable($countries)){ 
      $files = file_get_contents($countries); 
      $files = explode('|',$files); 
      foreach($files AS $file){ 
       $value = sql_safe($file); 
       if(strlen($value) < 6){ 
        echo '<option disabled>'.$value.'</option>'; 
       }else{ 
        if($value == $result){ 
         $selected = ' selected="selected" '; 
        } 
        echo '<option value="'.$value.'".$selected.'>'.$value.'</option>'; 
       } 
      } 
     }else{ 
      echo 'The file is nor readable !'; 
     } 
    }else{ 
     echo "The file is not exist !"; 
    } 
    echo '</select>'; 
} 

現在解釋 我有一個文本文件,包括國家名稱用「|」分隔 在這個文件中,有國家之前標題,,我的意思是這樣

U|United Kingdom|United State|UAE etc .. 
L|Liberia|Libya etc .. 

現在什麼功能不要被禁用,標題,它總是一個字.. 但strlen函數的最小數目它是給我是5不是一個..「這是第一個問題 第二個在$結果永遠不等於$值和乙醚我不知道爲什麼??

+0

你說這些國家是用|隔開的,但你提供的例子每行有一個國家。這是什麼? – LSerni

+0

EDITED ,,,現在看 –

回答

1

你需要分裂兩次文件,一個爲行,一個爲國家。

此外,既然你的「國家嘗試標題「始終是每行的第一項,您不需要使用strlen進行檢查。只需移出每一行的第一項:一個是標題,下面是國家。

就是這樣。

注意,在你的代碼存在輸出值的echo一個語法錯誤,>符號實際上是引號。

function list_countries($id,$name=null,$result=null){ 
    $countries = 'countries.txt'; 
    $selected = ''; 
    $text = '<select name="'.$name.'" id="'.$id.'">'; 
    $text .= '<option disabled>ﻁﺎﻠﺑ ﺎﻠﻏﺩ</option>'; 
    if(file_exists($countries)){ 
     if(is_readable($countries)){ 
      $list = file($countries); 
      foreach($list as $item){ 
       $item = trim($item); 
       $opts = explode('|', $item); 
       // The first item is the header. 
       $text .= "<option disabled>$opts[0]</option>"; 
       array_shift($opts); 
       foreach($opts as $opt) 
       { 
         $value = sql_safe($opt); 
         $text .= '<option'; 
         if($value == $result) 
           $text .= ' selected="selected"'; 
         $text .= ' value="'.$value.'"'; 
         $text .= '>'.$value."</option>\n"; 
       } 
      } 
     }else{ 
      $text .= "The file is not readable!"; 
     } 
    }else{ 
     $text .= "The file does not exist!"; 
    } 
    $text .= '</select>'; 
    return $text; 
} 

我稍微修改代碼,以便在函數實際上返回文本輸出,而不是它呼應;這使得更多的可重用性。爲了使上述函數表現得像你一樣,只是

echo $text; 
} 

更換return,你是好。

+0

謝謝,, 我解決了這個問題,通過改變這個 if($ value == $ result){ echo''; 繼續; } echo''; 謝謝 –