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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站