【问题标题】:Highlight first row in php while loop在 php while 循环中突出显示第一行
【发布时间】:2014-11-03 14:54:24
【问题描述】:

我正在从 mysql 数据库中获取所有行。现在我只想用类名keywordHighlight 突出显示 php while 循环中的第一行。

如何在 php while 循环结果中仅突出显示第一行?

if($numSearch > 0){     

echo "<font color='green'>We found $numSearch result(s).</font>";
    echo "<table width='100%' cellpadding='0' cellspacing='0'>";
    echo "<thead>";
    echo "<tr>";                
    echo "<td class='' valign='top' width='200'></td>";
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "<td class='' valign='top' width='125'></td>";
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "<td class='' valign='top' width='125'></td>";                             
    echo "</tr>";   
    echo "</thead>";
    echo "<tbody>";     


while($resGetSearch =  mysql_fetch_array($getSearch)){

    $SearchCdid = $resGetSearch['cdid'];
    $SearchFamilyName =  $resGetSearch['family_name'];          
    $SearchGivenName =  $resGetSearch['given_name'];            
    $SearchCompamyCid = $resGetSearch['cid']; 
    $SearchDepartment = $resGetSearch['department'];
    $SearchTitle = $resGetSearch['title'];

    $SearchComapnyName =  mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
    $resSearchCompanyName =  mysql_fetch_array($SearchComapnyName);     
    $companyName = $resSearchCompanyName['company_name'];

    if (strlen($companyName) >= 20) {
        $companyName =  substr($companyName, 0, 10). "" . substr($companyName, -5);
    }else{
        $companyName = $companyName;
    }

    // my Highlighted class = keywordHighlight";    


    echo "<tr onclick='getDetails($SearchCdid);' >";                                        
    echo "<td valign='top' >$companyName</td>";
    echo "<td valign='top'>$SearchFamilyName</td>";
    echo "<td valign='top'>$SearchGivenName</td>";
    echo "<td valign='top'>$SearchDepartment</td>";
    echo "<td valign='top'>$SearchTitle</td>";
    echo "</td>";
    echo "</tr>";       
    }
    echo "</tbody>";
    echo "</table>";                        
    echo "<hr/>";
    echo "<br/>";   

}//elseif is not empty search
elseif($numSearch === 0){
    echo "<font color='red'>No Matches.</font>";
}

【问题讨论】:

    标签: php


    【解决方案1】:
    $first = true;
    
    while ( $resGetSearch =  mysql_fetch_array($getSearch) )
    {
    
        if ( $first == true )
        {
            // Add code here that only applies to the first iteration.
            $first = false;
        }
        else
        {
            // Add code here that only applies to the 2, 3, and so on.
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      放一些标志$f=0; 如果f==0 then do 是这样的:

          $highlight="<div class='keywordHighlight'>valur of first row</div>";// instead of dive you can use table also it depends on how you want to display.
      $f=$f+1;
      

      然后在其他部分休息。

      【讨论】:

        【解决方案3】:

        这样做

        $i = 1;
        while($resGetSearch =  mysql_fetch_array($getSearch)){ 
            $highlight = $i == 1 ? 'keywordHighlight' : '';
            echo "<tr class='{$highlight}' onclick='getDetails($SearchCdid);' >";
            ---------------
            -------------
            -------------
        
            $i++;
        }
        

        仅适用于 CSS

        或者你可以只用 css 突出显示它

        #highlight tbody tr:nth-child(1){
          background: #ff6600;
        }
        

        有更多优雅的方式只突出显示第一行,只有 css 不需要编码,请考虑示例 http://jsbin.com/soravuzakahu/1/

        【讨论】:

        • 它没有突出显示第一行。
        • @Shibbir 你确定你在 keywordHighlight 类的 css 中具有正确的行高亮属性和值吗?
        • @Shibbir 分享你的 css,只关联 css
        【解决方案4】:

        使用一个标志并将其值设置为 true 并在循环中检查其值,如果为 true 则打印类名并将其值设置为 false。像 $flag=true;然后在while循环中检查

         if($flag==true) { 
            <td class='yourclassname';
          $flag= false;
          } 
        

        【讨论】:

          【解决方案5】:
          <?php
          if($numSearch > 0){     
          
          echo "<font color='green'>We found $numSearch result(s).</font>";
              echo "<table width='100%' cellpadding='0' cellspacing='0'>";
              echo "<thead>";
              echo "<tr>";                
              echo "<td class='' valign='top' width='200'></td>";
              echo "<td class='' valign='top' width='125'></td>";                             
              echo "<td class='' valign='top' width='125'></td>";
              echo "<td class='' valign='top' width='125'></td>";                             
              echo "<td class='' valign='top' width='125'></td>";                             
              echo "</tr>";   
              echo "</thead>";
              echo "<tbody>";     
          
          
              $i = 0;
          while($resGetSearch =  mysql_fetch_array($getSearch)){
              ++$i;
              $SearchCdid = $resGetSearch['cdid'];
              $SearchFamilyName =  $resGetSearch['family_name'];          
              $SearchGivenName =  $resGetSearch['given_name'];            
              $SearchCompamyCid = $resGetSearch['cid']; 
              $SearchDepartment = $resGetSearch['department'];
              $SearchTitle = $resGetSearch['title'];
          
              $SearchComapnyName =  mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
              $resSearchCompanyName =  mysql_fetch_array($SearchComapnyName);     
              $companyName = $resSearchCompanyName['company_name'];
          
              if (strlen($companyName) >= 20) {
                  $companyName =  substr($companyName, 0, 10). "" . substr($companyName, -5);
              }else{
                  $companyName = $companyName;
              }
          
              // my Highlighted class = keywordHighlight";    
          
              if($i == 1)
                  echo "<tr onclick='getDetails($SearchCdid);' >";
              else
                  echo "<tr class='keywordHighlight' onclick='getDetails($SearchCdid);' >";
              echo "<td valign='top' >$companyName</td>";
              echo "<td valign='top'>$SearchFamilyName</td>";
              echo "<td valign='top'>$SearchGivenName</td>";
              echo "<td valign='top'>$SearchDepartment</td>";
              echo "<td valign='top'>$SearchTitle</td>";
              echo "</td>";
              echo "</tr>";       
              }
              echo "</tbody>";
              echo "</table>";                        
              echo "<hr/>";
              echo "<br/>";   
          
          }//elseif is not empty search
          elseif($numSearch === 0){
              echo "<font color='red'>No Matches.</font>";
          }
          

          【讨论】:

            【解决方案6】:

            您可以在循环外添加一个布尔值。像这样:

            $first = true;
            
            while($resGetSearch =  mysql_fetch_array($getSearch)){
            
                if(first == true){
                    // Add code here that only applies to the first iteration.
                }
                $first = false;
            }
            

            【讨论】:

              猜你喜欢
              • 2013-02-25
              • 1970-01-01
              • 1970-01-01
              • 2017-02-12
              • 1970-01-01
              • 1970-01-01
              • 2019-09-27
              • 2014-07-13
              • 2012-10-30
              相关资源
              最近更新 更多