实施题号递增

时间:2019-07-02 07:41:41

标签: php codeigniter

我要使每个问题编号都从1开始。当前,问题编号基于数据库中的相应编号。如何使它从1开始?

<body>

    <div class="container">

        <h1> Quiz</h1>


         <?php echo form_open('user/resultdisplay/', ['class' =>'form-horizontal']);?>


        <?php if(count($questions) > 0):?>
            <?php foreach($questions as $row):?>

            <p><?=$row->ch_id?>. <b><?=$row->ques?></b></p>

            <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$row->ch_des1?>"> <?=$row->ch_des1?><br>
            <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$row->ch_des2?>"> <?=$row->ch_des2?><br>
            <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$row->ch_des3?>"> <?=$row->ch_des3?><br>
            <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$row->ch_des4?>"> <?=$row->ch_des4?><br> 

            <?php endforeach;?>
        <?php else:?>
                <tr>
                    <td>No records found!</td>
                </tr>
        <?php endif;?>
    </body>

            <input type="submit" value="Submit!">

        </form>

    </div>

    </body>

1 个答案:

答案 0 :(得分:0)

添加计数器并手动增加计数器:

<?php if(count($questions) > 0):?>
    <?php $index = 1; ?>
    <?php foreach($questions as $row):?>

    <p><?=$index++?>. <b><?=$row->ques?></b></p>

    <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$row->ch_des1?>"> <?=$row->ch_des1?><br>
    <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$row->ch_des2?>"> <?=$row->ch_des2?><br>
    <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$row->ch_des3?>"> <?=$row->ch_des3?><br>
    <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$row->ch_des4?>"> <?=$row->ch_des4?><br> 

    <?php endforeach;?>
<?php else:?>
        <tr>
            <td>No records found!</td>
        </tr>
<?php endif;?>