在循环中突出显示php中的第一行

时间:2014-09-10 04:25:00

标签: php

我从mysql数据库获取所有行。现在我想强调php中的第一行while while循环,类名为keywordHighlight

如何在循环结果中仅突出显示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>";     


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

6 个答案:

答案 0 :(得分:0)

放一些旗帜$f=0; if 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;

并在其他部分休息。

答案 1 :(得分:0)

这样做

$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/

答案 2 :(得分:0)

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

$first = true;

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

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

答案 3 :(得分:0)

<?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>";
}

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

$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.
    }

}