Php 显示从数组获取的值时出错

Php 显示从数组获取的值时出错,php,jquery,Php,Jquery,我有一个数据库表,从中获取值并显示在视图页面中。然后管理员将单击表中每一行旁边的复选框。单击每个复选框,我将获取该复选框的值。其代码(查看文件和js)如下所示 查看文件: <input type="checkbox" name="options" id="options" value="<?php echo $row->slNo?>"> <input type="checkbox" id="selectAll" name="selectAll" />

我有一个数据库表,从中获取值并显示在视图页面中。然后管理员将单击表中每一行旁边的复选框。单击每个复选框,我将获取该复选框的值。其代码(查看文件和js)如下所示

查看文件:

<input type="checkbox" name="options" id="options" value="<?php echo $row->slNo?>">
<input type="checkbox" id="selectAll" name="selectAll" />
<input type="hidden" id="getrolevalues">
我已经将得到的值保存在一个隐藏的文本框中,并将其发送到控制器。下面是它的代码(查看文件和js)

查看文件:

<input type="checkbox" name="options" id="options" value="<?php echo $row->slNo?>">
<input type="checkbox" id="selectAll" name="selectAll" />
<input type="hidden" id="getrolevalues">
到目前为止,一切正常。但在发送存储在数组中的值后,我收到一个错误。即在控制器中,我获取数组值,然后使用explode函数分离每个值。然后使用for循环将这些值发送到模型,获取值并显示结果。代码如下

控制器:

function tabledata()
    {
         echo $data=$this->input->post('gotoption');
         $pieces = explode( ",", $data);
        //echo $pieces[1];
        //echo $e=count($pieces);
        $sql=array();
    for($i = 0; $i< count($pieces); $i++)
    { 
        $piec= $pieces[$i];
        $sql[]=$this->rolesmodel->roleselect($piec);
    }
    $sql['dipl']=$sql;

        $slr=$this->load->view('roleschecked',$sql,true);
        $value=array(
            'result'=>$slr
        );
        echo json_encode($value);
}
如果我这样使用,我会得到一个错误

.........
$sql[]=$this->rolesmodel->roleselect($piec);
.............
$sql['dipl']=$sql;

请帮我解决这个问题

为什么要在隐藏字段中用逗号分隔所有复选框

您可以将它们作为数组直接发布:

<input type="checkbox" name="options[]" value="123" />
<input type="checkbox" name="options[]" value="543" />
因此,您的
tabledata()
函数如下所示:

function tabledata(){

    $options = $this->input->post('options');
    $sql=array();
    foreach($options as $option){
        $sql[]=$this->rolesmodel->roleselect(intval($option));
    }

    // I'm not sure why you are assigning this back into the same array?!
    $sql['dipl']=$sql;
    $slr=$this->load->view('roleschecked',$sql,true);
    $value=array(
        'result'=>$slr
    );
    echo json_encode($value);
}
// function tabledata()
$innerSql = array();
for($i = 0; $i < count($pieces); $i++) { 
    $piec = $pieces[$i];
    $innerSql[] = $this->rolesmodel->roleselect($piec);
}

$sql['dipl'] = $innerSql;

请看一看注释行,其中您将所有SQL语句分配回SQL数组,这对我来说很奇怪。

对我来说,它看起来像konsolenfreddy建议的那样-请看$SQL['dipl']分配。当您第一次为$sql赋值时,您有一个查询结果数组。然后再次分配$sql,但在同一个数组上执行此操作-$sql,但使用索引'dipl'将其分配到。得到的是数组的数组。看看这个例子:

$aSql = array();
$aSql[] = 'foo';

$aSql['bar'] = $aSql;

var_dump($aSql);

result >>>

array(2) {
  [0]=>
  string(3) "foo"
  ["bar"]=>
  array(1) {
    [0]=>
    string(3) "foo"
  }
}
尝试重命名内部$sql,如下所示:

function tabledata(){

    $options = $this->input->post('options');
    $sql=array();
    foreach($options as $option){
        $sql[]=$this->rolesmodel->roleselect(intval($option));
    }

    // I'm not sure why you are assigning this back into the same array?!
    $sql['dipl']=$sql;
    $slr=$this->load->view('roleschecked',$sql,true);
    $value=array(
        'result'=>$slr
    );
    echo json_encode($value);
}
// function tabledata()
$innerSql = array();
for($i = 0; $i < count($pieces); $i++) { 
    $piec = $pieces[$i];
    $innerSql[] = $this->rolesmodel->roleselect($piec);
}

$sql['dipl'] = $innerSql;
//函数tabledata()
$innerSql=array();
对于($i=0;$irolesmodel->roleselect($piec);
}
$sql['dipl']=$innerSql;

我之所以在一个隐藏字段中写入所有以逗号分隔的复选框,是因为我从一个从模型中获取的表中获取了这些chkbox值。查看页面和模型页面位于下面查看页面:SL。无名称角色。模型是函数selectroles(){$slct=“SELECT*fromDummyRoles”;$qry=$this->db->query($slct);return$qry;}。从注释中的代码中,仍然可以使用
选项[]
。此外,您多次分配ID
选项
,这是无效的。您所有的选项都具有相同的ID“options”。这是错误的(可能会造成一些混乱)。您最好的选择是从“选项”中删除id(或者在它们后面添加一个计数器,甚至是值)。错误是->致命错误:调用成员函数result()在第9行C:\wamp\www\newatmindia\application\views\roleschecked.php中的非对象上,显示错误的视图文件是您选择了以下人员

Sl。无姓名角色
$aSql = array();
$aSql[] = 'foo';

$aSql['bar'] = $aSql;

var_dump($aSql);

result >>>

array(2) {
  [0]=>
  string(3) "foo"
  ["bar"]=>
  array(1) {
    [0]=>
    string(3) "foo"
  }
}
// function tabledata()
$innerSql = array();
for($i = 0; $i < count($pieces); $i++) { 
    $piec = $pieces[$i];
    $innerSql[] = $this->rolesmodel->roleselect($piec);
}

$sql['dipl'] = $innerSql;