我想使用foreach循環查看來自兩個單獨表格的值。在用戶選擇「啓動學期」,並從下拉列表中選擇「結束學期」,並且這兩個學期被存儲到一個數組名爲$ semesterarray如何顯示來自mySQL數據庫的兩個獨立表的值?
<table style='width: 75%; text-align: left;' cellpadding = '4'>
<tr bgcolor=#000090>
<th><FONT COLOR = #FFFFFF><b><?php echo $startsem ?></b></th>
<th><FONT COLOR = #FFFFFF><b><?php echo $endsem ?></b></th>
</tr>
// If $semesterarray contains 10 and 11, I want to be able to view the
// courses taken in the 10 semester and the 11 semester under two separate columns.
<?php
function getSemesterDetails($semester)
{
$output = "";
$semA = $semester."reg";
$query = "SELECT course1,course2,course3 FROM $semA";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
// row displays: [course1]=> [course2]=> [course3]=>
// Add semester code to array Keys to indicate proper semester
// [course1.11]=> [course2.11]=> [course3.11]=>
foreach ($row as $key => $value)
{
$row[$key.".".$semester] = $value;
unset($row[$key]);
}
$startcourse1 = $row['course1.'.$semester];
$startcourse2 = $row['course2.'.$semester];
$startcourse3 = $row['course3.'.$semester];
$startcoursesarray = array($startcourse1, $startcourse2, $startcourse3);
$startcourses = implode("<br>", $startcoursesarray);
$endcourse1 = $row['course1.'.$semester];
$endcourse2 = $row['course2.'.$semester];
$endcourse3 = $row['course3.'.$semester];
$endcoursesarray = array($endcourse1, $endcourse2, $endcourse3);
$endcourses = implode("<br>", $endcoursesarray);
echo "<tr bgcolor=#ABB5F6>
<td>$startcourses</td>
<td>$endcourses</td>
</tr>";
}
}
foreach ($midsemarrayA as $key => $semester)
{
echo getSemesterDetails($semA);
}
?>
任何想法?
我不確定你的問題是什麼。 – andrewsi 2012-07-05 16:41:44
使用上面的代碼,我只能查看在「10」學期採取的課程。這是因爲底部的echo語句發生在foreach循環的第一次迭代中。是否有工作方法循環查看「11」學期中所採用的課程? 「10」學期和「11」學期是我數據庫中的兩個獨立表格。 – myom 2012-07-05 16:43:45