从SQL表中正确输出数据有问题

时间:2011-01-13 16:38:22

标签: php mysql

我正在尝试从SQL表中输出数据

表格列是:

sheduleID,userID,empID,timeSlot,WeekSlot,daySlot


连接到数据库

$schedQ = "SELECT * FROM seo_schedule WHERE empID=1 AND weekSlot=1";
$Em1Wk1Res = mysql_query($schedQ) or die(mysql_error());

显示数据

echo "<div class='week1'>";
    while ($Em1WkRow = mysql_fetch_array($Em1Wk1Res)) {
        $clientQ = "SELECT * FROM clients WHERE userID=".$Em1WkRow["userID"]."";
        $clientRes = mysql_query($clientQ) or die(mysql_error());
        $clientRow = mysql_fetch_array($clientRes);
        echo "<div class='day".$Em1WkRow["daySlot"]."'>";
        if ($Em1WkRow["timeSlot"] == "am") {
            echo "<span class='".$Em1WkRow["timeSlot"]."'>";
            echo $clientRow["company"];
            echo "</span>";
        }
        else if ($Em1WkRow["timeSlot"] == "pm") {
            echo "<span class='".$Em1WkRow["timeSlot"]."'>";
            echo $clientRow["company"];
            echo "</span>";
        }
        echo "</div>";
    }
echo "</div>";

当前输出

<div class="week1">
     <div class="day1">
           <span class="am">Company 1</span>
     </div>
     <div class="day1">
           <span class="pm">Company 1</span>
     </div>

     <div class="day2">
           <span class="am">Company 2</span>
     </div>
     <div class="day2">
           <span class="pm">Company 2</span>
     </div>
     ...etc fir rest of days in week 1
</div>

我想要展示的是:

<div class="week1">
     <div class="day1">
           <span class="am">Company 1</span>
           <span class="pm">Company 1</span>
     </div>

     <div class="day2">
           <span class="am">Company 2</span>
           <span class="pm">Company 2</span>
     </div>
     ...etc fir rest of days in week 1
</div>

我该怎么做呢?

3 个答案:

答案 0 :(得分:0)

你需要一个嵌套的while来度过星期容器中的<span />个星期。我不是一个php dev,所以无法帮助你实现,抱歉。

答案 1 :(得分:0)

问题是,这些事件取决于特定的时间,还是您只是想在每天上午和下午的1次公司活动中抛出它们?在这种情况下,你可以这样做:

$arr = array(0 => "am", 1 => "pm");
echo "<div class='week1'>";
while($Em1WkRow = mysql_fetch_array($Em1Wk1Res)) 
{
    $clientQ = "SELECT * FROM clients WHERE userID='" . $Em1WkRow["userID"] . "'";
    $clientRes = mysql_query($clientQ) or die(mysql_error());
    $i = 0;
    $out .= "<div class='day" . $Em1WkRow["daySlot"] . "'>"; // start day
    while($clientRow = mysql_fetch_array($clientRes) && $i < 2) 
    {
        // add current day's event
        $out .= "<span class='" . $arr[$i++] . "'>";
        $out .= $clientRow["company"];
        $out .= "</span";
    }
    $out .= "</div>" // end day
}
echo $out; // display all week's info
echo "</div>"; // end week

答案 2 :(得分:0)

这就是我最终做的......

function emp_schedule($empID) {
    $weeks = array(1, 2, 3, 4);
    foreach ($weeks as $i => $week) {
    $schedQ = "SELECT * FROM seo_schedule WHERE empID=$empID AND weekSlot=$week";
    $Em1WkRes = mysql_query($schedQ) or die(mysql_error());
    echo "<div class='week'>";
    echo "<div class='head'><span class='ts'>Wk ".$week."</span> <span>Monday</span> <span>Tuesday</span> <span>Wednesday</span> <span>Thursday</span> <span>Friday</span></div>";
    echo "<div class='ts'><span><strong>AM</strong></span><span><strong>PM</strong></span></div>";
        while ($Em1WkRow = mysql_fetch_array($Em1WkRes)) {
            $clientQ = "SELECT * FROM clients WHERE userID='".$Em1WkRow["userID"]."'";
            $clientRes = mysql_query($clientQ) or die(mysql_error());
            while($clientRow = mysql_fetch_array($clientRes)) {
                $schd = "<div class='".$Em1WkRow["timeSlot"]."'>";
                $schd .= "<span class='client'>".$clientRow["company"]."</span>";
                $schd .= "</div>";
                $days = array(1, 2, 3, 4, 5);
                foreach ($days as $i => $day) {
                    if ($Em1WkRow["daySlot"] == $day && $Em1WkRow["timeSlot"] == "am" ) {
                        echo "<div class='day ".$Em1WkRow["daySlot"]."'>";
                        echo $schd;
                    }
                    if ($Em1WkRow["daySlot"] == $day && $Em1WkRow["timeSlot"] == "pm" ) {
                        echo $schd;
                        echo "</div>";
                    }
                }
            }
        }
        echo "</div>";
    }
}

我知道但它确实有效。