Jump to content

Archived

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

Perad

Complex MySQL query

Recommended Posts

I have come up with a loop which generates a form. I am pretty stunned that it worked first time 8).

Anyway, i need to transfer data from this form into a database. Because the form size is constantly varying i don't know where i would start. Would i need to use a loop to generate a mysql command?

[code]Heres my loop to generate the form.
if ($players !== 0) {
echo '<form id="form2" name="form2" method="post" action="">';
echo 'UNC<br /><br />';
    for ($i=0;$i<$players;$i++) {
if (($players/2)==$i) {
echo '<br /><br />Opponent<br />Player:<input type="text" name="player'.$i.'" size="35" maxlength="35" /><br />';
} else {
      echo 'Player:<input type="text" name="player'.$i.'" size="35" maxlength="35" /><br />';
}
    }
echo '</form>';
  } [/code]

Any ideas on how i would handle this form?

Share this post


Link to post
Share on other sites
The name of the inputs should be[tt] player[][/tt], which will come across as an array named[tt] $_POST['player'][/tt]. You can then loop through the array and run your SQL statements.

Share this post


Link to post
Share on other sites
[quote author=effigy link=topic=112677.msg457413#msg457413 date=1161787209]
The name of the inputs should be[tt] [/tt], which will come across as an array named[tt] $_POST['player'][/tt]. You can then loop through the array and run your SQL statements.
[/quote]

Could you explain this more, do you mean...

player[<input type="text" name="player'.$i.'" size="35" maxlength="35" />]

Share this post


Link to post
Share on other sites
Change all of your inputs to [tt]<input type="text" name="player[]" size="35" maxlength="35" />[/tt] and add [tt]echo '< pre>', print_r($_POST, 1), '< /pre>';[/tt] to the submission page. Then submit and see what happens.

P.S. Take the spaces out of the pre tags.

Share this post


Link to post
Share on other sites
Sorry to take so long, until you editted it your post didn't make a great deal of sense :)

It comes back with
[code]Array
(
    [player] => Array
        (
            [0] => 1
            [1] => 2
        )

    [submit2] => Next
)[/code]

1 and 2 being the values which i put in. How would i incorporate this into a loop?

Share this post


Link to post
Share on other sites
[code]
foreach ($_POST['player'] as $key => $value) {
  echo "Code to process $value<br/>";
}
[/code]

Share this post


Link to post
Share on other sites

×

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.