简体   繁体   English

使用CodeIgniter创建新闻项

[英]Create News Item with CodeIgniter

I've followed the documentation, but when I try to create a news item, I get the error: 我遵循了文档,但是当我尝试创建新闻项时,出现错误:

"This page isn't working www.mi-linux.wlv.ac.uk didn't send any data. ERR_EMPTY_RESPONSE" “此页面不起作用,www.mi-linux.wlv.ac.uk没有发送任何数据。ERR_EMPTY_RESPONSE”

My base url: 我的基本网址:

$config['base_url'] = 'http://www.mi-linux.wlv.ac.uk/1043809/CodeIgniterNew/';

My Routes: 我的路线:

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

My News_model.php File: 我的News_model.php文件:

<?php
class News_model extends CI_Model {

public function __construct() {
    $this->load->database();
}

public function get_news($slug = FALSE) {
    if ($slug === FALSE) {
        $query = $this->db->get('news');
        return $query->result_array();
    }

    $query = $this->db->get_where('news', array('slug' => $slug));
    return $query->row_array();
}

public function set_news() {
    $this->load->helper('url');

    $slug = url_title($this->input->post('title'), 'dash', TRUE);

    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'text' => $this->input->post('text')
    );

    return $this->db->insert('news', $data);
}
}

My database.php 我的database.php

$db['default'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => '1403809',
'password' => 'IhaveApassword',
'database' => 'db1403809',
'dbdriver' => 'mysqli',

Here is my controller, I have two other files in the controllers folder, one called pages.php and another called Welcome.php 这是我的控制器,在controllers文件夹中还有另外两个文件,一个名为pages.php,另一个名为Welcome.php

Controller News.php 控制器News.php

<?php
class News extends CI_Controller {

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

public function index() {
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';

    $this->load->view('templates/header', $data);
    $this->load->view('news/index', $data);
    $this->load->view('templates/footer');
}

public function view($slug = NULL) {
    $data['news_item'] = $this->news_model->get_news($slug);

    if (empty($data['news_item'])) {
        show_404();
    }

    $data['title'] = $data['news_item']['title'];

    $this->load->view('templates/header', $data);
    $this->load->view('news/view', $data);
    $this->load->view('templates/footer');
}

public function create() {
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['title'] = 'Create a news item';

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('text', 'Text', 'required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('templates/header', $data);
        $this->load->view('news/create');
        $this->load->view('templates/footer');

    }
    else {
        $this->news_model->set_news();
        $this->index();
    }
}
}

Well I guess it's your server which is down: 好吧,我想这是您的服务器故障了:

您网站的屏幕截图

Just to make sure it is not your code which is troublesome get a local PHP server and run it in local host. 只是要确保麻烦的不是您的代码,请获取本地PHP服务器并在本地主机中运行它。 Say WAMPP on Windows or XAMPP on Mac / Linux / Windows. 在Windows上说WAMPP,在Mac / Linux / Windows上说XAMPP。 They are free 他们是免费的

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

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