2012-10-07 33 views
0

好的,所以我使用codeigniter和tankauth。我想创建一个函数,用户可以提出问题并将问题存储在数据库中。看起来很简单,虽然我似乎遇到了问题,一旦我开始为控制器,库和模型创建新类。这是我到目前为止所做的:Codeigniter:创建新的类与数据库交互

VIEW:welcome.php(这是第一页,用户可以转到ask_question页面,并在结尾处显示锚点,以及运行时)。

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

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

机型: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; 
    } 
} 

在使用已经创建的类之前,我已经做了类似的事情没有问题。我还没有创建自己的类。我不知道我是否在写它(我刚刚复制了当前类的设置方式)。所以,请让我知道我是否有任何错误或任何事情。现在我无法让我的表单显示出来。该页面只是空白。

+0

加上'使用error_reporting(-1)'在笨脚本 –

+0

心想笨在index.php文件默认设置为显示所有错误的顶部 – LiverpoolFTW

+0

我不知道,确定 –

回答

0

我不知道这是否是唯一的错误 - 但它是其中之一 - 改变你的 '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 
+0

好的,所以对于从我的welcome.php视图链接到我的ask_question视图,我是否应该打电话给/ ask/ask_question /? (我更新了我的问题,将welcome.php包含在页面的锚点中。 – LiverpoolFTW