duanne9313 2014-10-19 06:59
浏览 46
已采纳

将PHP数组名称转换为字符串的最佳方法

I have an HTML table, linked to PHP $_SESSION data, to which I wish to add a Delete button to every row that deletes not only that row from the HTML table, but also from the $_SESSION variable.

This is the code that populates my table:

tableData.php

// echo the table headings
echo <<<HERE
<tr>
    <th>CityID</th>
    <th>State</th>
    <th>City</th>
    <th></th>
</tr>
HERE;

if (isset($_SESSION['cityData']))
{

    foreach($_SESSION['cityData'] as $city)
    {
        echo "<tr>";
        // print each row of data
        foreach($city as $data)
        {
            echo "<td>" . $data . "</td>";
        }
        //echo '<td><button action="<?php unset(' . $_SESSION['cityData'][key($_SESSION['cityData'])] . ')?>">Delete Row</button></td>';
        echo "</tr>";
    }

}

The line that I commented out, echo '<td><button action="<?php unset(' . $_SESSION['cityData'][key($_SESSION['cityData'])] . ')?>">Delete Row</button></td>'; is the line that creates the button that I am trying to create, to do what I am wanting it to do. I am trying to figure out the best way to name the array that I want gone.

P.S. I know, I should have it invoke some other function that does both tasks, it is just, if I pass the array in like I did, it will complain of " Array to string conversion ". Is there a way to do what I am trying to do, cleanly?

  • 写回答

2条回答 默认 最新

  • douqilin4296 2014-10-19 07:14
    关注

    It's just not that simple. You need to get your buttons to submit to a link, and then have the PHP unset the content.

    foreach($_SESSION['cityData'] as $index => $city) //added $index =>
    {
        echo "<tr>";
        // print each row of data
        foreach($city as $data)
        {
            echo "<td>" . $data . "</td>";
        }
        echo '<td><form method="post" action=""><input type="hidden" name="delete" value="' . $index . '"><input type="submit" value="Delete Row"></form></td>';
        echo "</tr>";
    }
    

    So I added a form that a button that will submit to data that indicates the row number, so when your client clicks on the button, it will submit them and the row number will be passed as a POST variable.

    At the top of tableData.php, you can then have logic handling the delete. Simply check if the delete is set, and then attempt to unset from there.

    if (isset($_POST['delete']))
        unset($_SESSION['cityData'][$_POST['delete']]);
    

    You will want to have further validation that checks if POST delete within the bounds of $_SESSION['cityData'], but the basic idea is there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系