Jump to content

Having an endless loop. What I'm I doing wrong?


oeroesmoeloevoeloe

Recommended Posts

Hi all,

 

I'm trying to make a method that makes it easier for me to generate errors when using forms. The main idea is that I call a static method to which I give an array which contains 1. The text entered by the user 2. The error message that will be given when the entered text is empty.

 

The method will then return a list of errors, or it will return an empty string. If the string is empty I know that the user filled in everything needed.

 

My form posts to itself and looks like this :

 

<form method="POST" action="admin.php">
	<table class="tableForDiv" cellpadding="3">
		<tr class="tblRowHeader">
			<td colspan="2">Nieuws</td>
		</tr>
		<tr>
			<td colspan="2">
				<label class="alignTopLeft" for="newsMessage"><strong>Nieuwsbericht</strong></label>
				<textarea cols="70" rows="3" name="newsMessage"></textarea>
			</td>
		</tr>
		<tr>
			<td class="fillSpaceInAdminNewsForm"></td>
			<td>
				<input type="submit" name="submitNews" value="Post bericht">
				<input type="reset" name="reset" value="reset">
			</td>
		</tr>
	</table>
</form>

 

Above the form I have this php code where I try to check if the textarea 'newsMessage' has any text. I'm using an array (correct way?) to store the values.

 

<?php
if (isset($_POST['submitNews']))
{
	$message = $_POST['newsMessage'];
	$message = mysql_real_escape_string($message);

	$fieldarray[0] = $message;
	$fieldsarray[1] = "Geef een berichttekst op"; 

	$errorstring = error_builder::give_error_string($fieldarray);

	if(!empty($errorstring))
	{
		echo $errorstring;
	}
}	
?>

 

and finally my php class :

 

<?php

class error_builder
{
public static function give_error_string($array)
{
	$noErrors=true;	
	$errorString = "Gelieve het volgende te corrigeren : 
					<div class='divLoginErrors'><ul>";

	$fieldarray = array($array);
	for($i = 0; $i<=count($fieldarray); $i+2)
	{
		if(empty($fieldarray[$i]))
		{
			$message = $fieldarray[$i+1];
			$errorString.="<li>$message</li>";			
			$noErrors = False;
		}
	}
	if(!$noErrors)
	{
		$errorString.="</ul></div>";
	}
	else
	{
		$errorString="";
	}
	return $errorString;
}
}

?>

 

I'm doing $i+2 everytime to go to the next field. I know there is probably a better way of doing this (double array I guess?) but I'm new to php programming and am not really sure how to do it ;)

 

When I run all of the above code, I think I generate an andless loop. I see the wait cursor forever when I press the submit button.

 

When i put the for loop in comments all goes well, so I guess I'm creating some kind of endless loop? Or does it have anything to do with the static method I'm trying to call?

 

I hope you guys can help me! If you need anything else, please ask and I'll post it.

 

thank you!

 

fast responses wow :P

 

but indeed, adding the  $i+=2 seems to have solved the loop problem, but I don't get any output atm.

 

it seems that this code does not output anything.

 

if(empty($fieldarray[$i]))
		{
			$message = $fieldarray[$i++];
			$errorString.="<li>$message</li>";	
			echo "$i";	
			$noErrors = False;
		}

 

is my reasoning wrong for empty($fieldarray[$i]) ?

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.