Jump to content

Help with first php project


MJ0730

Recommended Posts

Hi. Just started learning PHP though a little project I'm working on. I've gotten about as far as I can get on my own. I know there's a lot wrong with the code, so I'll take any help I can get.

The basic idea is a quiz, where two items are picked at random, some of the values are compared. What I have working so far is the randomizing of the items and the comparison. What I have no idea how to do is get data back and forth from the answer form.

$weight = array(1, 2, 3, 4, 5, 6);
$weight_class = array("Small", "Small+", "Large", "Heavy", "Super", "B757");

$dep = array("Full length", "Intersection");
$waive = "No";

$aircraft = array
	(
	array("A300",$cat[2],$weight[4],$dep[0],$weight_class[3]),
	array("A320",$cat[2],$weight[3],$dep[0],$weight_class[2]),
	array("A340",$cat[2],$weight[4],$dep[0],$weight_class[3]),
	array("AC68",$cat[0],$weight[1],$dep[1],$weight_class[0]),
	array("B727",$cat[2],$weight[3],$dep[0],$weight_class[2]),
	array("B737",$cat[2],$weight[3],$dep[0],$weight_class[2]),
	); //this array is cut down for brevity

//need a to function to generate the next question

//Randomize the selection of aircraft
$random_aircraft = array_rand($aircraft, 2);
$lead = $aircraft[$random_aircraft[0]];
$trail = $aircraft[$random_aircraft[1]];

//Check if aircraft can depart from intersection. If it can, randomize $dep_point to 0 or 1.
//Trail
if ($trail[3] === 'Intersection') {
	$random_trail_dep_point = array_rand($dep, 1);
} else {
	$random_trail_dep_point = 0;
}

if ($random_trail_dep_point == 0) {
		$trail_dep_point = 'Full length';
	} else {
		$trail_dep_point = 'Intersection';
	}

//Lead
if ($lead[3] === 'Intersection') {
	$random_lead_dep_point = array_rand($dep, 1);
} else {
	$random_lead_dep_point = 0;
}

if ($random_lead_dep_point == 0) {
		$lead_dep_point = 'Full length';
	} else {
		$lead_dep_point = 'Intersection';
	}


//Comparing the two a/c for time requirement
if ($lead_dep_point === $trail_dep_point) {
	if ($lead[2] > $trail[2]) {
		if ($lead[4] === 'Super' && $trail[4 === 'Super']) {
			$time = 'None';
		} elseif ($lead[4] === 'Super' && $trail[4] === 'Heavy') {
			$time = 3;
		} elseif ($lead[4] === 'Super') {
			$time = 3;
		} elseif ($lead[4] === 'Heavy' && $trail[4] === 'Heavy') {
			$time = 2;
		} elseif ($lead[4] === 'Heavy') {
			$time = 2;
		} elseif ($lead[6] === 'B757' && ($trail[4] === 'Small' || $trail[4] === 'Small+')) {
			$time = 2;
		}
		else {
			$time = 'None';
		}
	}
} elseif ($lead[2] > $trail[2] && $trail_dep_point === 'Intersection') {
	if ($lead[4] === 'Super') {
		$time = 4;
	} elseif ($lead[4] === 'Heavy') {
		$time = 3;
	} elseif ($lead[4] === 'B757' && ($trail[4] === 'Small' || $trail[4] === 'Small+')) {
		$time = 3;
	} elseif ($lead[4] === 'Large' && ($trail[4] === 'Small' || $trail[4] === 'Small+')) {
		$time = 3;
	} elseif ($lead[4] === 'Small+' && $trail[4] === 'Small') {
		$time = 3;
	} else {
		$time = 'None';
	}
} else {
	if ($lead[2] > $trail[2]) {
		if ($lead[4] === 'Super' && $trail[4 === 'Super']) {
			$time = 'None';
		} elseif ($lead[4] === 'Super' && $trail[4] === 'Heavy') {
			$time = 3;
		} elseif ($lead[4] === 'Super') {
			$time = 3;
		} elseif ($lead[4] === 'Heavy' && $trail[4] === 'Heavy') {
			$time = 2;
		} elseif ($lead[4] === 'Heavy') {
			$time = 2;
		} elseif ($lead[6] === 'B757' && ($trail[4] === 'Small' || $trail[4] === 'Small+')) {
			$time = 2;
		}
		else {
			$time = 'None';
		}
	}
}

//this is wrong.. don't know how to get data from the form to check the answer
function check_answer($answer) {
	if ($answer == $time) {
		$feedback = 'Correct';
	} else {
		$feedback = 'The correct answer is ' . $time;
	}
	return $feedback;
}
?>

Here's the html form. its the rough draft, so its not pretty. Note, the checkbox data doesn't do anything yet. That will be the next step once I get everything else sorted.

<body>
<div id="question">
	<p><?php echo $trail[0]; ?> departing from <?php echo $trail_dep_point; ?> behind a <?php echo $lead[0]; ?> at <?php echo $lead_dep_point ?>.</p>
</div>
	
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" name="answer_form">
	<fieldset>
		<legend>Time Requirement</legend>
			<label for="two">Two Minutes</label> <input type="radio" id="two" name="answer" value="2" required>
			<label for="three">Three Minutes</label> <input type="radio" id="three" name="answer" value="3" required>
			<label for="four">Four Minutes</label> <input type="radio" id="four" name="answer" value="4" required>
			<label for="none">None</label> <input type="radio" id="none" name="answer" value="None" required>
	</fieldset>	
	
	<fieldset>
		<legend>Waiveable</legend>
			<label for="waive">Can be waived</label> <input type="checkbox" id="waive" name="waive" value="waive">
	</fieldset>

	<input type="submit" class="big">
	<input type="reset" class="big" value="Clear">
	<input type="button" class="big"  style="float: right;" value="Next"><!--Need to generate next question -->
</form>

<div class="flex_container">
	<div id="answer">
		<p><?php echo $feedback; ?></p> <!--this doesnt work-->
	</div>	

	<div id="feedback">
		<table>
	  <tbody>
		<tr>
		  <th scope="col">&nbsp;</th>
		  <th scope="col">Aircraft</th>
		  <th scope="col">Weight Class</th>
		</tr>
		<tr>
		  <th scope="row">Lead</th>
		  <td><?php echo $lead[0]; ?></td>
		  <td><?php echo $lead[4]; ?></td>
		</tr>
		<tr>
		  <th scope="row">Trail</th>
		  <td><?php echo $trail[0]; ?></td>
		  <td><?php echo $trail[4]; ?></td>
		</tr>
	  </tbody>
	</table>
	</div>	
</div>

</body>

TIA

Link to comment
Share on other sites

Your form needs to direct the action to your script.  Your script needs to check that it does have a POST coming in to it to avoid hackers by doing this:

	if ( !$_SERVER['REQUEST_METHOD'] == 'POST')
	    {
	     echo "invalid response";
	    exit;
	}
	

After getting a POST response you can then go ahead and process the incoming $_POST array elements, using the name attribute of each form item.  If you have multiple submit buttons in your form you may want to assign a name to each so as to distinguish what the user wants the script to do.

Assign the values of the form's text fields to php vars when writing the html.

 

	echo "<input type='text' name='first_name' value='$first_name'>";
	

When you receive the inputs assign the post array values to these PHP vars. 
[php[

$first_name = $_POST['first_name'];

[/php]

If you want to then send the form back to the user simply redisplay your same html contents.    I usually use a php function that outputs my entire html page read up on using 'heredocs' for the page output) and when I  want the script to re-display it I simply call that function and exit, adding an error message to the output with a php variable such as $err_msg.  Make sure you use a global directive for those php vars that are buried in your html.

Hope this gives you some direction.

 

Link to comment
Share on other sites

On 9/15/2018 at 12:41 PM, ginerjm said:

Your form needs to direct the action to your script.  Your script needs to check that it does have a POST coming in to it to avoid hackers by doing this:

 


	if ( !$_SERVER['REQUEST_METHOD'] == 'POST')
	    {
	     echo "invalid response";
	    exit;
	}
	

 

After getting a POST response you can then go ahead and process the incoming $_POST array elements, using the name attribute of each form item.  If you have multiple submit buttons in your form you may want to assign a name to each so as to distinguish what the user wants the script to do.

Assign the values of the form's text fields to php vars when writing the html.

 


	echo "<input type='text' name='first_name' value='$first_name'>";
	

When you receive the inputs assign the post array values to these PHP vars. 
[php[

$first_name = $_POST['first_name'];

[/php]

If you want to then send the form back to the user simply redisplay your same html contents.    I usually use a php function that outputs my entire html page read up on using 'heredocs' for the page output) and when I  want the script to re-display it I simply call that function and exit, adding an error message to the output with a php variable such as $err_msg.  Make sure you use a global directive for those php vars that are buried in your html.

Hope this gives you some direction.

 

Ahh ok I think I had the wrong concept. I thought I could send variables back and forth between the form and the script. But if it looks like I have to collect the data, then basically build a new page to include that data. Is that correct?

So logically the flow would be: Display form -> collect input -> process input -> display "new" page.

I fixed a lot of mistakes and cleaned up that long IF chain into only a couple lines, and moved everything into one file for simplicity. Ill work on it a little more and come back when I get stuck again :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.