如何使用它们的 ID 动态获取 Tabs 和 Pills 中的数据?使用 PHP & 代码点火器

How can I dynamically fetch data in Tabs and Pills using their IDs? Using PHP & Codeigniter

我正在为问题和答案创建一个模块,我需要分别获取数据。

我创建了一个选项卡,我将在其中单击我要回答的问题,它的子项或答案应该匹配。

为了视觉参考,这是我的问题

I have this data that are all fetched, which should not be. That is why I created Tabs and Pills so I can separate questions and answers

1213是每条数据的id号

所以在 Tabs and Pills 中,我得到了这个

The problem of this, is that I cannot fetch the data dynamically, it just shows ID 13 data for question and answer. How can I change this dynamically so I can click back and forth the tabs and show the right data by its id?

I need to fetch the ID 12 of the 1st Tab

这是我的观点

<ul class="nav nav-tabs" role="tablist">
        <?php foreach($questions as $question){ ?>
            <?php echo $question->id; ?>

            <li class="nav-item">
                <a class="nav-link" data-toggle="tab" href="#<?php echo $question->id ?>" role="tab"><h4><?php echo $question->question ?></h4></a>
            </li>
        <?php } ?>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
        <div class="tab-pane" id="13" role="tabpanel">

        <div class="card" style="border:2px solid black;">
            <div class="card-body">
                    <?php foreach($this->question_model->findAnswersByQuestion($question->id) as $answer){ ?>
                        <?php if($answer->type_id==0): ?>

                                <input type="radio" name="question_<?php echo $question->id; ?>" value="<?php echo $answer->answer ?>" required/>
                                <?php echo $answer->answer; ?><hr>


                        <?php endif; ?>

                        <?php if($answer->type_id==1): ?>
                            <div class="input-group input-group-lg">
                                <input type="text" class="form-control col-md-6" placeholder="Enter Answer" name="question_<?php echo $question->id; ?>" required/>
                            </div>
                        <?php endif; ?>

                    <?php } ?>
            </div>
        </div><br>

        </div>
</div>

修复选项卡结构,循环遍历 nav-item 的问题,但不循环遍历 tab-pane,这就是为什么单击选项卡 link 没有任何反应的原因。

<ul class="nav nav-tabs" role="tablist">
    <?php foreach($questions as $question){ ?>
    <li class="nav-item">
        <a class="nav-link" data-toggle="tab" href="#tab-<?php echo $question->id ?>" role="tab"><h4><?php echo $question->question ?></h4></a>
    </li>
    <?php } ?>
</ul>

<!-- Tab panes -->
<div class="tab-content">
    <?php foreach($questions as $question){ ?>
    <div class="tab-pane" id="tab-<?= $question->id ?>" role="tabpanel">
        <div class="card" style="border:2px solid black;">
            <div class="card-body">
                <?php foreach($this->question_model->findAnswersByQuestion($question->id) as $answer){ ?>

                    <!-- form input $answer / item -->

                <?php } ?>
            </div>
         </div>
     </div>
     <?php } ?>
</div>