Simple php Array if question

Joined: 11/28/2008

I'm getting confused and frusterated on this,
so I'm just going to ask

I have a bunch of $erroor in my script $erroor1 $erroor2 $erroor3 etc,

its for a form,
if the form field isnt filled out on each part it will have $erroor1 to say yes

and at the end of the script i want it to

if $erroor(any of them) == yes

then do something

does this make sense?

Joined: 10/18/2008
$erroor is an array? You

$erroor is an array? You could use

if (count($error) > 0))

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
an example

<?php
@extract($_POST);
include("emailfun.php");
$name = nl2br($name);
$email = nl2br($email);
$nick = nl2br($nick);
$onick = nl2br($onick);
$ref1n = nl2br($ref1n);
$ref1e = nl2br($ref1e);
$ref2n = nl2br($ref2n);
$ref2e = nl2br($ref2e);
$ref3n = nl2br($ref3n);
$ref3e = nl2br($ref3e);

if ($name == "" )
{
$namee123 = "This field can not be empty.";
$erroor1 = "yes";
}
else
{
$namene = $name;
$namee123 = "";
$erroor1 = "no";
}

if (check_email_address($email)) {

$emailne = $email;
$emaile123 = "";
$erroor2 = "no";

} else {
$emaile123 = "Please Provide An EMail Address";
$erroor2 = "yes";
}

if ($nick == "" )
{
$nicke123 = "This field can not be empty.";
$erroor3 = "yes";
}
else
{
$nickne = $nick;
$nicke123 = "";
$erroor3 = "no";
}

if ($onick == "" )
{
$onicke123 = "This field can not be empty.";
$erroor4 = "yes";
}
else
{
$onickne = $onick;
$onicke123 = "";
$erroor4 = "no";
}

if ($ref1n == "" )
{
$ref1ne123 = "This field can not be empty.";
$erroor5 = "yes";
}
else
{
$ref1nne = $ref1n;
$ref1ne123 = "";
$erroor5 = "no";
}

if ($ref1e == "" )
{
$ref1ee123 = "This field can not be empty.";
$erroor6 = "yes";
}
else
{
$ref1ene = $ref1e;
$ref1ee123 = "";
$erroor6 = "no";
}

if ($ref2n == "" )
{
$ref2ne123 = "This field can not be empty.";
$erroor7 = "yes";
}
else
{
$ref2nne = $ref1n;
$ref2ne123 = "";
$erroor7 = "no";
}

if ($ref2e == "" )
{
$ref2ee123 = "This field can not be empty.";
$erroor8 = "yes";
}
else
{
$ref2ene = $ref1e;
$ref2ee123 = "";
$erroor8 = "no";
}

if ($ref3n == "" )
{
$ref3ne123 = "This field can not be empty.";
$erroor9 = "yes";
}
else
{
$ref3nne = $ref1n;
$ref3ne123 = "";
$erroor9 = "no";
}

if ($ref3e == "" )
{
$ref3ee123 = "This field can not be empty.";
$erroor10 = "yes";
}
else
{
$ref3ene = $ref1e;
$ref3ee123 = "";
$erroor10 = "no";
}

at the end if any of the $erroor is yes I am going to $include it back to the form

Joined: 10/18/2008
Why not just use an array?

Why not just use an array?

$erroor = array();

if ($name == "" ){
  array_push($erroor, "This field can not be empty.");
}else{
  $namene = $name;
}

/* <!-- etc --> */

if ($ref3e == "" ){
  array_push($erroor, "This field can not be empty.");
}else{
  $ref3ene = $ref1e;
}

// Check to see if there are any errors
if (count($erroor) > ){
  // print out all the error issues:
  print "<ul>";
  foreach($erroor as $erroor_item){
    print "<li>" . $erroor_item . "</li>";
  }
  print "</ul>";
}

You could also use the key as a field identifier:

// instead of: array_push($erroor, "This field can not be empty.");
// use:

$erroor['Email Field'] = "This field can not be empty.";

// Then to print it out:
if (count($erroor) > ){
  // print out all the error issues:
  print "<ul>";
  foreach($erroor as $fieldname => $erroor_item){
    print "<li>" . $fieldname . ": " . $erroor_item . "</li>";
  }
  print "</ul>";
}

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
this is what i put in, trying

this is what i put in,
trying to figure this out,
i get an error on line 22

Parse error: syntax error, unexpected ')' in /home/ircmay/public_html/tyler/procoper.php on line 22

<?php
@extract($_POST);
include(
"emailfun.php");
$name = nl2br($name);
$email = nl2br($email);
$nick = nl2br($nick);
$onick = nl2br($onick);
$ref1n = nl2br($ref1n);
$ref1e = nl2br($ref1e);
$ref2n = nl2br($ref2n);
$ref2e = nl2br($ref2e);
$ref3n = nl2br($ref3n);
$ref3e = nl2br($ref3e);


if (
$name == "") {
array_push($error, "This field can not be empty.");
}else{
$namene = $name;
}

if (
count($erroor) > ){
include(
"operapp.php");
}else{
include(
"mailoperapp.php");
}
?>

usually the error is on the line or just above it, I can't see it sorry :/

Joined: 10/18/2008
Opp, you got that from my

Opp, you got that from my code. This line:

if (count($erroor) > ){

needs to be greater than 0.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
its almost getting there. I'm

its almost getting there.

I'm getting the error

Warning: array_push() [function.array-push]: First argument should be an array in /home/ircmay/public_html/tyler/procoper.php on line 19
sent

<?php
@extract($_POST);
include(
"emailfun.php");
$name = nl2br($name);
$email = nl2br($email);
$nick = nl2br($nick);
$onick = nl2br($onick);
$ref1n = nl2br($ref1n);
$ref1e = nl2br($ref1e);
$ref2n = nl2br($ref2n);
$ref2e = nl2br($ref2e);
$ref3n = nl2br($ref3n);
$ref3e = nl2br($ref3e);


$erroor = array();

if (
$name == "") {
array_push($error, "This field can not be empty.");
}else{
$namene = $name;
}

if (
count($erroor) > 0){
include(
"operapp.php");
}else{
include(
"mailoperapp.php");
}
?>

operapp.php is the form itself
mailoperapp is the function that actually emails it out

Joined: 10/18/2008
$error should be $erroor if

$error should be $erroor

if ($name == "") {
array_push($error, "This field can not be empty.");
}else{
$namene = $name;
}

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.