2016-06-22 48 views
-1

我有50個變量在PHP中。我想檢查它們中的每一個,如果它們是真的,則在稱爲$point的變量中添加2個點。我是新的,所以我寫了幾行,但我認爲我做錯了方式。我該如何做到這一點PHP如果聲明是正確的?

$strenght_point = 0; 

if($f_name){$strenght_point++;} 
if($l_name){$strenght_point + 2;} 
if($full_name){$strenght_point + 2;}  

我怎麼能這樣做的權利way.Thanks

更新我的全部功能,在這裏... 它笨控制器功能 希望你們現在明白了很好

功能strength_scale(){

 $user_id = $this->uri->segment(2); 
     $user_name = $this->uri->segment(3); 


      $query = $this->db->get_where('aoa_user', array('id' => $user_id, 'username' => $user_name));  
      foreach ($query->result() as $row){ 

       $f_name = $row->f_name;    
       $l_name = $row->l_name;    
       $full_name = $row->full_name;    
       $username = $row->username;    
       $alias_name = $row->alias_name;    
       $gender = $row->gender;    
       $country = $row->country;    
       $avatar = $row->avatar;    
       $cover_photo = $row->cover_photo; 
       $email = $row->email; 
       $skill = $row->skill; 
       $other_skills = $row->other_skills; 
       $ex_time = $row->ex_time; 
       $about = $row->about; 
       $company = $row->company; 
       $company_position = $row->company_position; 
       $phone = $row->phone; 
       $facebook = $row->facebook; 
       $facebook_page = $row->facebook_page; 
       $google_plus = $row->google_plus; 
       $twitter = $row->twitter; 
       $youtube = $row->youtube; 
       $skype = $row->skype; 
       $linkedin = $row->linkedin;      
       $website = $row->website; 
       $latitude = $row->latitude; 
       $longitude = $row->longitude; 
       $verification = $row->verification; 

      } 

     $strength_point = 0; 

     if($f_name){$strength_point++;} 
     if($l_name){$strength_point + 2;} 
     if($full_name){$strength_point + 2;}  
} 
+3

*「我有50個變量」*然後你做錯了什麼。你可能想要使用一個數組。您還需要將新值分配回您的點變量。 – Rizier123

+1

你爲什麼認爲你做錯了?你是否遇到某種形式的錯誤? – castis

+1

你拼寫錯誤。除此之外,你的代碼是正確的。 – durbnpoisn

回答

2

改爲使用變量創建一個數組。

https://3v4l.org/FYTtG

$arr = array("f_name" => true, "l_name" => true, "full_name" => true); 

$strength=0; 

Foreach($arr as $var){ 
    if($var) $strength = $strength+2; 
} 

Echo $strength; 
+1

你可以這樣做:'$ strength + = 2;'.... upvote努力回答這個問題。 –

+0

真的,沒想到它 – Andreas

1

正如Rizier123說,你需要增加你的力量變量正確地將。 你可以寫一個簡單的函數,會接受你的50個變量中的一個,並返回強度增量:

function defineStrength($param) 
{ 
    if ($param) { 
     return 2; 
    } 
    return 0; 
} 

$strength = 0; 

$f_name = true; 
$l_name = false; 
$full_name = false; 

$strength += defineStrength($f_name); 
$strength += defineStrength($l_name); 
$strength += defineStrength($full_name); 

然而,陣列會是一個更好的方式去,如安德烈亞斯mentionned。

在你的問題更新中,你說你使用CodeIgniter。 As the documentation states您可以將查詢結果作爲純數組返回。 所以你可以進一步開發這樣的:

function defineStrengthFromArray(array $row) 
{ 
    $strength = 0; 
    foreach ($row as $param) { 
     $strength += defineStrength($param); 
    } 
    return $strength; 
} 

foreach ($query->result_array() as $row){ 

    $strength = defineStrengthFromArray($row); 

} 
+0

好的!不是小巧緊湊的,但它是一種很好的方式。 +1從我 – Andreas

+0

@Andreas謝謝)+1爲您的答案 – postrel

+0

PLZ再次檢查我已更新 –

相關問題