简体   繁体   中英

Reusable fieldsets PHP

I need to have about 60 different fieldsets within a form that all contain the same elements. The only difference is the ID being called by my query. The ID will be populating data for me from mysql.

Is there a way to do this more efficiently other than adding 60 fieldsets?

<fieldset>
    <h2>Make Your Pick</h2>
        <?php 

        require_once ('mysql_connect.php');

        $query = "SELECT id, name FROM table WHERE id = 1";
        $result = @mysql_query ($query) or die ('error submitting' .mysql_error());

        echo "<select name='winner' id='winner'><option>Who Will Win?</option>";
        while($drop=mysql_fetch_array($result)){

        //data stored in $drop
        echo "<option value=$drop[id]>$drop[name]</option>";
        }
        echo "</select>";

        ?>
    <input id="insert" type="submit" value="Next" />
</fieldset>

I found the answer!

<?php
for ($i = 1; $i <= 60; $i++) {
echo '<fieldset>
<h2 class="fs-title">Make Your Pick</h2>';
        require_once ('mysql_connect.php');

    $query = "SELECT id, name FROM table WHERE id = $i";
    $result = @mysql_query ($query) or die ('error submitting' .mysql_error());

    echo "<select name='winner' id='winner'><option>Who Will Win?</option>";
    while($drop=mysql_fetch_array($result)){

    //data stored in $drop
    echo "<option value=$drop[id]>$drop[name]</option>";
    }
    echo "</select>";

echo '<hr>
<input id="insert" type="button" name="next" class="next action-button" value="Next" />
</fieldset>';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM