Jump to content


Photo

Complex MySQL query


  • Please log in to reply
5 replies to this topic

#1 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 25 October 2006 - 02:35 PM

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?

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>';
  }

Any ideas on how i would handle this form?

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 25 October 2006 - 02:40 PM

The name of the inputs should be player[], which will come across as an array named $_POST['player']. You can then loop through the array and run your SQL statements.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 25 October 2006 - 02:46 PM

The name of the inputs should be , which will come across as an array named $_POST['player']. You can then loop through the array and run your SQL statements.


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

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



#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 25 October 2006 - 02:49 PM

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

P.S. Take the spaces out of the pre tags.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 25 October 2006 - 03:35 PM

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

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

    [submit2] => Next
)

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

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 25 October 2006 - 03:47 PM

foreach ($_POST['player'] as $key => $value) {
  echo "Code to process $value<br/>";
}

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users