Jump to content

Arrays


jetlife76

Recommended Posts

Ok i am trying to create a simple program that will allow mwe to use an array with 8 numbers, the output form should show the numbers in their original order that the user inputs them, in numerical order, Highest number, lowest number and then the average number. I am confused on what logic i would use to do this. If anyone could help me get started it would be much appreciated. here's waht i have so far.

<?php
$Num1 = $_POST['fielda'];
$Num2 = $_POST['fieldb'];
$Num3= $_POST['fieldc'];
$Num4 = $_POST['fieldd'];
$Num5 = $_POST['fielde'];
$Num6 = $_POST['fieldf'];
$Num7 = $_POST['fieldg'];
$Num8 = $_POST['fieldh'];
$Numbers = array(1,2,3,4,5,6,7,;
$big = max($Numbers);
$small = min($Numbers);
$average = array_sum($Numbers) / 8; 
?>

Link to comment
https://forums.phpfreaks.com/topic/249655-arrays/
Share on other sites

There's an easier way to do this. Forms can generate their own arrays as well.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
	<label>Number 1: <input type="text" name="number[1]" size="3"></label><br>
	<label>Number 2: <input type="text" name="number[2]" size="3"></label><br>
	<label>Number 3: <input type="text" name="number[3]" size="3"></label><br>
	<label>Number 4: <input type="text" name="number[4]" size="3"></label><br>
	<input type="submit">
</form>
<?php 

// Check for form post
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

// Make sure at least one number was submitted, and that it is an array
if( isset($_POST['number']) && is_array($_POST['number']) && !empty($_POST['number']) ) {
	// Loop through numbers
	echo '<h3>Original</h3>';
	foreach( $_POST['number'] as $num => $val ) {
		echo "Number $num: $val<br>";
	}
	// Sort numbers naturally and reverse - doesn't work for negative numbers
	natsort($_POST['number']);
	$_POST['number'] = array_reverse( $_POST['number'], TRUE ); 
	// Loop through them again
	echo '<h3>Sorted</h3>';
	foreach( $_POST['number'] as $num => $val ) {
		echo "Number $num: $val<br>";
	}
	// Get average
	echo '<h3>Average</h3>';
	echo ( array_sum($_POST['number']) / count($_POST['number']) );
}

}

?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281595
Share on other sites

I dont understand this line,

if( $_SERVER['REQUEST_METHOD'] == 'POST' )

is there a way to do it without the form doing the array itself?

i need to have an input and output form becaue that's the way it was taught in class, i understand that this way can be easier but its confusing because it's different than the way it's explained in the book

Link to comment
https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281610
Share on other sites

Of course. There is a way, but this way will impress your teacher. Arrays are meant to group similar data that may have to be sorted or looped through, so why not use them?

 

First off, $_SERVER is a Super-Global (this means it can be accessed in ANY SCOPE - within functions or classes - and is usually reserved for variables that get defined internally). You can read more about it here

http://php.net/reserved.variables.server.php

'REQUEST_METHOD' will usually contain either GET (the user came to this page via a link,or a form with GET method) or POST (if a user came to this page via a form with POST method)

 

if( $_SERVER['REQUEST_METHOD'] == 'POST' )

checks to see if the page was access through a form.

 

Anything else? If you're unsure how the data is formatted, this little script will show you what the $_POST Super-Global holds after the form is submitted.

 

<?php 

// Check if form was submitted
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
// the <pre> tag will make everything output nice
echo '<pre>';
// print_r() will output an array's structure
print_r( $_POST );
echo '</pre>';
}

?>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; // this forces the form to post to itself ?>">
Number 1: <input type="text" name="number[1]" size="3"><br>
Number 2: <input type="text" name="number[2]" size="3"><br>
Number 3: <input type="text" name="number[three]" size="3"><br>
<input type="submit">
</form>

Link to comment
https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281627
Share on other sites

ok i figured out how to do it, but now when i print, the numbers for the sorted and original sequences, they display in a vertical list, i need them to display horizontally(1 2 3 4 5 ...), it also repeats the words"sorted Numbers" before each number(don't need that

 

here's what i have:

if (isset($_POST['numbers']))
{
     $Numbers = $_POST['numbers'];
     $big = max($Numbers);
     $small = min($Numbers);
     $average = array_sum($Numbers) / count($Numbers);
 print "Original Sequence: <br>";
 sort($Numbers);
 foreach ($Numbers as $item) {
 	 print "sorted Numbers:$item<br>";}
 print 
 print "Largest Number: $big<br>";
 print "Smallest Number: $small<br>";
     print "Average Number: $average<br>";
 //else 
 //print "all entries must be numeric"
}
  
?>

Link to comment
https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281628
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.