Jump to content

help adding $errorCount and new array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
dcord

dcord

    Newbie

  • Members
  • PipPip
  • 25 posts
I quote

Quote

Immediatly after the validateWord() function, declare and initialize a new variable called $errorCount and a new array called $words[].

here is the code can you tell me if I inserted this variable in the correct position? the last (if)statement doesnt work.

<?php
function displayError($fieldName, $errorMsg) {
 global $errorCount;
 echo "Error for \"$fieldName\": $errorMsg<br />\n";
 ==$errorCount;
}
function validateWord($data, $fieldName) {
 global $errorCount;
 if (empty($data)) {
  displayError($fieldName, "This field is
  required");
  $retval = "";
} else { //Only clean up the input if it isn't
  //empty
  $retval = trim(data);
  $retval = stripslashes($retval);
  if ((strlen($retval)<4) ||
   (strlen($retval >7)) {
    displayError($fieldName, "Words must be
      at least four and at most
      seven letters:);
  }
 }
 $retval = strtoupper($retval);
 $retval = str_shuffle($retval);
 return($retval);
}
$errorCount = 0;
$words = array();
$words[] = validateWord($_POST['Word1'], "Word 1");
$words[] = validateWord($_POST['Word2'], "Word 2");
$words[] = validateWord($_POST['Word3'], "Word 3"); 
$words[] = validateWord($_POST['Word4'], "Word 4");
 if ($errorCount>0)
  echo "Please use the \"Back\" button to
   re-enter the data.<br />\n";
 else {
   $wordnub = 0;
   foreach ($words as $word)
    echo "word ".++$wordnum.": $word<br />\n";
}
 
  
?>

Edited by Alexander, 28 November 2010 - 02:48 PM.
bbcode formatting


#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Please use tags to format your code. I don't see where you increment $errorCount. I guess you have mistake here:

==$errorCount;

instead of

++$errorCount;



#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Also if you look at the formatting I fixed, you'll notice you forget to terminate one of your strings which should stop the script.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
dcord

dcord

    Newbie

  • Members
  • PipPip
  • 25 posts
thank you guys for all your help. I stare at this code for a long time and still miss the mistakes even when I think I have it correct.