Return to Snippet

Revision: 49524
at July 23, 2011 11:32 by ariellephan


Initial Code
<h2>Enter in 3 numbers</h2>
<p>First 2 is the one you want to check for multiples, 3rd is the number you want to count from 1 to</p>
<form action="fizzbuzz.php" method="post">
	<p>Insert number A <input type="number" name="A" size="5" maxlength="10" value="<?php if (isset($_POST['A'])) echo $_POST['A']; ?>" />(required)</p>
	<p>Insert number B<input type="number" name="B" size="5" maxlength="10" value="<?php if (isset($_POST['B'])) echo $_POST['B']; ?>" />(required)</p>
	<p>Insert number N <input type="number" name="N" size="5" maxlength="10" value="<?php if (isset($_POST['N'])) echo $_POST['N']; ?>" /> (required)</p>
	<p><input type="submit" name="submit" value="Print the series!" /></p>
	<input type="hidden" name="submitted" value="TRUE" />
</form>

<?php

$page_title = 'Fizz Buzz';
          
     // Check if the form has been submitted.          
if (isset($_POST['submitted'])) {

	if (is_numeric($_POST['A']) && is_numeric($_POST['B']) && is_numeric($_POST['N']) ) {
	
		// Print the heading.
		echo '<h1 id="mainhead">This is how it should be</h1>';
	
		$i = NULL; // Initialize $i.
		
	                for ($i=1; $i <=$_POST['N']; $i++)
               { 
              
               if  ( $i%$_POST['A']==0 && $i%$_POST['B'] !== 0 ){echo "F ";} 
                          elseif ( $i%$_POST['B']==0 && $i%$_POST['A'] !== 0 ) {echo "B ";}
                                elseif ( $i%$_POST['A']==0 && $i%$_POST['B'] == 0 ) {echo "FB ";}
                                       else {echo "$i ";}
                }
	
		// Print some spacing.
		echo '<p><br /></p>';
		
	} else { // Invalid submitted values.
		echo '<h1 id="mainhead">Error!</h1>
		<p class="error">Please enter a valid #</p><p><br /></p>';
	}
	
} // End of main isset() IF.
   
?>

Initial URL

                                

Initial Description

                                

Initial Title
FizzBuzz with input from user

Initial Tags

                                

Initial Language
PHP