将HTML表格行转换为PHP数组并将其保存到数据库?

问题描述:

我试图将一个html表格行保存到php数组中,然后将数组保存到数据库中。将HTML表格行转换为PHP数组并将其保存到数据库?

<form action="" method="post"> 
     <table class="widefat" id="theTable"> 
         <thead> 
           <tr> 
            <th>Level Identifier</th> 
            <th>Non-logged in message</th> 
            <th>Logged in message</th> 
           </tr> 
         </thead> 
         <tbody> 

            <tr> 
            <td><input type="text" value="" style="height: 19px;background-color: white;font-size:10px;"/></td> 
            <td><textarea style="font-size:10px;" name="promo_msg_free" cols="43" rows="1">This is your custom message template</textarea></td> 
            <td><textarea style="font-size:10px;" name="promo_msg_free" cols="43" rows="1">This is your custom message template</textarea></td> 
            </tr> 

            <tr> 
            <td><input type="text" value="" style="height: 19px;background-color: white;font-size:10px;"/></td> 
            <td><textarea style="font-size:10px;" name="promo_msg_free" cols="43" rows="1"></textarea></td> 
            <td><textarea style="font-size:10px;" name="promo_msg_free" cols="43" rows="1"></textarea></td> 
            </tr> 

         </tbody>  
        </table> 
       </form> 

我怎么能检索每行数据并将其保存到数组元素,最后我会保存数组分贝?谢谢

+1

你尝试过什么到目前为止做samething?任何尝试解析HTML?任何尝试写入数据库?什么样的数据库?你喜欢/可以使用的任何图书馆? – Ryan 2013-03-19 18:18:07

+0

你的问题不清楚。您想要保存每个表格行的值,还是只保存输入和文本区域中的值? – SteveP 2013-03-19 18:19:41

+0

为什么你会首先使用表格布局表单元素?使用div/css – GGio 2013-03-19 18:33:19

如果你想节省每一行HTML DO:

使用jQuery。

var rowsArray = {}; 
var i = 0; 
$('#theTable tr').each(function({ 
    rowsArray[i] = $(this).html(); // if you want to save the htmls of each row 
    i++; 
}); 

然后用AJAX发布此数据

$.ajax({ 
    type: 'post', 
    url: URL_TO_UR_SCRIPT, 
    data: { myarray : rowsArray }, 
    success: function(result) { 
    //ur success handler OPTIONAL 
    } 
}); 

在PHP端你做:

$array = isset($_POST['myarray']) ? $_POST['myarray'] : false; 
if ($array) { 
    $array = serialize($array); 
    //UPDATE YOUR DATABASE WITH THIS SERIALIZED ARRAY 
} 

你不能保存PHP数组到数据库中,因此,你需要序列化和当您从数据库中检索时使用反序列化()

如果你的意思是你想保存输入和文本区域值,那么你需要为每个元素设置 名称,然后使用$ _POST在脚本中访问它们。

$array = array; 
foreach($_POST as $key => $value) { 
    //sanitize your input here 
    $array[$key] = $value; 
} 
$serialized = serialize($array); 
//save serialized array in your DB 

注意/提示:仅供参考不使用HTML表布局表单元素。表格应该用于数据表示。你可以很容易地使用的divCSS

+0

仅供参考,行是动态的。请看看这个截屏视频,你会得到我想要完成的.http://screencast.com/t/0T3I9jglD4K – 2013-03-19 18:38:46

+0

无所谓他们是动态的或不是你可以使用div/css使其动态化。应该使用表格来显示统计数据,而不是很好地处理。阅读此:http://stackoverflow.com/questions/83073/why-not-use-tables-for-layout-in-html – GGio 2013-03-19 18:55:05

这是基本的PHP使用情况。您提供输入名称,当您提交表单时,您提交的页面中的脚本将完成这项工作。

你的价值观将驻留在

$_POST 

阵列英寸所以,你通过

$_POST['input_name'] 

访问他们,你将不得不通过调用它的名字要经过的每个值,然后把它放到数据库相应。

+0

问题是,行是动态的。我不确定会有多少行。所以我无法命名它们。 – 2013-03-19 18:33:54

+0

然后你可以做jQuery,正如我在我的答案中所提到的,只是稍微改变一下。 $( '输入'),每个({})。 $( 'textarea的')每个({})。 etc ... – GGio 2013-03-19 18:36:32