第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法顯示與另一個表的主鍵相關(guān)的外鍵數(shù)據(jù)?

有沒有辦法顯示與另一個表的主鍵相關(guān)的外鍵數(shù)據(jù)?

PHP
手掌心 2022-10-28 16:11:11
我正在制作一個測驗應用程序后端模塊。我正在嘗試編輯問題和答案,如何編輯問題并且該相關(guān)問題的答案將顯示?我有兩張桌子questions和answers一張桌子。問題表結(jié)構(gòu)id|question答案表結(jié)構(gòu)id| answer|question_id所以question必須連接表才能question_id獲取它的答案?,F(xiàn)在我有這個錯誤,如果我點擊任何我想編輯的問題會顯示所有答案,即使該特定答案與問題無關(guān)。應用程序的錯誤或問題,請參閱這個控制器將調(diào)用模型public function editPost(){    $result = $this->post_model->showEditPost();    echo json_encode($result);}該模型查詢它以顯示數(shù)據(jù)。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 - 這個腳本顯示模態(tài)數(shù)據(jù)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');            }        });}此代碼觸發(fā) 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>
查看完整描述

1 回答

?
BIG陽

TA貢獻1859條經(jīng)驗 獲得超6個贊

替換這 2 個 javascript 函數(shù),類型參數(shù)應該是get或post在你的$.ajax({type:method_here})


將以下內(nèi)容添加到您的 ajaxsuccess:function()


var html = '';

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

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

}

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

1. 函數(shù) ShowAllQuestions


function showAllQuestions(){

    $.ajax({

        type: 'post',

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

        async: false,

        dataType: 'json',

        success: function(data){

            var html = '';

            var i;

            var n=1;


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

                html +='<div class="card">'+

                    '<div class="card-header" style="color:white; background-color:black">'+

                        '<h4>Question</h4>'+

                    '</div>'+

                    '<div class="card-body">'+

                        '<form>'+

                            '<div class="form-group">'+

                                '<label for="exampleFormControlTextarea1"><h5> Question: </h5></label>'+

                                '<textarea class="form-control" name="question" id="question" rows="8">'+data[i].question+'</textarea>'+

                            '</div>'+

                            '<hr>'+

                            '<a href="javascript:;" class="btn btn-info item-edit" data-id="'+data[i].id+'">Edit </a>&nbsp;'+

                        '</form>'+

                    '</div>'+

                '</div><br>';

            }


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

        },

        error: function(){

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

        }

    });

}

2.顯示特定問題答案的功能


將問題的 id 發(fā)送到controller以從數(shù)據(jù)庫中獲取相關(guān)答案。data使用codeigniter時不需要通過參數(shù)發(fā)送id 。它允許您將參數(shù)作為 url 段傳遞。


$this->uri->segment(number_here)可以幫助您實現(xiàn)這一目標。number_here 將替換為您想要獲得的實際段數(shù)。


uri->segment你可以從這里了解更多


//edit

$('#showdata').on('click', '.item-edit', function(){

    var id = $(this).data('id');


    $('#myModal').modal('show');

    $('#myModal').find('.modal-title').text('Edit Question');

    $('#myForm').attr('action', '<?php echo base_url() ?>posts/updatePost');

    $.ajax({

        type: 'POST',

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

        async: false,

        dataType: 'json',

        success: function(data){

            console.log(data);

            var html = '';

            for(var 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 Edit Data');

        }

    });

});

獲取控制器內(nèi)部的 id 而不是模型


public function editPost(){

    $id = $this->uri->segment(3) //if you don't know about uri->segment learn from user_guide

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

    echo json_encode($result);

}

你的模型應該是


public function showEditPost($id){

    // Show answers

    $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;

    }

}


查看完整回答
反對 回復 2022-10-28
  • 1 回答
  • 0 關(guān)注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號