使数组表可编辑且可保存

时间:2016-05-21 01:18:00

标签: php arrays csv html-table edit

我有一个CSV文件,我已将其转换为数组并成功将其打印到屏幕上的表格中。

但我想要做的是,有一个按钮使表格可编辑,然后可以更改和更新数据。

我对PHP很新,所以我甚至不知道从哪里开始。

这就是我用来将我的数组转换为表的方法:

//This converts an array to a table
function array2table($array, $recursive = false, $null = ' ')
{
// Sanity check
if (empty($array) || !is_array($array)) {
    return false;
}

if (!isset($array[0]) || !is_array($array[0])) {
    $array = array($array);
}

// Start the table
$table = "<table>\n";

// The header
$table .= "\t<tr>";

// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
    if($heading ==0){
        $heading = "Full Name";
    }else if($heading == 1){
        $heading = "Weight";
    }else if($heading == 2){
        $heading = "Weight of belongings";
    }else if($heading == 3){
        $heading = "Total Weight";
    }else if($heading == 4){
        $heading = "Unassemblium Required";
    }else if($heading == 5){
        $heading = "Reassemblium Required";
    }else if($heading == 6){
        $heading = "Cost of Unassemblium";
    }else if($heading == 7){
        $heading = "Cost of Reassemblium";
    }else if($heading == 8){
        $heading = "Total cost of journey";
    }

   $table .= '<th>' . $heading. '</th>';

}
$table .= "</tr>\n";

// The body
foreach ($array as $row) {
    $table .= "\t<tr>" ;

    foreach ($row as $cell) {

        $table .= '<td>';


        // Cast objects
        if (is_object($cell)) { $cell = (array) $cell; }

        if ($recursive === true && is_array($cell) && !empty($cell)) {
            // Recursive mode
            $table .= "\n" . array2table($cell, true, true) . "\n";
        } else {
            $table .= (strlen($cell) > 0) ?
                htmlspecialchars((string) $cell) :
                $null;
        }

        $table .= '</td>';
    }

    $table .= "</tr>\n";
}

$table .= '</table>';
return $table;
}

我需要帮助,为用户提供使此表可编辑的选项 - 然后将其保存到数组中。我可以添加什么?

0 个答案:

没有答案