Jump to content


Photo

Array Help


  • Please log in to reply
7 replies to this topic

#1 V-Man

V-Man
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationVermont

Posted 13 May 2006 - 02:10 AM

Well, I am playing with variables, and I need some help.

        $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>");

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.

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

rtfm :)

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 13 May 2006 - 02:29 AM




 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>"); 
Try that ok

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 V-Man

V-Man
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationVermont

Posted 13 May 2006 - 02:33 AM

[!--quoteo(post=373389:date=May 12 2006, 10:29 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ May 12 2006, 10:29 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
 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>"); 
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
rtfm :)

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 13 May 2006 - 02:41 AM

<?       

$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>");
 }
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 13 May 2006 - 02:44 AM

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]



#6 V-Man

V-Man
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationVermont

Posted 13 May 2006 - 02:49 AM

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?
rtfm :)

#7 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 13 May 2006 - 03:10 AM


$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>");


#8 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 13 May 2006 - 03:17 AM

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.

...

$num_errs = count(array_filter($error, create_function('$value', 'return !empty($value);')));

echo "<p>There were $num_errs errors processing your submission.</p>";

...

EDIT:

emehrkay, the count won't be correct because you have the subtraction happening inside the 'if' statement. Try this:
$count = (int) 0;
$errors = '';
foreach($error as $value) {
    if (!empty($value)) {

        $errors .= $value . "<br />\n";
        $count++;
    }
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users