jetlife76 Posted October 23, 2011 Share Posted October 23, 2011 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/249655-arrays/ Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281595 Share on other sites More sharing options...
jetlife76 Posted October 23, 2011 Author Share Posted October 23, 2011 Ok that is a bit much for me, is there another way to do this, i am a beginner in php, i just need to know how to do the array and manipulate it to show the values i listed earlier. Quote Link to comment https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281603 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 That is how you'd do it. Look at each line, understand whats happening. If you don't understand a line, please ask. Quote Link to comment https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281607 Share on other sites More sharing options...
jetlife76 Posted October 23, 2011 Author Share Posted October 23, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281610 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281627 Share on other sites More sharing options...
jetlife76 Posted October 23, 2011 Author Share Posted October 23, 2011 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" } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281628 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 Okay? I'm not here to code for you. Everything in your foreach loop will be repeated for every array key of $Numbers If you want them all on one line, perhaps something like implode would work instead of foreach. Quote Link to comment https://forums.phpfreaks.com/topic/249655-arrays/#findComment-1281650 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.