Codeigniter从表中获取记录未定义变量错误

时间:2018-09-07 12:39:04

标签: php codeigniter

我试图从表中获取数据,创建了控制器,模型和视图,但是当我尝试打开视图时,出现两个错误,一个是Message: Undefined variable: u_list,另一个是Message: Invalid argument supplied for foreach()我正在使用CodeIgniter 3.1.9

控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class UserFetch extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->model('userinsert');
    }

    public function index() {  
     $data['u_list']=$this->userinsert->select();
     $this->load->view('dashboard', $data);
    }
}
?>

型号

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class UserInsert extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    function user_insert($data) {
        $this->db->insert('users', $data);
    }

    public function select() {  
        $query = $this->db->get('users');
        return $query; 
    } 
}
?>

查看

<tbody>
    <?php
        foreach ($u_list as $row) {  
    ?>
    <tr>  
    <td><?php echo $row->first_name;?></td>
    <td><?php echo $row->last_name;?></td>
    </tr>  
    <?php }
    ?>
</tbody>

帮我这个家伙

4 个答案:

答案 0 :(得分:2)

用下面的代码替换您的功能选择

    Scanner s=new Scanner(System.in);
    int m=s.nextInt();
    s.nextLine();
    String str=s.nextLine();
    StringTokenizer st=new StringTokenizer(str);
    int len=st.countTokens();
    int a[]=new int[len];
    String []temp=new String[len];
    for(int i=0;i<len;i++)
    {
        temp[i]=st.nextToken();
        a[i]=Integer.parseInt(temp[i]);
    }
    Arrays.sort(a);
    List<String> output = new ArrayList<>();
    for(int i=0;i<len-1;i++)
    {
        for(int j=i+1;j<len;j++)
        {
            if((a[i]+a[j])==m) {
                String msg = a[i]+" "+a[j];
                if(!output.contains(msg)) {
                    System.out.println(msg);
                    output.add(msg);
                }
            }
        }
    }
    s.close();

希望这会有所帮助

答案 1 :(得分:1)

您在MODEL中忘记了result

 public function select() {  
        $query = $this->db->get('users')->result();
        return $query; 
    } 

答案 2 :(得分:1)

..
<dependencies>
  ..
  <!-- This is the lib I want to add -->
  <dependency>
      <groupId>io.lettuce</groupId>
      <artifactId>lettuce-core</artifactId>
      <version>5.0.3.RELEASE</version>
  </dependency>
  ..
</dependencies>
...
<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <finalName>${uberjar.name}</finalName>
              <transformers>
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.openjdk.jmh.Main</mainClass>
                </transformer>
              </transformers>
              <filters>
                <filter>
                  <!--
                      Shading signed JARs will fail without this.
                      http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
                  -->
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
</plugin>
...

答案 3 :(得分:0)

在您的模型中...

public function select() {  
      $this->db->select("*"); 
      $this->db->from('users');
      $query = $this->db->get();
      return $query->result();
}