0

Ok, so i am using codeigniter and tankauth. I want to create a function where a user can ask a question and the question is stored in the databse. Seems simple enough although I seem to be running into issues once i start creating new classes for the controllers, libraries, and models. Here is what i have done so far:

VIEW: welcome.php (this is the first page, the user can go to the ask_question page with the anchor at the end, well when it works).

<?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(); ?>

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/');
    }
}

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

I have done stuff like this before using the already created class no problem. I have not yet created my own classes. I am not sure if i am doing it write (I just copied the way the current classes are set up). So please let me know if I I have any errors or anything. Right now I cannot get my form to show up at all. the page is just blank.

j0k
  • 22,600
  • 28
  • 79
  • 90
ageoff
  • 2,798
  • 2
  • 24
  • 39

1 Answers1

1

I'm not sure if this is the only error - but it is one of them - change your 'capitalisaiton':

from ask_question.php:

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

to

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

from ask.php:

 class ask extends CI_Controller

to

 class Ask extends CI_Controller
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • Ok, so for a link to my ask_question view from my welcome.php view shall i make the call to /ask/ask_question/ too? (I updated my question to include the welcome.php with the anchor to the page. – ageoff Oct 07 '12 at 03:49