PHP:根据ID或行日期在行中保存多个输入字段 [英] PHP: Saving multiple input fields in rows based on ID or date of row

查看:116
本文介绍了PHP:根据ID或行日期在行中保存多个输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定该标题的描述性如何,但是:

I'm not entirely certain how descriptive that title is, but:

在Wordpress中,我有一个自定义的数字数据表,该表每月添加一次(通过电子表格导入).因此,我使用$wpdb提取该表的内容,该表以stdClass对象数组的形式返回:

In Wordpress I have a custom table of numerical data which gets added to monthly (via a spreadsheet import). As such, I'm using $wpdb to pull the contents of that table, which gets returned as an array of stdClass objects:

Array
(
    [0] => stdClass Object
        (
            [id] => 553
            [assocID] => 1
            [title] => Afghanistan
            [weight_political] => 8.2
            [indicator_political] => 0.0
            [weight_security] => 9.5
            [indicator_security] => 0.0
            [weight_criminal] => 9.5
            [indicator_criminal] => 1.0
            [weight_governance] => 9.0
            [indicator_governance] => 0.0
            [overall_score] => 9.2
            [datamonth] => 2013-08-18
            [active] => 1
        )

    [1] => stdClass Object
        (
            [id] => 369
            [assocID] => 1
            [title] => Afghanistan
            [weight_political] => 8.4
            [indicator_political] => 0.0
            [weight_security] => 9.5
            [indicator_security] => 0.0
            [weight_criminal] => 9.3
            [indicator_criminal] => 1.0
            [weight_governance] => 9.0
            [indicator_governance] => 0.0
            [overall_score] => 9.2
            [datamonth] => 2013-07-05
            [active] => 1
        )

)

这很好,可以提供所需的确切信息,然后可以将其作为一系列输入字段进行编辑(请参见我的

This is fine and gives me exactly what I want, which I can then echo out as a series of input fields to edit (see my jsFiddle for example output).

但是,我无法解决的问题(而且我很确定自己只是在这里非常愚蠢,缺少明显的东西)正在将适当的数据保存回 到适当的数据库行.

However, what I can't get my head around (and I'm pretty sure I'm just being super-stupid here and missing something obvious) is saving the appropriate data back to the appropriate database row.

我曾考虑将输入名称设置为数组(即<input name="weight_security[]"...),但显然缺少参考.我希望将2013年8月的数据(编号 553 )保存到2013年8月的行中;我只是不知道在哪里或如何实现该引用.

I've considered setting the input names as arrays (i.e. <input name="weight_security[]"...), but it's clearly missing a reference. I want the August 2013 data (id 553) to be saved to the August 2013 row; I just can't figure out where or how to implement that reference.

在此示例中,只有2行,但是一旦上线,则可能多达14行,并将其添加到每月,因此关键是能够同时修改多行.

In this example there's only 2 rows, but there could be as many as 14 once this is live, and will be added to monthly, so the ability to modify multiple rows simultaneously is key.

非常感谢您的帮助.

推荐答案

回答我自己的问题:输入名称位于多维数组中;因此,而不是像这样的多个字段:

To answer my own question: the key was in multidimensional arrays for the input names; so, instead of multiple fields like:

<input name="weight_political" value="8.2" />
<input name="indicator_political" value="0" />

我决定一个级别,并将这些字段名称包装在名为geodata的数组中,并且至关重要的是,以一个数组键作为需要更新的行的ID开头,因此:

I decided to go up a level and wrap these field names in an array called geodata and, crucially, preface that with an array key that was the ID of the row that needed updating, thus:

<input name="geodata[553][weight_political]" value="8.2" />
<input name="geodata[553][indicator_political]" value="0" />

然后,当解析发布的数据时,我可以遍历geodata数组以获取行ID(作为键),然后将数据作为其中的关联数组:

Then, when parsing the posted data, I can loop through the geodata array to get the row ID (as the key), then the data as an associative array within that:

<?php
if ($_POST["geodata"]) {

        foreach ($_POST["geodata"] as $rowID=>$valuesArray) {

            $queryArray = array();

            foreach ($valuesArray as $key=>$value) {
                $queryArray[] = $key . ' = '  . $value;
            }

            $queryString = implode(', ', $queryArray);

            $wpdb->query("UPDATE $table_name SET $queryString WHERE id = $rowID");

        }
    }
?>

也:

为了尽可能地提高效率(因为初始的9x14网格将返回126个值进行更新,其中许多都是不必要的),所以我写了一个小jQuery函数来检测何时更改了输入字段.如果是这样,则将其namedata[id][identifier]更改为geodata[id][identifier]-这样,$_POST['geodata']将仅发送需要更新的值,而不是整个批次.下面的jsFiddle,供有兴趣的人使用.

To try to be as efficient as possible (since an initial 9x14 grid would return 126 values to update, many of them needlessly), I wrote a small jQuery function to detect when an input field was changed; if so, then its name was changed from data[id][identifier] to geodata[id][identifier] - that way, $_POST['geodata'] would only send the values that needed to be updated, rather than the whole batch lot. jsFiddle below, for those who are interested.

http://jsfiddle.net/hkVTn/1/

这篇关于PHP:根据ID或行日期在行中保存多个输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆