Jump to content

Array Help


V-Man

Recommended Posts

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]
Link to comment
Share on other sites

[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
Link to comment
Share on other sites

[!--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
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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]
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.