Page 1 of 1

Help with Array and variables

Posted: Thu Dec 02, 2004 1:30 pm
by wadesmart
Im have been working on understanding arrays but getting the information back out is difficult.

Here is a tutorial I have been working one.

Page One:

Code: Select all

<?php
<html><head></head>
<body>
  <?php 
    echo"<form method='post' action='exam2.php'>";
      $Student = array("Albert Einstein", "Ivan The Terrible", "Napoleon", "Simon Bolivar", "Issac Newton");
        while (list(,$Name)=each($Student)) {
          echo "What grade did $Name get in Math?";
          echo "<br /><br />";
          echo "<select name='Math[]'>
                  <option>Grade A</option>
                  <option>Grade B</option>
                  <option>Grade C</option>
                  <option>Grade D</option>
                  <option>Grade F</option>
                </select>";
          echo "<br /><br />";
          echo "<input type='hidden' name='Student[]' value='$Name'>";
        }
    echo "<input type='submit'></form>";
  ?>
</body>
</html>

?>
And Page Two

Code: Select all

<?php
<html><head></head>
  <body>
  <p>In Math the graders were in order:<br /></p>
    <?php
      
      while(list($Index,$Value)=each($_POST['Math'])) {
        $GradeStudent[] = $_POST['Math'.$Index] . $_POST['Student'.$Index];
      }
      asort($GradeStudent);
      while(list($Index, $Value)=each($GradeStudent)) {
        echo '<br />'.$_POST['Student'.$Index].' - '.$_POST['Math'.$Index].'.';
      }
    ?>
  </body>
</html>

?>
Im have another script I got help on but its not helping me with this one. I do not understand how to get the $Index to be correct. It produces errors.

Posted: Thu Dec 02, 2004 2:48 pm
by wadesmart
Tried this...

Code: Select all

<?php
<html><head></head>
  <body>
  <p>In Math the graders were in order:<br /></p>
    <?php
      var_dump($_POST);
      echo '<pre>';
      print_r($_POST);
      
      while(list($_POST['Index'],$_POST['Value'])=each($_POST['Math'])) {
        $GradeStudent[] = $_POST['Math'].$_POST['Index'] . $_POST['Student'].$_POST['Index'];
      }
      asort($GradeStudent);
      while(list($_POST['Index'], $_POST['Value'])=each($GradeStudent)) {
        echo '<br />'.$_POST['Student'].$_POST['Index'].' - '.$_POST['Math'].$_POST['Index'].'.';
      }
    ?>
  </body>
</html>

?>

Posted: Thu Dec 02, 2004 3:08 pm
by rehfeld
the last time i helped you when we did the $_POST['car-'.$i] type stuff,
we were not using arrays at all. we were just making a bunch of variables.

here you are using arrays, so you dont do that stuff anymore.

Code: Select all

<html><head></head>
  <body>
  <p>In Math the graders were in order:<br /></p>
    <?php
      
      while(list($Index,$Value)=each($_POST['Math'])) {
        $GradeStudent[] = $_POST['Math'][$Index] . $_POST['Student'][$Index];
      }
      asort($GradeStudent);
      while(list($Index, $Value)=each($GradeStudent)) {
        echo '<br />'.$_POST['Student'][$Index].' - '.$_POST['Math'][$Index].'.';
      }
    ?>
  </body>
</html>