只有单个返回值($ data)才会显示codeigniter View页面

时间:2014-12-18 07:11:21

标签: mysql codeigniter

我在 view / result.php

中有一个 result.php 页面
<body>        
    <table border="1">
        <tr>
            <th>name1</th>
            <th>name2</th>
            <th>name3</th>
        </tr>
        <?php foreach($result as $row): ?>
        <tr>
            <td><?php echo $row->name1; ?></td>
            <td><?php echo $row->name2; ?></td>
            <td><?php echo $row->name3; ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php echo 'Your new value is '.$name1;?>
    <?php echo 'Your new value is '.$name2;?>
    <?php echo 'Your new value is '.$name3;?>
</body>

html表目前正在运行。它会从我的表测试中检索数据并显示它。问题是在下面的代码中回显部分,它不起作用。

<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>

从同桌测试。它在错误页面中显示错误,显示Undefined variable name1,name2,name3

控制器/使用example.php

<?php
class Example extends CI_Controller {

 function __construct()
  {
   parent::__construct();
   $this->load->helper('form');
   $this->load->helper('url');

 }

 function index() {
  $this->load->view('example_view');
  $this->getvalue();
  $this->edit_content();
 }

 function getvalue()
{
 if ($this->input->post('submit')==true) {
  $data1['name1']=$this->input->post('name1');
  $data1['name2']=$this->input->post('name2');
  $data1['name3']=$this->input->post('name3');
  $this->load->view('result',$data1);

  $this->load->model('Insert_model');
  $this->Insert_model->uploaddata($data);  

 }


}

function edit_content()
    {
        $data   = array();
        $this->load->model('Selected_model');
        $this->load->helper('url');
        $data['result'] = $this->Selected_model->get_contents();
        $this->load->view('result',$data);

}
}
?>

2 个答案:

答案 0 :(得分:0)

<body>        
<table border="1">
    <tr>
        <th>name1</th>
        <th>name2</th>
        <th>name3</th>
    </tr>
    <?php if(is_array($result)){ foreach($result as $row): ?>
    <tr>
        <td><?php echo $row->name1; ?></td>
        <td><?php echo $row->name2; ?></td>
        <td><?php echo $row->name3; ?></td>
    </tr>
    <?php endforeach; } ?>
</table>
<?php echo 'Your new value is '.$row->name1;?>
<?php echo 'Your new value is '.$row->name2;?>
<?php echo 'Your new value is '.$row->name3;?>

答案 1 :(得分:0)

如果您的数组类似于以下格式。

$info = array
(
    [0] => Array
        (
            [name1] => a
            [name2] => b
            [name3] => c
        )

    [1] => Array
        (
            [name1] => aa
            [name2] => bb
            [name3] => cc
        )

    [2] => Array
        (
            [name1] => aaa
            [name2] => bbb
            [name3] => ccc
        )

    [3] => Array
        (
            [name1] => aaaa
            [name2] => bbbb
            [name3] => cccc
        )

);

然后

<body>        
<table border="1">
    <tr>
        <th>name1</th>
        <th>name2</th>
        <th>name3</th>
    </tr>
<?php foreach($info as $row): ?>
    <tr>
        <td><?php echo $row->name1; ?></td> 
        <td><?php echo $row->name2; ?></td>
        <td><?php echo $row->name3; ?></td>
    </tr>
    <?php endforeach; ?>
</table>

上述循环将为您提供准确的结果,如。

name1    name2    name3
  a        b       c
  aa       bb      cc
  aaa      bbb      ccc
  aaaa     bbbb     cccc

现在,以下代码将生成错误,因为它不知道以下

$row->name1
$row->name1
$row->name1

原因是这些是在foreach循环的范围内定义的,这就是它产生错误的原因。