0

好的,所以我正在使用 codeigniter 和 tankauth。我想创建一个功能,用户可以在其中提出问题并将问题存储在数据库中。似乎很简单,尽管一旦我开始为控制器、库和模型创建新类,我似乎遇到了问题。这是我到目前为止所做的:

VIEW:welcome.php(这是第一个页面,用户可以通过锚点进入ask_question页面,当它工作时很好)。

<?php echo anchor('/ask/ask_question', 'Ask a Question'); ?></br>

查看:ask_question.php

Ask a questsion bro!</br>
</br>
<?php 
$title = array(
           'name' => 'title',
           'id' => 'title',
           'value' => set_value('title'),
           'maxlength' => 750,
           'size' => 30,
           'style' => 'width: 100px',
           );
$body = array(
          'name' => 'body',
          'id' => 'body',
          'value' => set_value('body'),
          'maxlength' => 5000,
          'size' => 30,
          'style' => 'width: 100px',
          );
?>
<?php echo form_open('/Ask/ask_question'); ?>
<table>
    <tr>
        <td><?php echo form_label('Title', $title['id']); ?></td>
        <td><?php echo form_input($title); ?></td>
    <td style="color: red;"><?php echo form_error($title['name']); ?><?php echo isset($errors[$title['name']])?$errors[$title['name']]:''; ?></td>
    </tr>
    <tr>
        <td><?php echo form_label('Body', $body['id']); ?></td>
        <td><?php echo form_input($body); ?></td>
    <td style="color: red;"><?php echo form_error($body['name']); ?><?php echo isset($errors[$body['name']])?$errors[$body['name']]:''; ?></td>
    </tr>
</table>
<?php echo form_submit('ask_question', 'Ask Question'); ?>
<?php echo form_close(); ?>

控制器:ask.php

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

class ask extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->helper(array('form','url'));
        $this->load->library('Ask_Question');
        $this->load->library('tank_auth');
        $this->load->library('form_validation');
        $this->lang->load('tank_auth');
    }

    function index()
    {
      if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
      }
      else {
        $this->load->view('/ask/ask_question/');
      }
    }

    function ask_question()
    {
      if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
      }
      elseif ($this->tank_auth->is_logged_in(FALSE)) {
        redirect('/auth/send_again/');
      }
      else {
        $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean|min_length[15]');
        $this->form_validation->set_rules('body', 'Body', 'trim|required|xss_clean|min_length[15]');
        if ($this->form_validation->run()) {
          if (!is_null($data = $this->Ask_Question->ask_question(
                                     $this->form_validation->set_value('title'),
                                     $this->form_validation->set_value('body')))) {
        $data['site_name'] = $this->config->item('website_name', 'tank_auth');
          }
          else {
        $errors = $this->Ask_Question->get_error_message();
        foreach ($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
          }
        }
      }
      $this->load->view('/ask/ask_question/');
    }
}

图书馆:Ask_Question.php

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

require_once('phpass-0.1/PasswordHash.php');

define('STATUS_ACTIVATED', '1');
define('STATUS_NOT_ACTIVATED', '0');

/**
 * Tank_auth
 *
 * Authentication library for Code Igniter.
 *
 * @package     Tank_auth
 * @author      Ilya Konyukhov (http://konyukhov.com/soft/)
 * @version     1.0.9
 * @based on    DX Auth by Dexcell (http://dexcell.shinsengumiteam.com/dx_auth)
 * @license     MIT License Copyright (c) 2008 Erick Hartanto
 */
class Ask_Question
{
    private $error = array();

    function __construct()
    {
        $this->ci =& get_instance();

        $this->ci->load->config('tank_auth', TRUE);

        $this->ci->load->library('session');
        $this->ci->load->database();
        $this->ci->load->model('questions');

        // Try to autologin
        $this->autologin();
    }

    function ask_question($title, $body)
    {
      $user_id = $this->ci->session->userdata('user_id');
      $class_id = '1';
      $tag1 = 'KENT';
      $this->ci->questions->ask_question($user_id, $class_id, $title, $body, $tag1);
      return NULL;
    }

    function get_error_message()
    {
        return $this->error;
    }
}

模型:问题.php

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

/**
 * Users
 *
 * This model represents user authentication data. It operates the following tables:
 * - user account data,
 * - user profiles
 *
 * @package Tank_auth
 * @author  Ilya Konyukhov (http://konyukhov.com/soft/)
 */
class questions extends CI_Model
{
    private $table_name         = 'users';          // user accounts
    private $profile_table_name = 'user_profiles';  // user profiles

    function __construct()
    {
        parent::__construct();

        $ci =& get_instance();
        $this->table_name           = $ci->config->item('db_table_prefix', 'tank_auth').$this->table_name;
        $this->profile_table_name   = $ci->config->item('db_table_prefix', 'tank_auth').$this->profile_table_name;
    }

    function ask_question($user_id, $class_id, $title, $body, $tag1)
    {
      $this->db->insert('questions', array('user_id'=>$user_id,'class_id'=>$class_id, 'question_title'=>$title, 'question_content'=>$body, 'focus_peak_1'=>$tag1)); 
      return NULL;
    }
}

在使用已经创建的类之前我已经完成了这样的事情,没问题。我还没有创建自己的课程。我不确定我是否正在写(我只是复制了当前类的设置方式)。所以请让我知道我是否有任何错误或任何事情。现在我根本无法显示我的表格。该页面是空白的。

4

1 回答 1

1

我不确定这是否是唯一的错误——但它是其中之一——改变你的“资本”:

来自 ask_question.php:

<?php echo form_open('/Ask/ask_question'); ?>

<?php echo form_open('/ask/ask_question'); ?>

来自 ask.php:

 class ask extends CI_Controller

 class Ask extends CI_Controller
于 2012-10-07T02:38:06.873 回答