Jump to content

cjtemple

New Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by cjtemple

  1. Stefany93 I like your $error array approach in the past I would just append a string to the error message but I think I will go with your approach from now on. Also I haven't tested it but I wonder if !empty(trim($_POST['field_name'])) would be effective in testing to ensure the required field has something there?
  2. Thank you for spotting the typos, much appreciated. I modified some of my underlining requirements (most notably is I don't want two values that are equal at or plus one if the index is an odd number) and took a different approach that seams to be much more efficient/buggy <?php $csLength = 110; $reflectorIn = range(1,$csLength); $reflectorOut = array(); shuffle($reflectorIn); echo "<br ><br >Reflector: \n"; generateReflector(); function generateReflector() { global $reflectorIn, $reflectorOut; $clone = $reflectorIn; $resultOut = array(); for($i = 0; $i < count($reflectorIn); $i++) { while (true) { $newKey = array_rand($clone); $newValue = $clone[$newKey]; if($reflectorIn[$i]!=$newValue) { if($i+1<count($reflectorIn)) { if((($i+1)%2) ==1) { array_push($reflectorOut,$newValue); break; } else { if($reflectorIn[$i+1]!=$newValue) { array_push($reflectorOut,$newValue); break; } continue; } } else { array_push($reflectorOut,$newValue); break; } } else { continue; } } unset($clone[$newKey]); } for($i = 0; $i < count($reflectorIn); $i+=2) { array_push($resultOut, $reflectorOut[$i].",".$reflectorOut[$i+1]); } echo "<br />Input: \n"; for($i = 0; $i < count ($reflectorIn); $i++) { echo "<item>".$reflectorIn[$i]."</item>\n"; } echo "<br />Output: \n"; for($i = 0; $i < count($resultOut); $i++) { echo "<item>".$resultOut[$i]."</item>\n"; } } ?> One additional note I need to swap my $reflectorOut and $resultOut arrays. The $resultOut needs to be just a temporary array, but the way I am using them above is backwards.
  3. You know I noticed the typo else where, I will have to poor through it a bit to look for the others, and I bet it will dramatically effect the results.
  4. When doing form validation I like to take the following approach (most of what follows is pseudo code) : Setup a boolean variable to toggle if any validation fails $allValid = true; if(validation fails) { $allValid = false; } Then prior to making your database connection and inserting data check the all valid variable if($allValid) { // connect to db, create query, execute query } As for checking the values you could if($_POST['companyname']==null || $_POST['companyname']=="") { $allValid = false; } You should also ensure the user is not attempting to inject any malicious code.
  5. I have 2 arrays that I create using range from 1 to 110, I then shuffle them to mix them up. The next part is that I want to make sure that if you go through the arrays the values in array 1 are not in the same place in array 2 (plus or minus 1). Here is what I have but it isn't working at all. $csLength = 110; $cs = range(1,$csLength); $reflectorIn = range(1,$csLength); $reflectorOut = range(1,$csLength); shuffle($cs); shuffle($reflectorIn); shuffle($reflectorOut); function generateReflector() { global $reflectorIn, $reflectorOut; $resultOut = array(); for($i = 0; $i < count($reflectorIn); $i++) { testOutput($i, $reflectorOut[$i]); } for($i = 0; $i < count($reflectorIn); $i+=2) { array_push($resultOut, $reflectorOut[$i].",".$reflectorOut[$i+1]); } echo "<br />Input: \n"; for($i = 0; $i < count ($reflectorIn); $i++) { echo "<item>".$reflectorIn[$i]."</item>\n"; } echo "<br />Output: \n"; for($i = 0; $i < count($resultOut); $i++) { echo "<item>".$resultOut[$i]."</item>\n"; } } function testOutput($currentPosition, $currentValue) { global $reflectorIn, $reflectorOut; if($currentPosition != 0) { if($refelctorIn[$currentPosition-1] == $currentValue) { swapOutput($currentPosition, $currentValue); } } if($currentPosition < count($reflectorOut)) { if($reflectorIn[$currentPosition+1] == $currentValue) { swapOutput($currentPosition, $currentValue); } } if($refelectorIn[$currentPosition] == $currentValue) { swapOutput($currentPosition, $currentValue); } } function swapOutput($currentPosition, $currentValue) { global $reflectorIn, $reflectorOut; $endSwap = false; while(!$endSwap) { $keys = array_rand($reflectorOut); $newKey = $keys[0]; $newValue = $reflectorOut[$newKey]; if($newKey!=0) { if($reflectorIn[$newKey-1]==$currentValue) { continue; } if($reflectorIn[$newKey-1]==$newValue) { continue; } } else if($newKey<count($reflectorOut)) { if($reflectorIn[$newKey+1]==$currentValue) { continue; } if($reflectorIn[$newKey+1]==$newValue) { continue; } } else if($reflectorIn[$currentPosition]==$currentValue) { continue; } else { $reflectorOut[$newKey] = $currentValue; $reflectorOut[$currentPosition] = $newValue; break; } } }
×
×
  • 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.