有没有办法显示与另一个表的主键相关的外键数据?

我正在制作一个测验应用程序后端模块。我正在尝试编辑问题和答案,如何编辑问题并且该相关问题的答案将显示?

我有两张桌子questionsanswers一张桌子。

问题表结构

id|question

答案表结构

idanswer|question_id

所以question必须连接表才能question_id获取它的答案。

现在我有这个错误,如果我点击任何我想编辑的问题会显示所有答案,即使该特定答案与问题无关。

应用程序的错误或问题,请参阅

这个控制器将调用模型

public function editPost(){

    $result = $this->post_model->showEditPost();

    echo json_encode($result);

}

该模型查询它以显示数据。


public function showEditPost(){

    // Show answers

    $id = $this->input->get('id');


    $this->db->select('*');

    $this->db->from('answers');

    $this->db->join('questions', 'answers.question_id = questions.id');

    $this->db->where('questions.id', $id);

    $query = $this->db->get();

    if($query->num_rows() > 0){

        return $query->result();

    }else{

        return false;

    }

}

Show Edit Post function ajax script - 这个脚本显示模态数据


function showEditPost(){

    $.ajax({

        type: 'ajax',

        url: '<?php echo base_url() ?>posts/editPost',

        async: false,

        dataType: 'json',

        success: function(data){

            var html = '';

            var i;


        for(i=0; i<data.length; i++){

                html +='<input type="text" value="'+data[i].answer+'" class="form-control" /><hr>';

                }


                $('#showEdit').html(html);

            },

            error: function(){

                alert('Could not get Data from Database');

            }

        });

}

此代码触发 Ajax


<div class="modal fade-scale" id="myModal" tabindex="-1" role="dialog">

   <div class="modal-dialog" role="document">

     <div class="modal-content">

       <div class="modal-header">

         <h4 class="modal-title">Modal title</h4>

       </div>

       <div class="modal-body">

       <div class="card">

            <div class="card-header">

                <h4>Update Answers</h4>

            </div>


手掌心
浏览 78回答 1
1回答

BIG阳

替换这 2 个 javascript 函数,类型参数应该是get或post在你的$.ajax({type:method_here})将以下内容添加到您的 ajaxsuccess:function()var html = '';for(var i=0; i<data.length; i++){&nbsp; &nbsp; html +='<input type="text" value="'+data[i].answer+'" class="form-control" /><hr>';}$('#showEdit').html(html);1. 函数 ShowAllQuestionsfunction showAllQuestions(){&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: 'post',&nbsp; &nbsp; &nbsp; &nbsp; url: '<?php echo base_url() ?>posts/showPosts',&nbsp; &nbsp; &nbsp; &nbsp; async: false,&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; success: function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var html = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var n=1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(i=0; i<data.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html +='<div class="card">'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<div class="card-header" style="color:white; background-color:black">'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<h4>Question</h4>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</div>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<div class="card-body">'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<form>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<div class="form-group">'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<label for="exampleFormControlTextarea1"><h5> Question: </h5></label>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<textarea class="form-control" name="question" id="question" rows="8">'+data[i].question+'</textarea>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</div>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<hr>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<a href="javascript:;" class="btn btn-info item-edit" data-id="'+data[i].id+'">Edit </a>&nbsp;'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</form>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</div>'+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</div><br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#showdata').html(html);&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; error: function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('Could not get Data from Database');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}2.显示特定问题答案的功能将问题的 id 发送到controller以从数据库中获取相关答案。data使用codeigniter时不需要通过参数发送id 。它允许您将参数作为 url 段传递。$this->uri->segment(number_here)可以帮助您实现这一目标。number_here 将替换为您想要获得的实际段数。uri->segment你可以从这里了解更多//edit$('#showdata').on('click', '.item-edit', function(){&nbsp; &nbsp; var id = $(this).data('id');&nbsp; &nbsp; $('#myModal').modal('show');&nbsp; &nbsp; $('#myModal').find('.modal-title').text('Edit Question');&nbsp; &nbsp; $('#myForm').attr('action', '<?php echo base_url() ?>posts/updatePost');&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; url: '<?php echo base_url() ?>posts/editPost/' + id,&nbsp; &nbsp; &nbsp; &nbsp; async: false,&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; success: function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var html = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i=0; i<data.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html +='<input type="text" value="'+data[i].answer+'" class="form-control" /><hr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#showEdit').html(html);&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; error: function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('Could not Edit Data');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});获取控制器内部的 id 而不是模型public function editPost(){&nbsp; &nbsp; $id = $this->uri->segment(3) //if you don't know about uri->segment learn from user_guide&nbsp; &nbsp; $result = $this->post_model->showEditPost($id);&nbsp; &nbsp; echo json_encode($result);}你的模型应该是public function showEditPost($id){&nbsp; &nbsp; // Show answers&nbsp; &nbsp; $this->db->select('*');&nbsp; &nbsp; $this->db->from('answers');&nbsp; &nbsp; $this->db->join('questions', 'answers.question_id = questions.id');&nbsp; &nbsp; $this->db->where('questions.id', $id);&nbsp; &nbsp; $query = $this->db->get();&nbsp; &nbsp; if($query->num_rows() > 0){&nbsp; &nbsp; &nbsp; &nbsp; return $query->result();&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP