dongpao5127 2017-04-07 13:58
浏览 37

从POST填写html php表中的数据

I want to fill data in the table. I get the values from Sessions and the POST.

There is the method where I am getting the values I want to put in the table.

if ( isset($_POST) ) {

    //$valuee[] = array();  
    foreach ( $_POST as $key => $value )
        //$valuee[] = str_split($value, 3); 
        var_dump($value);
        //extract($valuee);
    { 
        $cr = explode( "&",$key ); // ◄■■ SPLIT : [0]="CORP_RESP",[1]="1".
    }

    mysqli_query( $con,"insert into respostas 
                            (menuid,corp_resp,id,data,
                            id_avaliacao) " . 
                        "values ('{$cr[1]}','$value', '{$cr[2]}', 
                                 '{$cr[5]}', '{$cr[3]}')" );
}

That is the output I want to put in the table from var_dump($value).

string(4) "0.00" string(4) "0.00" string(4) "0.45" string(4) "0.00" string(4) "0.00" string(4) "0.50" string(4) "0.65" string(4) "0.75" string(4) "0.00" string(4) "0.00" string(4) "0.00" string(4) "0.60" string(4) "0.65" string(4) "0.00" string(4) "0.00" string(4) "0.60"

There is code where is building the table. That table is dynamic according what is select before the POST. The code need to handle with the number of columns and lines.

$s2 = $_SESSION['categorias'];
$utilizador = $_SESSION['utilizador'];
$uCount = $_SESSION['cparametro']; 
$carCount = $_SESSION['ncategoria'];

$table ="<center><table><tr><center><td>Parametro</td></center>";

for($x = 0; $x < $carCount; $x++){
    $val= htmlspecialchars($s2[$x], ENT_HTML5 | ENT_COMPAT, 'UTF-8');
    $table= $table."<td>".$val."</td>";
}

$table = $table."</tr>";
for($i = 0 ; $i <$uCount ; $i++ ){
    $table = $table."<tr><td>".$utilizador[$i]."</td>";

    for($j=0;$j<$carCount;$j++){

        $val= htmlspecialchars($s2[$j], ENT_HTML5 | ENT_COMPAT, 'UTF-8');
        $table = $table."<td>";
        $table = $table.$value;
        $table = $table."</td>";
    }    
}


$table = $table."</tr>";
$teste = "Resultado";
$table= $table."<center><td>$teste</td></center>";

$table = $table."</table>";
echo $table;

Where is $table = $table.$value; need to pass the values correct all is before in the fills and not passing only the last one? The rest of the table is correct.

Like you see in the picture what is the output:

Before the POST:

enter image description here

After the POST:

enter image description here

New output:

enter image description here

  • 写回答

1条回答 默认 最新

  • ds08541 2017-04-07 18:16
    关注

    This seems awfully complicated, which results in not being able to identify the problem, but In short; the problem is you are using:

    $table = $table.$value;
    

    but $value is not the correct variable, you should use:

    $table = $table.$val;
    

    Longer idea: If you would like to improve your code you could use this notation:

    $table.= "append to the var"
    

    instead of:

    $table = $table . "append to var"
    

    It makes it a lot more readable, but I think you could simplify it even more..

    why do you have $carcount and $s2?

    I think $carcount will tell you how many categories are in $s2, but you could just use count($s2) instead, this will reduce the amount of code, also for $uCount and $utilizador

    This will do the same, and starts to get more readable, I'm sure it can be simlified even more, but this is what I could do in a few minutes:

    $s2 = $_SESSION['categorias'];
    $utilizador = $_SESSION['utilizador'];
    
    
    $table ="<table><tr><center><td>Parametro</td>";
    
    foreach($s2 as $cat){
        $val= htmlspecialchars($s2[$cat], ENT_HTML5 | ENT_COMPAT, 'UTF-8');
        $table.= "<td>".$val."</td>";
    }
    
    $table .= "</tr>";
    foreach($utilizador as $util){
        $table.= "<tr><td>".$util."</td>";
    
        foreach($s2 as $cat){
    
            $val= htmlspecialchars($cat), ENT_HTML5 | ENT_COMPAT, 'UTF-8');
            $table.="<td>$val</td>";
    
        }    
    }
    
    $table.= "</tr>";
    $table.= "<td>Resultado</td>";
    
    $table.="</table>";
    echo $table;
    

    It would be even nicer if you would put this in a function, and call that function from where you need the table.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算