2016-12-29 65 views
-2

我在數據庫中有不同的字段,並且im讀取它們的值。
現在我想要做的是如果某個字段存在於數據庫中,則將其分號。總結所有字段,如果從數據庫中存在

這裏是我的代碼:

<?php 

$percent_profile_image = 40; 
$percent_cover_image = 20; 
$percent_profiletext = 10; 
$percent_travelintext = 10; 
$percent_sparetimetext = 10; 
$percent_gouttext = 10; 

$avatar_status_num; 
$cover_status_num; 
$profiletext = $display_profile['profile_text_approved']; 
$sparetimetext = $display_profile['spare_time_text_approved']; 
$travelintext = $display_profile['traveling_text_approved']; 
$gouttext = $display_profile['go_out_text_approved']; 

if($avatar_status_num == 2) { echo $percent_profile_image; } + if($avatar_status_num == 2) { echo $percent_profile_image; } 
?> 

現在我知道我如果代碼是錯誤的。我想要做什麼,如果例如$ avatar_status_num = 2我想打印出40.如果$ cover_status_num = 2我想減去這些數字,所以。 40 + 20。所以它應該只打印出數字並且如果來自DB的值是nr2就減去它。

我希望你明白我的問題:) Cheerz

+0

如果您的代碼在最後一行顯示正確的語法,這將有所幫助。你想添加一些東西嗎?我可能猜測正確,但我不想發表猜測作爲答案。 – Philipp

回答

0

使用一個額外的變量來總結自己的價值觀。

$sum = 0; 
if (isset($someValue) && $someValue == 2) { 
    $sum += 40; 
} 

if (isset($someOtherValue) && $someOtherValue == 2) { 
    $sum += 20; 
} 

或者,如果你想這樣做動態的代碼:

$percent = array(
    'profile_image' => 40, 
    'cover_image' => 20, 
    'profile_text_approved' => 10, 
    'traveling_text_approved' => 10, 
    'spare_time_text_approved' => 10, 
    'go_out_text_approved' => 10, 
); 

$sum = 0; 
foreach ($display_profile as $key => $value) { 
    if (array_key_exists($key, $percent) && $value == 2) { 
     $sum += $percent[$key]; 
    } 
} 

注意在這種情況下,$percent鍵名應該是一樣的$display_profile鍵名稱。

相關問題