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

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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题