codeigniter: editing and deleting from database

和自甴很熟 提交于 2019-12-02 19:09:50

问题


I am having trouble coding to edit/delete a question in the database. From my quiz questions. This is my model. I would like to edit a question from the view through the controller.

This is my current mode:

function getquestions($quizid)
{
    // get data about the quiz - we need the quiz size
    $quiz = $this->getquiz($quizid);
    $qzsize = $quiz['quizsize'];

    $this->db->select('id'); // we want just the id fields
    $this->db->limit($qzsize); // how many questions to ask

    // this next clause allows us to select random questions - a full discussion of how this works and its drawbacks is

    // here https://www.warpconduit.net/2011/03/23/selecting-a-random-record-using-mysql-benchmark-results/

    $this->db->order_by('RAND()');
    $res = $this->db->get_where('questions',array('quizid' => $quizid));
    log_message('DEBUG',$this->db->last_query());
    if ($res->num_rows() == 0) {

        // no questions found!

        // returning false to controller tells it we have encountered an error

        // leave controller to decide how to handle error
        return false;
    }

    $questions = array();
    foreach ($res->result_array() as $row) {
        $questions[] = $row['id'];
    }
    shuffle($questions); // provide questions in random order
    $res->free_result();
    return $questions;
}
/**
 * get a question

 * @param int - question id

 * @return mixed false|array - question data + answers (array)
 */
function getquestion($id) {
    $this->db->where('id',$id);
    $res = $this->db->get('questions');
    if ($res->num_rows() != 1) {
        return false;
    }
    $question = $res->row_array(); // get first row as an array - this is the question data

    // now get the answers for this question

    $res = $this->db->get_where('answers',array('questionid' => $id));
    if ($res->num_rows() < 2) {

        // must have at least two answers
        return false;
    }
    $answers = array();
    // get each row as an array and push it onto the answers array

    foreach ($res->result_array() as $row) {
        $answers[] = $row;
    }
    shuffle($answers);
    // now return all the data as one array, with keys to help access elements

    return array('q' => $question,'a' => $answers);
}

回答1:


Somewhere in you controller you need to detect the 'delete' action. This might be through a URL or through a form input. I am sure you can do this bit.

For instance, a controller might have its method defined as

public function view($question_id=0, $action='') { 

Then you might have a link on your page that has the action part defined as 'delete'. In you controller you would have an

if ($action == 'delete') {

To delete the chosen question from the database you would use

$this-db-where('question_id', $question_id);
$this->db->limit(1);
$this->db->delete('my_table');

Which is described in detail here: http://www.codeigniter.com/userguide3/database/query_builder.html#deleting-data

To edit the data, you would collect your form input thus:

http://www.codeigniter.com/userguide3/libraries/input.html

And using your error checked input values, post the new data to your database using the query builder (linked to earlier).

Something like

$data = array(
   'name' => $name,
   'description' => $description;
);
$this-db-where('question_id', $question_id);
$this->db->update('mytable', $data);

I hope that helps in some way, or if not, please be more specific in your question.

Good luck with your website,

Best wishes,

Paul



来源:https://stackoverflow.com/questions/34696791/codeigniter-editing-and-deleting-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!