Jump to content

Hey guys for loop var naming.


Dethman

Recommended Posts

Hey guys I need help doing something like this:

 


<?php
for ($i=0;$i<=15;$i++){
//The above FOR statement will start the code to cycle through the name and gender arrays that we set in the form.

//If there's a name entered, insert the names into the database.

$query = 'insert into players (`GameID`, `name`, `sex`, `score`) values ('$GameID', '$player".$i."', '$sex".$i."', '0')'.$i;

// The Below Code is for the query number to be queried over an over again!
mysql_query ($query."".$i."");
//The below echo statement is here so you can see that the code is working. Delete it when the site is ready to go live.

//end else
} //end for loop

?>

 

Any Help Would be Awesome

Link to comment
https://forums.phpfreaks.com/topic/142018-hey-guys-for-loop-var-naming/
Share on other sites

I may be on the wrong track here but

wouldn't this be easier ?

<?php
foreach($_POST as $P)
{
  if(!empty($P['NAME']))
  {
    mysql_query ("insert into players (`GameID`, `name`, `sex`, `score`) values ('$GameID', '{$P['player']}', '{$P['sex']}', '0')");
  }
}
?>

just to add on to yours to make it easier on the database...

 

<?php
$in = "insert into players (`GameID`, `name`, `sex`, `score`) values";
foreach($_POST as $P)
{
  if(!empty($P['NAME']))
  {
    $in .= ' (\''.$GameID.'\', \''.$player.$i.'\', \''.$sex.$i.'\', \'0\'),';
  }
}
mysql_query(rtrim($in,','))or die(mysql_error());
?>

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.