如何显示mySQL数据库中两个独立表的值?

时间:2012-07-05 16:38:05

标签: php mysql html-table

我正在尝试使用foreach循环查看来自两个单独表的值。用户从下拉列表中选择“开始学期”和“结束学期”,这两个学期存储在一个名为$ semesterarray的数组中

<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);
}

?>

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这应该足以让你开始:

function getSemesterDetails ($semester) {
    $output = "";

    $query = "SELECT course1,course2,course3 FROM $semester";
    $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);

        $output .= "<tr bgcolor=#ABB5F6>
        <td>$startcourses</td>
        <td>$endcourses</td>
        </tr>";
    }

return $output;
}

foreach ($semesterarray as $key => $semester) {
     getSemesterDetails($semester);
}

你想让它返回一个值数组而不是一个字符串,但是把它转换成一个子程序应该可以解决问题。