Jump to content

Loop


user4fun
 Share

Recommended Posts

I have a form5 textfieldsusername //submitted only once it is the samethen 4 textfieldstxt1txt2txt3txt4after the user submits the infromationit insert the information in a tableproblemsright now

$link = mysql_connect("", "", "") or die ("No connection");   mysql_select_db(" ") or die ("no database");	  $sql = "INSERT INTO submit_entry (entry_id, username, txt, condition)  VALUES (' ', '$username', '$txt1', 'contacted')";  $query=mysql_query($sql);if($query){	echo " ";}else{	echo "<b>ERROR</b>";}$sql = "INSERT INTO submit_entry (entry_id, username, txt, condition)  VALUES (' ', '$username', '$txt2', 'contacted')";  $query=mysql_query($sql);if($query){	echo " ";}else{	echo "<b>ERROR</b>";}

and so on for 4 timesnotice the '$txt' is the only one that changescan i put that in a loop?or is the code ok as is.problem 2how can i make it recognize that the txt2 is empty and it should stop and not bother process the next fields txt3 and txt4incase the user only inserted txt1

Link to comment
Share on other sites

Hi!The easiest way is to rename txt1,txt2 etc to txt[] (all to the same):

<textarea name="txt[]"></textarea>or<input type="text" name="txt[]" />

and then loop thru them (you get $_POST['txt'] as an array):

$txts = $_POST['txt'];foreach( $txts as $txt ) {  $sql = "INSERT INTO submit_entry (entry_id, username, txt, condition)  VALUES (' ', '$username', '$txt', 'contacted')";    $query=mysql_query($sql);  if($query)	  echo " ";  else	  echo "<b>ERROR</b>";}

Note that you should only get those fields that has a value from the browser. If you want to be on the safe side you can add this to the loop:

if (empt($txt))	   break;

It's always a good practice to avoid to the same thing over and over in your code, depending on the case/problem you can use loops (as in your case) or divide the code into functions (or both...)Hope that helped..Good Luck and Don't Panic!

Link to comment
Share on other sites

If you want to put your code in a loop, you can do it like this:

$done = false;$nr = 1;while (!$done){  $var = "txt" . $nr++;  if (isset($$var) && !empty($$var))  {	$sql = "INSERT INTO submit_entry (entry_id, username, txt, condition)  VALUES (' ', '$username', '" . mysql_real_escape_string($$var) . "', 'contacted')";	$query=mysql_query($sql);	if(!$query)	{	  echo "<b>ERROR:</b> " . mysql_error();	  $done = true;	}  }  else	$done = true;}

Link to comment
Share on other sites

If you run the first piece of code from Mr Chisol, name all of your variables txt[]. If you run the piece of code I posted, that code looks for local variables named $txt1, $txt2, $txt3 etc. If you are getting them directly out of $_POST or $_GET, you can use something more efficient then what I wrote. What I wrote is expecting local variables called $txt1, $txt2, etc, because that's what you wrote in your first example.

Link to comment
Share on other sites

This is something like what I do for the query so that you only have one query and not loop the query itself but the values inside it...

$cols= array(..., ..., ..., ....);$data= array(..., ..., ..., ...); // or you can use the methods described above to get your form data into an array (name="text[]" automatically gives you an array) $query= "INSERT INTO $table (";            foreach ($cols as $v) { $query .= "$v, "; }  //Get each $cols (this is an array of the column names in the same order as your data            $query= substr($query, 0, -2);  //This removes the final ', ' which is added in the loop            $query .= ") VALUES (";             for ($i=$start; $i<=$end; $i++) { $query .= "'".$data[$i]."', "; }  //this puts data from the data array           $query = substr($query, 0, -2);  //remove the last ", " from the loop again           $query .= ")"; //add the last ) to the VALUES ( ) to finish the query    

Then I use this as a function and all you do is set the column names and a few parameters and hey presto :)Don't know if that helps but it works for me! :)

Link to comment
Share on other sites

If you wanted to change the code I posted above to get the data from POST, it would be like this:

$done = false;$nr = 1;while (!$done){  $var = "txt" . $nr++;  if (isset($_POST[$var]) && !empty($_POST[$var]))  {	$sql = "INSERT INTO submit_entry (entry_id, username, txt, condition)  VALUES (' ', '$username', '" . mysql_real_escape_string($_POST[$var]) . "', 'contacted')";	$query=mysql_query($sql);	if(!$query)	{	  echo "<b>ERROR:</b> " . mysql_error();	  $done = true;	}  }  else	$done = true;}

That will work for any number of fields in POST labelled txt1, txt2, txt3 etc. It will stop when it finds one that either doesn't exist or is empty.

Link to comment
Share on other sites

You would have to change the initial value of $nr to 0 because when it is first called it is incremented by 1, correct? As is your code would skip txt1 and instead start with txt2.

Link to comment
Share on other sites

No!The increment is done after the value is used (saved in the string in this case), when the operator (++) is after the variable.If the operator is before the variable (++$nr), the increment will be done before the value is used...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...