php - 使用Codeigniter,程序无法正常运行

标签 php mysql codeigniter fetch

我正在尝试(没有成功)使用 codeigniter 运行我的程序:

这是程序:

    <?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>

我已经尝试过这个..但是当我运行它时我遇到了一些错误;这是我的 Codeigniter 代码:

我的“查看”文件:

<?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>

这是我的 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;

        }

    }


?>

和 Controller 文件:

    <?php

    class Home extends CI_Controller{

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

        }
    }
?>

我不知道发生了什么,我不明白:/。 这些是错误:

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

希望你能帮助我

最佳答案

使用 Codeigniter

使用 $data 在 Controller 上传递数据,如下所示

class Home extends CI_Controller {

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

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

        // Load views like so so don't have to use include 

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

    }
}
// ?> No need for php close on controllers and models etc.

https://www.codeigniter.com/user_guide/general/views.html#loading-multiple-views完整阅读

https://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view

Codeigniter has it's own database library The loading of the database in codeigniter

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

配置/数据库.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()
);

然后自动加载它。自动加载一些库和助手也很好。完整阅读本手册。

配置/autoload.php

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

这里也有一些不错的读物

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

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

https://www.codeigniter.com/user_guide/general/styleguide.html#php-closing-tag

关于php - 使用Codeigniter,程序无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43049422/

相关文章:

如果 doc 的关键字发生变化,则 MySQL 会触发 doc 的更新时间戳

PHP 生成 SQL 复选框 - 基于选中的选择/插入和删除

mysql - 如何使用MySQL查询真子集?

javascript - 代码点火器 : form validation jquery

php - 在 XAMPP 上安装 PEAR

javascript - 如何使用 jQuery.ajax 将此 JSON 转换为在 HTML 中显示,为什么最后是 0?

phpMyAdmin 导致连接失败

codeigniter - 使用 Codeigniter 支付宝

php - 在 CodeIgniter 的 Hook 中获取 session 数据

javascript - 如何使用SweetAlert2在PHP中制作删除确认框?