Jump to content

[SOLVED] Simple validation function makes PHP to stop respond


nvee

Recommended Posts

Well, I will be dead honest with you, this is the first time I have ever used an array (accept for mysql_fetch_array) so I was quite chuffed with my idea to make my "empty validation" work ... and the code appears fine but when I do a test PHP fails to respond and gives me a time-out error.

 

The code:

 

$errors = array($tradename,$regname);
			echo "<ul>";
			while($value = $errors) {
				if(empty($value)) {
					echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>";
				}
			}
			echo "</ul>";
		}

 

I think its simple enough, the variables in the array is variables posted from the form which is declared earlier in the script. I however use a function to declare the variables (yes I know, why right a function to purely do a $_POST["value"];, but I am trying to be as professional with my code now as I can be :)

 

Any ideas why PHP would crash on my code? ;)

 

Link to comment
Share on other sites

Is your function coded to use passing by value, reference or used global?

 

And does it return the values.

 

I think the problem may be that value is not in scope when you reffer to it - please show full code...

Link to comment
Share on other sites

You have an infinite loop there. If you want to loop through an array like that you have to set $value to the current element that's pointed to by the internal array pointer using current(), then every loop move that pointer using next() Like this:

 

while($value = current($errors))
{
     // Do whatever you want with $value here
     next($errors);
}

 

Alternatively here are some other examples on how to loop through arrays.

 

for($i = 0;$i < count($errors);$i++)
{
     // $errors[$i];
}

 

foreach($errors as $key => $val)
{

}

 

Or even something like:

 

for(;$value = current($errors);next($errors))
{
// Do something with $value
}

Link to comment
Share on other sites

You can also use list and each with a while loop:

 


while ( list($value) = each($errors) )
{
   if ( empty($value) )
      echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>\n\r";
}

 

 

Link to comment
Share on other sites

Thank you for all your comments...

 

Alexwd: Thank you for explaining the array situation to me. I never thought of the concept that the array does not "auto pass" to the next value, so logically speaking it would go on to the next value. I however tried your option using current and next, but:

 

* When there is nothing entered in the fields (empty fields) then still does not return the list error, but atleast does not crash the script

* However, when I enter values, it crashes the script (weird ... )

 

Andy-H: I want to correct myself by saying that the code was not an function yet (but I plan to make it one) so apologise for the misinformation. Your second post came the closest so far, when there is nothing in the fields, it returns the list error, but now when there is something in the field, it still returns the error. I did a var_dump on the array and the variables has definately been entered in the array, so the array definately is not empty. Any ideas why it would do this?

 

Logically speaking, the simplest way to do this i guess is to not use a while and do all validation manually, but I am so eager to learn new ways (and easier ways) that I really want things like this to work.

 

Thank you for your prompt and quick responses :)

Link to comment
Share on other sites

I also tried this, but this appears to do just the opposite. Irrelevant if I enter values in the fields or not, it does not give me the error message:

 

$errors = array($tradename,$regname);
			echo "<ul>";
			for($i = 0;$i < count($errors);$i++)
				{
				   if (empty($errors) ) {
					  echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>";
				}
			}

Link to comment
Share on other sites

 


while ( list($value) = each($errors) )
{
   if ( !isset($value) || strlen($value) === 0 )
   {
      echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>  ";

      // remove the next line if you wish to display the error once per empty value
      break;
   }
}

Link to comment
Share on other sites

I also tried this, but this appears to do just the opposite. Irrelevant if I enter values in the fields or not, it does not give me the error message:

 

$errors = array($tradename,$regname);
			echo "<ul>";
			for($i = 0;$i < count($errors);$i++)
				{
				   if (empty($errors) ) {
					  echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>";
				}
			}

The reason for this is because you'll need to pass $errors[$i], passing just $errors into empty will be checking the $errors array, and not specific elements.

Link to comment
Share on other sites

Alex, sorry for previous post, you posted the code while I sent that message. Your reply worked, I needed to set the $errors[$i]

 

Thank you soo much to both for your assistance. I have also never used a for() loop, as I have always gotten away with while() - It would appear that the problem has been the actual variable with if(empty($value)) where $value never picked up the actual values within the array like $value[""] - How would I go about doing this with a while loop? I know the topic is solved, but I would really like to get a grip on arrays. (as you can see I am still very new to php)

 

while($value = current($errors)) {
// Specifically with the following line, how would I complete the $value within a while, or must I just do it like $value["0"]; which will then self populate the rest as the while is running?
if(empty($value[""]) {
echo "do something ...";
next($errors);
}

}

Link to comment
Share on other sites

if you are taking the current() next() method, doing the following should work

while($value = current($errors)) {
// Specifically with the following line, how would I complete the $value within a while, or must I just do it like $value["0"]; which will then self populate the rest as the while is running?
if(empty($value) {
echo "do something ...";
}
next($errors);
}

 

this is because you store the current value of the array into the variable $value. Basically, all that current does is return whichever element the internal array pointer is pointing at (read the php manual for more info on how this works) and next changes the internal pointer to the next element

Link to comment
Share on other sites

Hi Mikesta707

 

Thanks for your reply, and yes, it makes perfect sense now. I think the reason why it did not work earlier was that I actually had the next($errors); within the if statement in the while loop as appose to the outside of it. I may be wrong, but hell, it should work and logically speaking, the for() solution given earlier works, so im done fiddling around with this.

 

Thank you all for your help with such a minor task, now for the function ;)

Link to comment
Share on other sites

Actually no, mikesta707, there's a reason it will not work. empty($value) would never even get a chance to run, because if $value was empty, the while statement wouldn't evaluate to true in the first place. Making this an ineffective way to loop through an array that may contain empty values.

Link to comment
Share on other sites

mmmm ... okay, so if I understand you correctly, if no value was passed (meaning the array fields are empty) the while statement would not run because there is no values to run. This makes even more sense :) But out of curiousity, does this not also work with the for the same, as the for says:

 

for($i = 0;$i < count($errors);$i++)

 

if $errors has nothing in it, it should revert back to 0, meaning the for loop should also not run? Or am I mistaken? OR does the for loop work with the actual variables and not if the variables are empty?

Link to comment
Share on other sites

I know the topic is solved, but I have another related question to this. The function sounds simple enough, but I just want to find out how I will be going on about getting all of the variables into the function?

 

From my limited understanding, when declaring a function I need to stipulate which function variables will be going in, and the correct order  -

 

function name($var1, $var2)

 

In my case, the variables will never be the same and never the same amount of variables as it depends on the amount of fields was in the form. Now I know that I will work with an array on this one, so my question is do I bring the actual array in the function or do I set the array outside of the function and then use it as a variable when I use the function e.g.

 

// outside of function

$array = array($var1,$var2,$var3);

FUNCTIONNAME($array);

 

or can I send all the variables into the function and then have the actual array inside of the function?

 

 

Link to comment
Share on other sites

Technically the for statement is just looping through incrementing the variable $i from 0 until it gets to number of the highest element in the array. Here's an example that explains it better:

 

$array = Array('something', 'something else', 'something else');
for($i = 0;$i < count($array);$i++)
{
     echo "$i<br />";
}

 

If you run that you'll notice you get 0, 1, 2 which are the indexes of the array. So if you do:

 

$array = Array('something', 'something else', 'something else');
for($i = 0;$i < count($array);$i++)
{
     echo $array[$i] . '<br />';
}

 

You're getting all the elements of the array, it's the same as:

 

echo $array[0] . '<br />';
echo $array[1] . '<br />';
echo $array[2] . '<br />';

Link to comment
Share on other sites

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.