Jump to content

Incrementing each answer text box


Xtremer360

Recommended Posts

With each question its supposed to increment +1 and its not doing so. Its only showing 1 for each.

 

<?php 
                            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                                $i = 1
                            ?>
                                <dl>
                                
                                    <dt style="width: 190px;"><label for="answer[<?php echo $row['id']; ?>]"><?php echo $row['question'] ?></label></dt>
                                    <dd><input type="text" name="answer<?php echo $i ?>[<?php echo $row['id']; ?>]" class="answers[]" size="54" /></dd>
                                
                                </dl>
                            <?php
                            }
                            ?>

Link to comment
https://forums.phpfreaks.com/topic/240031-incrementing-each-answer-text-box/
Share on other sites

Well, first off you need to define $i with the initial value of 1 outside the loop, otherwise it will reset to 1 every loop. Then you need to increment $i every loop (++$i):

 

<?php 
                            $i = 1;
                            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                            ?>
                                <dl>
                                
                                    <dt style="width: 190px;"><label for="answer[<?php echo $row['id']; ?>]"><?php echo $row['question'] ?></label></dt>
                                    <dd><input type="text" name="answer<?php echo $i ?>[<?php echo $row['id']; ?>]" class="answers[]" size="54" /></dd>
                                
                                </dl>
                            <?php
                            ++$i;
                            }
                            ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.