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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题