Jump to content

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());
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.