duandu1966 2012-06-22 06:57
浏览 35
已采纳

在php中处理多个文本框

I have a dynamic rows coming from the database. each row has an order field. I want to make the user able to change the order by entering the order in text boxes show's next to each row, and when the user click save each row will be updated with the new order.

My problem is when I wrote this code:

    <table>
    <?
    foreach($results as $p)
    {
        echo '
            <tr>
            <td>'.$p['page_title'].'</td>
            <td><input type="text" size="2" id="'.$p['page_id'].'" name="s[]" value="'.$p['menu_order'].'"></td>
        </tr>';
    }
    ?>
    <tr><td colspan="2"><input type="submit" value="Save Order"></td></tr>
    </table>

as you can see that every dynamic text box would give the the new order after posting the form, but the Question is how I can know this order to which row ?

the image would explain more. After I clicked Save the s array would be like this:

Array ( [0] => 2 1 => 3 [2] => 1 )

However, I cannot know the page that I want to update !! enter image description here

  • 写回答

1条回答 默认 最新

  • dtp19819 2012-06-22 07:02
    关注

    I think what you are asking is how to order the fields by the number assigned to it. If that's what you are looking to do and if you are using MySQL to store this data you can just retreive it in that order. For example:

    SELECT * FROM mytable ORDER BY menu_order ASC
    

    Edit: Now that I have re-read your question you also might be asking how to assign that order to each row. I noticed that your fields are named s[] You could name them $p['page_id'] and update them accordingly.

     <?
        foreach($results as $p)
        {
            echo '
                <tr>
                <td>'.$p['page_title'].'</td>
                <td><input type="text" size="2" id="'.$p['page_id'].'" name="s['.$p['page_id'].']" value="'.$p['menu_order'].'"></td>
            </tr>';
        }
        ?>
    

    And then

    foreach($_POST['s'] as $key => $value) {
        // Update each $key with $value
        // Where $key is the ID and $value is the order
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)