Codeigniter:创建新类以与数据库交互

时间:2012-10-07 00:50:17

标签: php codeigniter tankauth

好的,所以我使用的是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(); ?>

CONTROLLER: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;
    }
}

MODELS:questions.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;
    }
}

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

1 个答案:

答案 0 :(得分:1)

我不确定这是否是唯一的错误 - 但它是其中之一 - 改变你的'capitalisaiton':

来自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