codeigniter:从数据库编辑和删除 [英] codeigniter: editing and deleting from database

查看:135
本文介绍了codeigniter:从数据库编辑和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在数据库中编辑/删除问题。从我的测验问题。这是我的模型。我想通过控制器从视图编辑一个问题。



这是我目前的模式:

  function getquestions )
{
//获取有关测验的数据 - 我们需要测验大小
$ quiz = $ this-> getquiz($ quizid)
$ qzsize = $ quiz ['quizsize'];

$ this-> db-> select('id'); //我们只需要id字段
$ this-> db-> limit($ qzsize); //请问多少个问题

//下一个子句允许我们选择随机问题 - 如何工作的一个完整的讨论,它的缺点是

//这里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){

//没有找到问题!

//返回false到控制器告诉我们遇到错误

//让控制器决定如何处理错误
return false;
}

$ questions = array();
foreach($ res-> result_array()as $ row){
$ questions [] = $ row ['id'];
}
shuffle($ questions); //以随机顺序提供问题
$ res-> free_result();
return $ questions;
}
/ **
*获取问题

* @param int - 问题ID

* @return mixed false | 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(); //获取第一行作为数组 - 这是问题数据

//现在得到这个问题的答案

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

//必须至少有两个答案
return false;
}
$ answers = array();
//将每一行作为一个数组并推送到答案数组

foreach($ res-> result_array()as $ row){
$ answers [] = $ row;
}
shuffle($ answers);
//现在返回所有数据作为一个数组,用键帮助访问元素

返回数组('q'=> $ question,'a'=> $ answers) ;
}


解决方案

以检测删除动作。这可能是通过URL或通过表单输入。



例如,控制器可能定义为

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


$ b b

然后,您可能在您的页面上有一个链接,其操作部分定义为删除。在你的控制器,你会有一个<$ p

  if($ action =='delete'){

要从数据库中删除所选问题,您将使用

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

详细说明如下:
http://www.codeigniter.com/userguide3/database/query_builder.html#deleting-data

要编辑数据,您将收集表单输入,因此:



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



使用您的错误检查输入值,使用查询构建器(链接到前面)将新数据发布到数据库。



p>

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

我希望以某种方式帮助,如果没有,请在您的问题中更具体。 / p>

您的网站很好运,



最好的祝福,



保罗


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

解决方案

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

这篇关于codeigniter:从数据库编辑和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆