多个复选框和PHP的问题

时间:2016-11-20 09:16:00

标签: php html

Hair Color:30 

如果我同时检查两个复选框,那么我只得到单个结果,如

Hair Cut:20
Hair Color:30

但期望结果都像

{{1}}

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

你只是回响一次! 而且您可能希望优化if循环,如下所示:

# your input dataframe
d <- structure(list(SellerID = c(1, 7, 4, 3, 1, 7, 4, 2, 5, 1, 2, 
5, 7), Period = c(1, 1, 1, 2, 2, 3, 3, 5, 5, 9, 9, 10, 10)), .Names = c("SellerID", 
"Period"), row.names = c(NA, -13L), class = "data.frame")

# your wanted output
o <- structure(list(SellerID = c(1, 7, 4, 3, 1, 7, 4, 2, 5, 1, 2, 
5, 7), Period = c(1, 1, 1, 2, 2, 3, 3, 5, 5, 9, 9, 10, 10), Inactive = c(0, 
0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0)), .Names = c("SellerID", 
"Period", "Inactive"), row.names = c(NA, -13L), class = "data.frame")

# 6 steps solution, step by step using vanilla R
# step1. - add tmp key for final sorting
d$tmp.key <- seq_len(nrow(d))
# step 2. - split by individual seller id
d.tmp <- split(d,f=d$SellerID)
# step 3. - add inactive column to individual sellers
d.tmp <- lapply(d.tmp,
    function(x){
       # Below as.numeric is optional
       # it may stay logical as well.
       # Also sorting by Period (not used here)
       # should be done (I am asuming it is sorted.)
       x$Inactive <- as.numeric(c(diff(x$Period) >= 6,FALSE))
       x
       })
# step 4. - assemble again individual sellers back into one data.frame
d <- do.call(rbind,d.tmp)
# step 5. - sort to original order using temp.key
d <- d[order(d$tmp.key),c("SellerID","Period","Inactive")]
# step 6. - rename rows according the row order
rownames(d) <- NULL

# here I am just comparing with your wanted ideal
> identical(d,o)    
[1] TRUE

答案 1 :(得分:0)

您可以尝试沿着这些方向尝试使用预定义的选项数组以及相关值,并测试提交数据中是否存在项目以显示项目和价格。

if( isset( $_POST['submit'], $_POST['item'] ) && !empty( $_POST['item'] ) ){

    $matrix=array(
        'haircut'   =>  20,
        'haircolor' =>  30,
        'beardtrim' =>  5,
        'shave'     =>  10
    );
    $total=0;

    foreach( $_POST['item'] as $item ){
        $key = strtolower( $item );
        if( array_key_exists( $key, $matrix ) ){
            echo "$item: {$matrix[ $key ]}<br />";
            $total += floatval( $matrix[ $key ] );
        }
    }
    echo "<br /><br />Total: {$total}";


} else {
    if( empty( $_POST['item'] ) ){
        echo 'Please select at least one option';
    }
}
相关问题