简体   繁体   English

Codeigniter问题PHP,获取数据

[英]Codeigniter issue php,fetching data

I made a program in php and it works properly. 我用php编写了一个程序,它可以正常工作。 Now, i wanna make that program using Codeigniter. 现在,我想使用Codeigniter制作该程序。

Here is my old program: 这是我的旧程序:

    <?php include('connect.php');

?>

<html>
<head>

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</head>
    <body>

    <div class="container"> 
    <div class="row">
    <div class="col-md-12">



    <a href="estudiante.php"><button type="button" class="btn btn-success">AGREGAR</button></a><br /><br />
        <h2 align="center">TABLA:MATERIAS</h2>
        <input id="busqueda_tabla" type="text">
            <table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
                <thead>
                    <th>id</th>
                    <th>Carrera</th>
                    <th>Nombre</th>
                    <th>Descripcion</th>
                    <th>Carga horaria (hs)</th>
                    <th>Accion</th>
                </thead>

    <?php

                $sql=mysql_query("SELECT s.*, c.nombre AS carrera FROM materias s LEFT JOIN carreras c ON s.carrera_id=c.id");//ESTA FUNCION ME AYUDA A ACOMODAR LAS CARRERAS EN LA COLUMNA "CARRERA" DE LA TABLA
                $i=1;
                    while($row=mysql_fetch_array($sql)){
                        echo "<tr>
                                <td>".$i."</td>
                                <td>".$row['carrera']."</td>
                                <td>".$row['nombre']."</td>
                                <td>".$row['descripcion']."</td>
                                <td>".$row['carga_horaria']."</td>
                                <td align='center'>
                                    <a href='editar.php?editar=1&iden=".$row['id']."'><button type='button' class='btn btn-primary'>EDITAR</button></a> |
                                    <a href='borrar.php?borrar=1&iden=".$row['id']."'><button type='button' class='btn btn-danger'>BORRAR</button></a>
                                </td>
                        </tr>";
                        $i++;

                    }
                ?>


            </table>    

        </div>
        </div>
        </div>

    </body>



</html>

And, "connect.php" file 而且,“ connect.php”文件

    <?php

    $con=mysql_connect('localhost','root','root')OR die('error : '.mysql_error());
    $db=mysql_select_db('desafio');

    if($db){
        echo '';

    }else{
        echo 'Error :' .mysql_error(); 
    }


?>

This is my Codeigniter code: 这是我的Codeigniter代码:

My "view" file: 我的“查看”文件:

<?php include('header.php'); ?>

<?php include('footer.php'); ?>


    <div class="container"> 
    <div class="row">
    <div class="col-md-12">

        <h2 align="center">TABLA:MATERIAS</h2>
        <input id="busqueda_tabla" type="text">
            <table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
                <thead>
                    <th>id</th>
                    <th>Carrera</th>
                    <th>Nombre</th>
                    <th>Descripcion</th>
                    <th>Carga horaria (hs)</th>
                    <th>Accion</th>
                </thead>


    <?php
        $i=1;
        foreach($records as $record) {

            echo "<tr>
                      <td>".$i."</td>
                      <td>".$record->carrera."</td>
                      <td>".$record->nombre."</td>
                      <td>".$record->descripcion."</td>
                      <td>".$record->carga_horaria."</td>
                      <td align='center'>
                         <a href='editar.php?editar=1&iden=".$record->id."'><button type='button' class='btn btn-primary'>EDITAR</button></a> |
                         <a href='borrar.php?borrar=1&iden=".$record->id."'><button type='button' class='btn btn-danger'>BORRAR</button></a>
                      </td>
                  </tr>";
        }
        $i++;
    ?>
    </table>

        </div>
        </div>
        </div>

Here is my Crudmodel file: 这是我的Crudmodel文件:

      <?php

    Class Crudmodel extends CI_Model{

        public function getRecords(){

            $this->db->select('s.*, c.nombre AS carrera')
                     ->from('materias s')
                     ->join('carreras c', 's.carrera_id = c.id', 'left');
            $q = $this->db->get();

            if($q -> num_rows() > 0){

                return $q->result();
            }

            return false;

        }

    }


?>

And the controller file: 和控制器文件:

    <?php

    class Home extends CI_Controller{

        public function index(){
            $this->load->model('Crudmodel');
            $data['records'] = $this->Crudmodel->getRecords();
            $this->load->view('home', $data['records']);

        }
    }
?>

But it does not work, i got this errors: 但是它不起作用,我得到了这个错误:

https://i.gyazo.com/607303edcd861d0a2257611c925f3e6e.png
https://i.gyazo.com/eb9d833bf5d0818db2906ec1447550c0.png
https://i.gyazo.com/c5bb77d50315888a375a1e775a5ad68c.png

Dont know what to do :/ 不知道该怎么办:/

The main cause is database connection. 主要原因是数据库连接。 First of all, check your database connection in database.php file. 首先,在database.php文件中检查数据库连接。 If all thing is good then please load your database. 如果一切顺利,请加载数据库。 You can load database by two ways: 您可以通过两种方式加载数据库:

  1. In autoload file: autoload library > pass database in the array. 在自动加载文件中:自动加载库>在数组中传递数据库。

  2. Or in model, please load in Constructor : $this->load->db 或在模型中,请在构造函数中加载:$ this-> load-> db

Thanks in advance. 提前致谢。

<?php

class Home extends CI_Controller{

    public function index(){
        $this->load->model('Crudmodel');
        $data['records'] = $this->Crudmodel->getRecords();

        $this->load->view('header');
        $this->load->view('home', $data);
        $this->load->view('footer');

    }
}

The loading of the database in codeigniter 在Codeigniter中加载数据库

https://www.codeigniter.com/user_guide/database/configuration.html https://www.codeigniter.com/user_guide/database/configuration.html

config/database.php 配置/ database.php中

$db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '****',
        'database' => 'database_name',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array()
);

And then autoload it. 然后自动加载。 It is also good to autoload some libraries and helpers also. 也可以自动加载一些库和帮助程序。 Read the manual fully. 完整阅读手册。

config/autoload.php 配置/ autoload.php

$autoload['libraries'] = array('database');

Some good reading here also 这里也有一些不错的阅读

https://www.codeigniter.com/user_guide/general/models.html#loading-a-model https://www.codeigniter.com/user_guide/general/models.html#loading-a-model

And

https://www.codeigniter.com/user_guide/general/views.html#loading-multiple-views https://www.codeigniter.com/user_guide/general/views.html#loading-multiple-views

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM