将HTML表格行转换为PHP数组并将其保存到数据库? [英] Convert HTML table rows into PHP array and save it to database?

查看:116
本文介绍了将HTML表格行转换为PHP数组并将其保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个html表格行保存到php数组中,然后将数组保存到数据库中。

 < form action =method =post> 
< table class =widefatid =theTable>
< thead>
< tr>
等级标识符< / th>
< th>未登入讯息< / th>
< th>登录信息< / th>
< / tr>
< / thead>
< tbody>

< tr>
< td>< input type =textvalue =style =height:19px; background-color:white; font-size:10px;/>< / td>
< td>< textarea style =font-size:10px; name =promo_msg_freecols =43rows =1>这是您的自订讯息模板< / textarea>< / td>
< td>< textarea style =font-size:10px; name =promo_msg_freecols =43rows =1>这是您的自订讯息模板< / textarea>< / td>
< / tr>

< tr>
< td>< input type =textvalue =style =height:19px; background-color:white; font-size:10px;/>< / td>
< td>< textarea style =font-size:10px; name =promo_msg_freecols =43rows =1>< / textarea>< / td>
< td>< textarea style =font-size:10px; name =promo_msg_freecols =43rows =1>< / textarea>< / td>
< / tr>

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

如何检索每行数据并将其保存到数组元素,最后我将保存数组到db?谢谢

解决方案

如果您想保存每行的HTML:
$ b

使用JQuery。

  var rowsArray = {}; 
var i = 0;
$('#theTable tr')。each(function({
rowsArray [i] = $(this).html(); //如果你想保存每行的htmls
i ++;
});

然后使用 ajax 发布此数据

  $。ajax({
类型:'发布',
网址:URL_TO_UR_SCRIPT ,
data:{myarray:rowsArray},
success:function(result){
// ur成功处理程序可选
}
});

在PHP方面,您可以:

 < code = $ array = isset($ _ POST ['myarray'])?$ _POST ['myarray']:false; 
if($ array){
$ array = serialize($ array );
//用这个序列化数组更新数据库
}

你无法将php数组保存到数据库中,因此您需要将其序列化并在数据库中检索时使用反序列化()

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

  $ array = array; 
foreach($ _ POST为$ key => $ value){
//在这里清理您的输入
$ array [$ key] = $ value;
}
$ serialized = serialize($ array);
//将序列化数组保存在数据库中

注意/提示: strong> FYI不使用html表格来布置表单元素。表格应该用于数据表示。您可以使用 div css $ b轻松完成相同的操作

I'm trying to save a html table rows into php array and then save the array in database.

<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>

How can i retrieve each row data and save it to array element and finally I would be saving the array to db? Thanks

解决方案

IF YOU WANT TO SAVE HTML OF EACH ROW DO:

Use 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++;
});

then use ajax to post this data

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

In PHP side you do:

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

you cant save php array into database therefore you need to serialize it and when you retrieve it from DB use unserialize()

IF you meant that you wanted to save the input and text area values then you need to set the names for each of the element and then access them in your script using $_POST.

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

NOTE/HINT: FYI do not use html table to lay out the form elements. Tables should be used for data representation. You could easily do samething using divs and css

这篇关于将HTML表格行转换为PHP数组并将其保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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