Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php检查基于过去用户偏好的复选框?_Php - Fatal编程技术网

使用php检查基于过去用户偏好的复选框?

使用php检查基于过去用户偏好的复选框?,php,Php,我试图根据存储在CSV中的以前的用户首选项选中复选框,但它只是在CSV中运行检查第一个索引,而跳过其余索引。在下面的代码中,如果我将“Sports,Lowkey,Wine”的CSV传递给它,我希望它在页面加载时检查这三个,但它只是检查运动 $typeArray = array( "1" => "Sports", "2" => "College", "3" => "Pub", "4"

我试图根据存储在CSV中的以前的用户首选项选中复选框,但它只是在CSV中运行检查第一个索引,而跳过其余索引。在下面的代码中,如果我将“Sports,Lowkey,Wine”的CSV传递给它,我希望它在页面加载时检查这三个,但它只是检查运动

$typeArray = array(
            "1" => "Sports",
            "2" => "College",
            "3" => "Pub",
            "4" => "Night Club",
            "5" => "Lowkey",
            "6" => "Wine",
            "7" => "Craft Beer",
        );
        $Sports="";
        $College="";
        $Pub="";
        $NightClub="";
        $Lowkey="";
        $Wine="";
        $CraftBeer="";
        $string = "Sports, Lowkey, Wine";
        $csv = str_getcsv($string, ", ");
        for($i=0; $i<count($csv); $i++){
            $index = array_search($csv[$i], $typeArray);
            if($index == 1){
                $Sports = "checked";
            }
            if($index == 2){
                $College = "checked";
            }
            if($index == 3){
                $Pub = "checked";
            }
            if($index == 4){
                $NightClub = "checked";
            }
            if($index == 5){
                $Lowkey = "checked";
            }
            if($index == 6){
                $Wine = "checked";
            }
            if($index == 7){
                $CraftBeer = "checked";
            }
            unset($index);
        }
        echo "<input type=\"checkbox\" name=\"Type[1]\" value=\"Sports\"" . $Sports . ">Sports ";
        echo "<input type=\"checkbox\" name=\"Type[2]\" value=\"College\"" . $College . ">College ";
        echo "<input type=\"checkbox\" name=\"Type[3]\" value=\"Pub\"" . $Pub . ">Pub ";
        echo "<input type=\"checkbox\" name=\"Type[4]\" value=\"Night Club\"" . $NightClub . ">Night Club ";
        echo "<input type=\"checkbox\" name=\"Type[5]\" value=\"Lowkey\"" . $Lowkey . ">Lowkey<br> ";
        echo "<input type=\"checkbox\" name=\"Type[6]\" value=\"Wine\"" . $Wine . ">Wine ";
        echo "<input type=\"checkbox\" name=\"Type[7]\" value=\"Craft Beer\"" . $CraftBeer . ">Craft Beer<br> ";
$typeArray=array(
“1”=>“体育”,
“2”=>“学院”,
“3”=>“酒吧”,
“4”=>“夜总会”,
“5”=>“低调”,
“6”=>“葡萄酒”,
“7”=>“工艺啤酒”,
);
$Sports=“”;
$College=“”;
$Pub=“”;
$NightClub=“”;
$Lowkey=“”;
$Wine=“”;
$CraftBeer=“”;
$string=“运动、低调、葡萄酒”;
$csv=str_getcsv($string,“,”);
对于($i=0;$i交换此行
$index=array\u搜索($csv[$i],$typeArray);

为此:


$index=array\u search(trim($csv[$i]),$typeArray);
当您对
$csv
进行变量转储时,您会注意到在
Lowkey
Wine
之前有一个空格:

array(3) { 
    [0]=> string(6) "Sports" 
    [1]=> string(7) " Lowkey" 
    [2]=> string(5) " Wine" 
}
因此,您的
数组搜索将失败

根据,在
str\u get\u csv
中的
分隔符只能是一个字符。您可以使用或


你可以发布简历(或它的一个代表性样本)吗?哎呀,刚才你看到了…@user3511578我使用字符串CSV,这会将字符串放入数组$string=“Sports,Lowkey,Wine”;$CSV=str\u getcsv($string,”);尝试将
name=\“Type[1]\
更改为
name=\“1\”
对其他人也一样。因为,我看不到任何与
类型相关的东西
@Fred ii-它被称为类型[],因为这是一个显示酒吧类型的酒吧web应用程序
$csv = explode(', ', $string);

// or 

$index = array_search(trim($csv[$i]), $typeArray);