【问题标题】:highlight a box in a list according to a condition in another table [duplicate]根据另一个表中的条件突出显示列表中的框[重复]
【发布时间】:2017-05-19 15:15:15
【问题描述】:

我有两张桌子:

表 1(总分)

表2(每周得分)

我有一个排行榜,我在上面呼应了表 1 的总体得分值:

问题:我在这里要做的是,无论谁在表 2(每周得分)中得分为“-10”,我想通过突出显示其框的颜色来提醒用户排行榜,现在是黄色,变为红色。

当前涉及的 css:

li mark div {
    display: block;
    margin: 4px;
    padding: 5px;
    min-height: 50px;
    border: 2px solid #eebb55;
    border-radius: 7pt;
    background: grey;
}

涉及显示列表的 PHP。这是“整体”(排行榜中的右侧标签)。每周也存在类似的情况。

<div id="overalllb" class="leadboardcontent" style="display:none">
    <div class="leaderboard">
        <ol>
            <li>
                <mark>
                    <?php  while( $toprow2 = sqlsrv_fetch_array( $stmt3) ) {
                        echo  "<div class='parent-div'><span class='rank'>" . $toprow2['overallRank'] . "</span><span class='name'>" . $toprow2['EmployeeName'] . "</span><span class='points'>" . $toprow2['Total_points_Rewarded'] . "</span></div>";
                    } ?>
                </mark>
            </li>
        </ol>
    </div>

通过查询从两个表中检索信息:

1.query 1 - 找出所有得分为-10的员工。

$q200 = " select *
  from Table2
  where  WeekNumber = 'week1' and pointsRewarded = '-10';";
  $stmt200=sqlsrv_query($conn,$q200);
  if($stmt200==false)
  {
  echo 'error to retrieve info !! <br/>';
  die(print_r(sqlsrv_errors(),TRUE));
  }

查询 2- 从表 1 中检索所有员工:

$q20 = "select *
  from EmployeeTable
  order by Total_points_Rewarded desc";
  $stmt20=sqlsrv_query($conn,$q20);
  if($stmt20==false)
  {
  echo 'error to retrieve info !! <br/>';
  die(print_r(sqlsrv_errors(),TRUE));
  }

我正在使用 PHP,请给我一个方法。上帝保佑。

【问题讨论】:

  • 能否请您发布用于显示此列表的脚本?

标签: javascript php html css twitter-bootstrap


【解决方案1】:

通过在 SQL 语句中连接这两个表,您可以简化该过程。使用上面 dhi_m 中的代码,但数据将来自单个 SQL 查询:

SELECT 
    `table1`.`EmployeeName`,
    `table1`.`Overallpoints`,
    `table1`.`overallRank`,
    `table1`.`Total_points_Rewarded`,
IF(`Table2`.`points` = '-10','red-css-class','green-css-class') AS `display_status`
FROM `Table1`
LEFT JOIN `table2`
    ON `table1`.`EmployeeID` = `table2`.`EmployeeID`
WHERE `table1`.`WeekNumber` = 'week1'
ORDER BY `Table1`.`Total_points_Rewarded` DESC

列表的 HTML 代码如下所示:

<div id="overalllb" class="leadboardcontent" style="display:none">
    <div class="leaderboard">
        <ol>
            <li>
                <mark>
                    <?php  while( $toprow2 = sqlsrv_fetch_array( $stmt3) ) {
                        echo  "<div class='".$toprow2['display_status']."'><span class='rank'>" . $toprow2['overallRank'] . "</span><span class='name'>" . $toprow2['EmployeeName'] . "</span><span class='points'>" . $toprow2['Total_points_Rewarded'] . "</span></div>";
                    } 
                    ?>
                </mark>

            </li>
        </ol>
    </div>
</div>

(未经测试,可能包含拼写错误)

【讨论】:

  • @Solan 我明白了,但不想创建另一个表,这是不必要的。
  • 我在想,我们可以比较两个查询中的名字,相似的名字会有一个红色框
  • @jane 上面的 SQL 查询不会创建新表,而是比较 join on 子句中的名称(实际上是员工 ID)。 if() 语句决定了 div 的颜色。
【解决方案2】:

请试试这个

    <style>

    li mark div .parent-div-green {
        display: block;
        margin: 4px;
        padding: 5px;
        min-height: 50px;
        border: 2px solid #eebb55;
        border-radius: 7pt;
        background: grey;
    }

    li mark div .parent-div-red {
        display: block;
        margin: 4px;
        padding: 5px;
        min-height: 50px;
        border: 2px solid red;
        border-radius: 7pt;
        background: grey;
    }
</style>
<div id="overalllb" class="leadboardcontent" style="display:none">
    <div class="leaderboard">
        <ol>
            <li>
                <mark>
                    <?php  while( $toprow2 = sqlsrv_fetch_array( $stmt3) ) {

                        $divClass ="parent-div-green";
                        /****** Here write a query/function or join the two tables to fetch the Table2 $toprow2['EmployeeID']'s' `points` field value to the variable `$points` ********/
                        if($points == -10)  $divClass = "parent-div-red";
                        echo  "<div class='".$divClass."'><span class='rank'>" . $toprow2['overallRank'] . "</span><span class='name'>" . $toprow2['EmployeeName'] . "</span><span class='points'>" . $toprow2['Total_points_Rewarded'] . "</span></div>";
                    } 
                    ?>
                </mark>

            </li>
        </ol>
    </div>

【讨论】:

  • 感谢您的输入。但它不起作用。我已经编写了两个查询来从两个表中检索信息。我们只需要计算两个地方的名称,如果名称匹配我们会使其框边框变为红色。
  • 没问题。问题是我们需要匹配两个表中的名称,如果匹配,则名称为红色,否则为绿色。我这么说是因为上面的 query1 将带来具有“-10”作为点数。
  • 所以,很遗憾你的代码不起作用。
  • if ($toprow20['EmployeeName'] == $toprow200['EmployeeName']) 然后红色否则绿色....!!
  • 您想从表 2 中获取 EmployeeName/EmployeeID 且 points 等于 -10,对吧?
【解决方案3】:

您可以签入一个简单的 php if..else 以突出显示它..

假设您在 $usersscore 内部用户循环时存储用户的特定分数

if($usersscore<=-10){
    $highlightclass="redmark";
 }
else{
   $highlightclass-"yellowmark";
}

在 html 中 在 html 元素的类中,您可以像这样回显它

<li class="<?php echo $highlightclass; ?>">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-01
    • 2019-12-02
    • 2010-12-17
    • 2014-02-05
    • 1970-01-01
    • 2022-01-05
    • 2018-01-04
    • 1970-01-01
    相关资源
    最近更新 更多