0

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>
acb_tld
  • 75
  • 1
  • 5
  • Have you heard of a [`for` loop](http://php.net/manual/en/control-structures.for.php)? – Bart Friederichs Oct 02 '13 at 19:22
  • Why can't you put the `while` around the `
    `?
    – Explosion Pills Oct 02 '13 at 19:23
  • 1
    (1) Don't use `mysql_*` functions anymore, but MySQLi/PDO with prepared statements. (2) Don't use `@` to suppress errors, they are bugs in disguise. (3) Don't `die` in the middle of code; fail gracefully. – Bart Friederichs Oct 02 '13 at 19:25
  • I'm sort of a newb Bart :) – acb_tld Oct 02 '13 at 19:31
  • I also need the value of the id to change within the fieldset. That's another big issue – acb_tld Oct 02 '13 at 19:34
  • @CBB If you're new to MySQL, please, don't waste any time learning the antiquated `mysql_query` interface. You should, at the very least, be using PDO. Better would be a database layer like [Doctrine](http://doctrine-project.org/) or [Propel](http://propelorm.org/). Even better is a [popular framework](http://codegeekz.com/best-php-frameworks-for-developers/). – tadman Oct 02 '13 at 19:42

1 Answers1

0

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>';
}
?>
acb_tld
  • 75
  • 1
  • 5
  • I'll now use Bart's recommendations in his 2nd comment. Time to learn about prepared statements! – acb_tld Oct 02 '13 at 19:52