foreach只显示一个元素

Please someone save my sanity and tell me what I'm doing wrong.

I have an array that is being populated by a query, if I do a print_r on that array I get the following,

[classes] => Array ( 

[0] => Array ( [course_number] => 1[course_name] => Thai [course_date] => 2015-02-23  [instructor_name] => Aaron ) 

[1] => Array ( [course_number] => 2 [course_name] => Linguistics [course_date] => 2015-03-02  [instructor_name] => Aaron ) 

[2] => Array ( [course_number] => 3 [course_name] => Ethics [course_date] => 2015-03-02  [instructor_name] => Aaron ) 

[3] => Array ( [course_number] => 4 [course_name] => Vocabulary [course_date] => 2015-03-02  [instructor_name] => Aaron ) 

[4] => Array ( [course_number] => 5 [course_name] => Civilization [course_date] => 2015-03-09 [instructor_name] => Aaron ) 

[5] => Array ( [course_number] => 6 [course_name] => Statistics [course_date] => 2015-03-09 [instructor_name] => Aaron ) 

[6] => Array ( [course_number] => 7 [course_name] => English [course_date] => 2015-03-09 [instructor_name] => Aaron ) )

I'm using the following to try and generate a table with any classes on the same date on the same row.

echo'   
<table>
    <caption><h1>Class Registration</h1></caption>
    <form action="',$scripturl,'?action=simsub_ENROL" method="POST" autocomplete="off">
    <input type=hidden name="studentid" value="' . $user_info['id'] . '">
    <tr>';

        $date = "2015-01-01";
        $i = 1;

        foreach ($simsub['classes'] as $record){
            if($date < $record['course_date']) {
                $i++;
                echo '</tr>
                        <td>
                            ' . $record['course_date'] . '
                        </td>               
                        <td>
                            ' . $record['course_number'] . ' - ' . $record['course_name'] . '<br />
                            ' . $record['instructor_name'] . '<br />
                        <input type="radio" name="class' . $i . '" value="' . $vrow['course_number'] . '
                        </td>';
            }else {
                echo '
                    <td>
                        ' . $record['course_number'] . ' - ' . $record['course_name'] . '<br />
                        ' . $record['instructor_name'] . '<br />
                        <input type="radio" name="class' . $i . '" value="' . $record['course_number'] . '
                    </td>';
            }

        $date = $record['course_date'];

    }

echo '
</tr>
<tr>
    <td>
    <div style="text-align:center"><input type="submit" class="button blue" value="Submit Registrations"></div>
    </td>   
</tr>
</form>
</table>';
  1. When it outputs I get a table showing 2 columns, the first date value and the values for the Thai class and then nothing else. It looks like the foreach loop never goes back to do the second value.

  2. The submit button is showing inside the same table field as Thai class information?

I suggest to use date comparison using strtotime. The main problem is that your HTML for the radio buttons are missing the closing " /> which stopped the other columns to be shown.

I'm using the following to try and generate a table with any classes on the same date on the same row.

If you want to display the classes of the same date on the same row, by using a temporary variable $date, you should print </tr><tr> when the current looped date is not equal to the date from the result and at the end of the loop you should print </tr> to finish the row. However, your query should be sorted by course date. Your <caption>, <form> and <input> between <table> and <tr> should be moved outside.

<?php
$simsub = array(
  'classes' => Array ( 
    0 => Array ( 'course_number' => 1, 'course_name' => 'Thai', 'course_date' => '2015-02-23', 'instructor_name' => 'Aaron' ), 
    1 => Array ( 'course_number' => 2, 'course_name' => 'Linguistics', 'course_date' => '2015-03-02', 'instructor_name' => 'Aaron' ),
    2 => Array ( 'course_number' => 3, 'course_name' => 'Ethics', 'course_date' => '2015-03-02', 'instructor_name' => 'Aaron' ), 
    3 => Array ( 'course_number' => 4, 'course_name' => 'Vocabulary', 'course_date' => '2015-03-02', 'instructor_name' => 'Aaron' ),
    4 => Array ( 'course_number' => 5, 'course_name' => 'Civilization', 'course_date' => '2015-03-09', 'instructor_name' => 'Aaron' ), 
    5 => Array ( 'course_number' => 6, 'course_name' => 'Statistics', 'course_date' => '2015-03-09', 'instructor_name' => 'Aaron' ),
    6 => Array ( 'course_number' => 7, 'course_name' => 'English', 'course_date' => '2015-03-09', 'instructor_name' => 'Aaron' ),
  )
);
$scripturl = '';
$user_info = array('id' => 1);

echo ' 
<caption><h1>Class Registration</h1></caption>
<form action="'.$scripturl.'?action=simsub_ENROL" method="POST" autocomplete="off">
<input type=hidden name="studentid" value="' . $user_info['id'] . '">  
<table border="1" cellspacing="2" cellpadding="5">
  <tr>';

$date = "";
$i = 1;

foreach ($simsub['classes'] as $record) {
  if($i == 1) $date = $record['course_date'];
  if(strtotime($date) !== strtotime($record['course_date'])) {
    echo '</tr><tr>';
  }
  echo '
    <td>
        ' . $record['course_date'] . '
    </td>               
    <td>
        ' . $record['course_number'] . ' - ' . $record['course_name'] . '<br />
        ' . $record['instructor_name'] . '<br />
      <input type="radio" name="class' . $i . '" value="' . $record['course_number'] . '" />
    </td>';
  $date = $record['course_date'];
  $i++;
}

echo '
  </tr>
</table>
<div>
  <input type="submit" class="button blue" value="Submit Registrations">
</div>
</form>';

The submit button is showing inside the same table field as Thai class information?

If you want the submit button displayed in the table, you have to set the respective colspan to the button <td>, otherwise just move it out of the table.

PhpFiddle Demo