2016-12-29 66 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键名称。

相关问题