dongtiaobeng7901 2017-11-27 17:11
浏览 44
已采纳

Php(使用数组的表)

How would I make a table that takes a PHP array. The array is decided by a html input. The table has the actual values in the first column, length in the second column, and if it starts with 3 letters, 3 numbers or neither in the third column.

echo '<table>';
    foreach ($array as $key => $value) {
        echo '<tr><td>';
        echo htmlspecialchars($array);
        echo '</td><td>';
        echo strlen($array);
        echo '</td><td>';

            //to work on
            $trim = substr($array, 0, 3);
                if(is_numeric($trim)){
                    echo 'numeric';
                }elseif(is_string($trim)){
                    echo 'string';
                }else{
                    echo 'else ?';
                }

        echo '</td></tr>';
        }
    echo '</table>';
  • 写回答

1条回答 默认 最新

  • duanli9930 2017-11-27 18:02
    关注

    PHP will interpret POST values with brackets in their name as arrays.

    <form method="POST">
        <input type="text" name="values[]" >
        <input type="text" name="values[]" >
        <input type="text" name="values[]" >
        <button type="submit">Go</button>
    </form>
    

    Now... The solution presented here is only to get you started. The third column can be a bit tricky because a value of 'ab123c' will pass as string.

    I will let you work the kinks the way you want them to be handled.

    if(is_array($_POST['values'])){
        echo '<table>';
            foreach ($_POST['values'] as $key => $value) {
                echo '<tr><td>';
                echo htmlspecialchars($value);
                echo '</td><td>';
                echo strlen($value);
                echo '</td><td>';
    
                    //to work on
                    $trim = substr($value, 0, 3);
                        if(is_numeric($trim)){
                            echo 'numeric';
                        }elseif(is_string($trim)){
                            echo 'string';
                        }else{
                            echo 'else ?';
                        }
    
                echo '</td></tr>';
                }
            echo '</table>';
    }
    

    PS: Never trust user input ;)

    I know the code is not squeaky clean, the goal was for it to be easy to understand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算