V-Man Posted May 13, 2006 Share Posted May 13, 2006 Well, I am playing with variables, and I need some help.[code] $error = array( "fname_err" => $fname_err, "lname_err" => $lname_err, "email_err" => $email_err, "type_err" => $type_err, "message_err" => $message_err); $error_ech = ($error['fname_err'] || $error['lname_err'] || $error['email_err'] || $error['type_err'] || $error['message_err']); $num_errs = count($error); echo("<p>There were ".$num_errs." errors processing your submission.</p>"); echo("<font color=\"red\" size=\"2\">"); echo($error_ech); echo("</font>");[/code]What I want to happen, is tell the number of errors, assuming the variable has something in it (theyre defined above this piece of code as NULL) and then output the errors that DO have values. Please help. This is all I get so far.[code]There were 5 errors processing your submission. -- Says this even when fields with no errors are filled in..1 -- this is all it says, ever... this should the errors them selves...[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 13, 2006 Share Posted May 13, 2006 [code] Well, I am playing with variables, and I need some help.CODE $error = array( "fname_err" => $fname_err, "lname_err" => $lname_err, "email_err" => $email_err, "type_err" => $type_err, "message_err" => $message_err); $error_ech = (!$error['fname_err'] || $error['lname_err'] || $error['email_err'] || $error['type_err'] || $error['message_err']); $num_errs = count($error); echo("<p>There were ".$num_errs." errors processing your submission.</p>"); echo("<font color=\"red\" size=\"2\">"); echo($error_ech); echo("</font>"); [/code]Try that ok Quote Link to comment Share on other sites More sharing options...
V-Man Posted May 13, 2006 Author Share Posted May 13, 2006 [!--quoteo(post=373389:date=May 12 2006, 10:29 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ May 12 2006, 10:29 PM) [snapback]373389[/snapback][/div][div class=\'quotemain\'][!--quotec--][code] Well, I am playing with variables, and I need some help.CODE $error = array( "fname_err" => $fname_err, "lname_err" => $lname_err, "email_err" => $email_err, "type_err" => $type_err, "message_err" => $message_err); $error_ech = (!$error['fname_err'] || $error['lname_err'] || $error['email_err'] || $error['type_err'] || $error['message_err']); $num_errs = count($error); echo("<p>There were ".$num_errs." errors processing your submission.</p>"); echo("<font color=\"red\" size=\"2\">"); echo($error_ech); echo("</font>"); [/code]Try that ok[/quote]Nope. Same thing. I get the same stuff echoed.Try it for your self. www.minvera-fx.com/cvgs/contact.php Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 13, 2006 Share Posted May 13, 2006 [code]<? $error = array( "fname_err" => $fname_err, "lname_err" => $lname_err, "email_err" => $email_err, "type_err" => $type_err, "message_err" => $message_err); $error_ech = (!$error['fname_err'] || $error['lname_err'] || $error['email_err'] || $error['type_err'] || $error['message_err']); $num_errs = count($error); if(!$error){ echo("<p>There were ".$num_errs." errors processing your submission.</p>"); echo("<font color=\"red\" size=\"2\">"); echo($error_ech); echo("</font>"); }?>[/code] Quote Link to comment Share on other sites More sharing options...
toplay Posted May 13, 2006 Share Posted May 13, 2006 V-Man, I recommend that you read up more on basic language constructs.The way you're using $error_ech, it will contain a boolean result. A value of one (1) is when the condition is true and zero (0) when it's false.The function count() calculates how many entries in the array irregardless of any have values specified or not. You have five array indexes and that's why it displays 5.[a href=\"http://us2.php.net/manual/en/function.count.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.count.php[/a] Quote Link to comment Share on other sites More sharing options...
V-Man Posted May 13, 2006 Author Share Posted May 13, 2006 The code was fine, and the script executed fine. The only thing that was not working right was the displaying of the number of errors (I only want the variable counted if it hold a value) and then displaying the actual error.Thanks toplay. I have read about count and arrays. Though I have never used them, I thought I would give them a shot. I can't for the life of me understand or come up with a way to make this work. Shall I post the whole script? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted May 13, 2006 Share Posted May 13, 2006 [code] $error = array( "fname_err" => $fname_err, "lname_err" => $lname_err, "email_err" => $email_err, "type_err" => $type_err, "message_err" => $message_err); $count = count($error);$errors = ""; foreach($error as $key => $value){if($value != ""){$errors . =$value."<br />\n";$count--;}} echo("<p>There were ".$count." errors processing your submission.</p>"); echo("<font color=\"red\" size=\"2\">"); foreach($error as $key => $value){echo $errors; echo("</font>");[/code] Quote Link to comment Share on other sites More sharing options...
toplay Posted May 13, 2006 Share Posted May 13, 2006 Please try and understand what I wrote before.emehrkay, showed you how to display each error using the foreach approach, and the example below is another way of getting the count of how many errors.[code]...$num_errs = count(array_filter($error, create_function('$value', 'return !empty($value);')));echo "<p>There were $num_errs errors processing your submission.</p>";...[/code]EDIT:emehrkay, the count won't be correct because you have the subtraction happening inside the 'if' statement. Try this:[code]$count = (int) 0;$errors = '';foreach($error as $value) { if (!empty($value)) { $errors .= $value . "<br />\n"; $count++; }}[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.