dqxuiq7772 2016-01-14 08:22
浏览 7
已采纳

检查循环值中是否存在逗号分隔值

I get some values from an old db of a client as such:

$query = "SELECT rowid,source FROM `".$table."`";
$result = mysql_query($query);$temp=0;$p = array();
while ($row = mysql_fetch_array($result)) {$p[$temp] = $row;$temp++;}

and then I loop through the values as such:

// loop through all possible values
foreach ($p as $obj) {

}

Inside the foreach loop I will print <option> element and I want to determine the <option selected/> elements. The selected elements could be more than 1, since its a multiselect form.

First thing I check whether a $selected value equals to rowid.

// check if exists equals to rowid
if ($obj["rowid"] == $selected){
   $return .= '<option value="'.$obj["rowid"].'" selected>'.$obj["source"].'</option>';
}else{
   $return .= '<option value="'.$obj["rowid"].'">'.$obj["source"].'</option>';                 
}

This works just find if $selected = "1" (or any single digit)

My problem and my question comes if $selected = "3,4". Then I need to loop through the values and separate the comma-separated values inside foreach ?

Here is my complete code:

$selected = "3,4";
$query = "SELECT rowid,source FROM `" . $table . "`";
$result = mysql_query($query);
$temp = 0;
$p = array();
while ($row = mysql_fetch_array($result)) {
    $p[$temp] = $row;
    $temp++;
}

// loop through all possible values
foreach($p as $obj) {
    // check if exists equals to rowid
    if ($obj["rowid"] == $selected) {
        $return.= '<option value="' . $obj["rowid"] . '" selected>' . $obj["source"] . '</option>';
    }
    else {

        // check if default exists with comma separated values
        if (!empty($default) && strpos($selected, ',') !== false) {
            $arr_values = explode(',', $selected);

            // loop through comma-separated values to get the selected items
            foreach($arr_values as $selected) {
                if ($obj["rowid"] == $default) {
                    $return.= '<option value="' . $obj["rowid"] . '" selected>' . $obj["source"] . '</option>';
                }
            }

            // $return .= '<option value="'.$obj["rowid"].'">'.$obj["source"].'</option>';
        }
        else {
            $return.= '<option value="' . $obj["rowid"] . '">' . $obj["source"] . '</option>';
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dousao9569 2016-01-14 08:27
    关注

    Just use in_array() + explode() functions combination to check if rowid exists in selected string

    if (in_array ($obj["rowid"], explode(",",$selected))){
          $return .= '<option value="'.$obj["rowid"].'" selected>'.$obj["source"].'</option>';
    }else{
          $return .= '<option   value="'.$obj["rowid"].'">'.$obj["source"].'</option>';                 
    }
    

    Explanation: E.G. if $selected = "3,4" then explode(",",$selected) return you array(3,4) and in_array check if $obj["rowid"]' exists in that array so it mean you will have in you output multiple <options with attribute selected set

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi