views:

25

answers:

1
//deal with individual form section posts
//-->Faction Name
if(isset($_POST['factionname'])){
    $unsani_faction_name = $_POST['faction'];
    $new_faction_name = str_replace(",", "", $unsani_faction_name);
    $faction_name = mysql_real_escape_string($new_faction_name);
    $faction_name = preg_replace('/\s\s+/', ' ', $faction_name);//strips excess white space
    $faction_name = stripslashes($faction_name);//strips slashes from name

    //remove special chars except: "& $ £ ^ - ( )"
    $faction_name = preg_replace('/[^a-z0-9\s£&$\^\(\)-]/i', '', $faction_name);  
    $string_length = strlen($faction_name);

    if($string_length < 0 || $string_length > 20) { 
        echo '<strong>Error:</strong> Property name needs to be between 1-20 characters.&nbsp;'; 
    }else { 
        $sql = mysql_query("SELECT * FROM ".TBL_USERPROPBANKS." WHERE prop_name='$prop_name'"); 
        $num_rows = mysql_num_rows($sql);
        if ($num_rows > 0) { 
            echo '<strong>Error:</strong> Bank with the same name in existance.&nbsp;'; 
        }else {  
            mysql_query("UPDATE ".TBL_USERPROPBANKS." SET prop_name='$prop_name' WHERE prop_id='$bankid'"); 
            header("Location: bank_cp.php?bankid=".$bankid."&section=settings");
        }
    }

I'm working out my errors using the above method. What is (in your opinion) the most logical way to:

  1. Counting the number of errors
  2. And echoing/printing them inside a separate section of my layout to show each error message in a list?

All I can think of at the moment is assigning null values to unique vars then filling it with the unique error message if it does not meet my validation requirements (there will be 20+ different errors). Any ideas on this one?

+1  A: 

I would create an empty array onto which I would push the errors and later on simply make a string out of it and display it.

$errors = array();
...
if($string_length < 0 || $string_length > 20) {
  $errors[] = '<strong>Error:</strong> Property name needs to be  between 1-20 characters.&nbsp;';
}
...
if($num_rows > 0) {
  $errors[] = '<strong>Error:</strong> Bank with the same name in existance.&nbsp;';
}

// lower in a place you display the errors
echo implode('<br />', $errors);
mhitza
I'm going to check this method out! I'll get back to you after I've played around with it a bit :)
Callum Johnson
how would I count the number of errors in this array?
Callum Johnson
http://www.php.net/count ...
Brad F Jacobs
thank you for the helpful link!
Callum Johnson
tested, works to my liking. Answer accepted.However, one quick change, implode only accepts two parameters.How do i get something to work like the hypothetical: echo implode(<li align="center">, $errors, </li>);?
Callum Johnson
`echo '<li align="center">'. implode('</li><li align="center">', $errors) .'</li>'` Of course if there is variation you can go on the root of iterating over the elements of the array.
mhitza