无法保存空列表

Unable to save empty list

需要帮助来保存空列表。我有 2 个列表,我可以在其中移动和保存,但是当我删除最后一个剩余项目并想要保存空列表时,它总是与最后一个项目一起保存,所以我无法保存空列表。

这是我的 2 个列表的代码:

<select name=master[] id=master class="master" multiple="multiple" size='6'>
<?php   
    $file = fopen("master.csv", "r");
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
        $master = $row[0];
?>
    <option value="<?php echo $master;?>"><?php echo $master; ?></option>
<?php
    }
?>
</select> 

<form action="update.php" method="post" >
    <input type=button class="master" name=b1 id=b1 value='Move >'>
    <input type=button class="master" name=b2 id=b2 value='< Remove'>&nbsp;&nbsp;&nbsp;&nbsp;

<select name=category[] id=category multiple="multiple" >
<?php   
    $file = fopen("category.csv", "r");
    while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
        $category = $row[0];
?>
    <option value="<?php echo $category;?>"><?php echo $category;?></option>
<?php
    }
?>
</select>
    <input type="hidden" name="masterlist" id="masterlist" value="">
<input type="submit" onclick="displayResult()" value="Save File" name="submit" >
</form>

这是我的 Update.php 文件:-

<?php
header("Location:".$URL.'index.php');

if ($_POST['masterlist']) {
    $list = $_POST['masterlist'];
    $str_master = explode (",", $list);
    foreach ($str_master as $key => $value) {
        $resultmaster.=$value. "\n";
    }
    file_put_contents('master.csv',$resultmaster);
}

if ($_POST['category']) {
    $category = $_POST['category'];
    $categoryunique = array_unique($category);
    sort($categoryunique);
    foreach ($categoryunique as $key => $value) {
        $resultcategory.=$value. "\n";
    }
    file_put_contents('category.csv',$resultcategory);
}
?>

这适用于保存空列表,但是当我尝试用多个列表保存空列表时,它会将前一个列表保存为空。不知道为什么?

<?php
header("Location:".$URL.'index.php');

if ($_POST['masterlist']) {
    $list = $_POST['masterlist'];
    $str_master = explode (",", $list);
    foreach ($str_master as $key => $value) {
        $resultmaster.=$value. "\n";
    }
    file_put_contents('master.csv',$resultmaster);
}

if ($_POST['category']) {
    $category = $_POST['category'];
    $categoryunique = array_unique($category);
    sort($categoryunique);
    foreach ($categoryunique as $key => $value) {
        $resultcategory.=$value. "\n";
    }
    file_put_contents('category.csv',$resultcategory);
}
if (empty($_POST['category'])) {
    file_put_contents("category.csv", "");
}

if ($_POST['category1']) {
    $category1 = $_POST['category1'];
    $category1unique = array_unique($category1);
    sort($category1unique);
    foreach ($category1unique as $key => $value) {
        $resultcategory1.=$value. "\n";
    }
    file_put_contents('category1.csv',$resultcategory1);
}
if (empty($_POST['category1'])) {
    file_put_contents("category1.csv", "");
}

if ($_POST['category2']) {
    $category2 = $_POST['category2'];
    $category2unique = array_unique($category2);
    sort($category2unique);
    foreach ($category2unique as $key => $value) {
        $resultcategory2.=$value. "\n";
    }
    file_put_contents('category2.csv',$resultcategory2);
}
if (empty($_POST['category2'])) {
    file_put_contents("category2.csv", "");
}
?>

if ($_POST['masterlist']) 当输入为空时不会成功,因为空字符串是假的。

如果要检查输入是否已提交,请使用if (isset($_POST['masterlist']))

你也可以在输入为空时写一个空文件:

if (empty($_POST['masterlist'])) {
    file_put_contents("master.csv", "");

对于多个表单,您需要更改代码以使其取决于提交的表单。

if (isset($_POST['form1'])) {
    $catkey = 'category';
    $filename = 'category.csv';
} else if (isset($_POST['form2'])) {
    $catkey = 'category1';
    $filename = 'category1.csv';
} else {
    die("Unrecognized form submitted");
}

if (empty($_POST[$catkey])) {
    file_put_contents($filename, "");
} else {
    $category = $_POST[$catkey];
    $categoryunique = array_unique($category);
    sort($categoryunique);
    foreach ($categoryunique as $key => $value) {
        $resultcategory.=$value. "\n";
    }
    file_put_contents($filename,$resultcategory);
}