简体   繁体   English

如何突出显示类似于关键字的sql结果

[英]How highlight sql results that are like the keyword

I've seen many posts on google and on this site about the same subject, but i didn't understand any of them. 我已经在Google和此网站上看到过很多关于同一主题的帖子,但我都不了解。 (I'm not very skilled in php yet) (我对php不太熟练)

I have the code below to output information about presidents which are saved in a database. 我有下面的代码,用于输出有关总统的信息,这些信息保存在数据库中。 The result is in a table. 结果在表格中。 But i want to highlight the keywords in this table. 但我想突出显示此表中的关键字。 So for example if you search for 'ad' you'll get the 'ad' in 'adams' highlighted. 因此,举例来说,如果您搜索“广告”,则会在“广告”中突出显示“广告”。

-Name-       -Birth-      -years president-    -died-    
Adams J Q     1767              4                80

As i've said before i'm not really skilled so i hope someone can show me what to do. 正如我之前所说,我不是很熟练,所以我希望有人可以告诉我该怎么做。 Thanks! 谢谢!

The code: 编码:

 extract($_POST, EXTR_SKIP);                                                                                        
    pdo()->prepare($qry);                                                                                               // wat is de query
    pdo()->bindParam(':zoekTerm', "%$zoekTerm%", PDO::PARAM_STR);                                                       // vul parameter in; let op het '' en "" verschil
    pdo()->execute();                                                                                                   // uitvoeren van de query met params


    function getData($qry, Array $bindParameters)                                                                       // haal data uit de query
    {
        pdo()->prepare($qry);
        foreach($bindParameters as $k => &$v) {
            pdo()->bindParam($k, $v, PDO::PARAM_STR);
        }
        pdo()->execute();
        return pdo()->fetchAll();                                                                                       // dus een array (associatief)
    }
                                                                                                                        //print the data from an array

    function prData(Array $data) {

        foreach($data as $k => $v) {
            //print_r($v);
            echo implode( ', ', $v);
        }

    }

    function prDataOp1Regel(Array $data) {
        foreach($data as $r) {

            $d = array();        
            foreach($r as $v) {
                $d[] = $v;          
            }
            print implode('</td><td>', $d); tr();   
        }
    }
    function prDataKeys(Array $data) {      
        foreach($data as  $v) {
            $r = array();                   
            foreach($v as $k => $dummy) {
                $lk = strtolower($k);
                // even iets geks. Omdat het kan.
                if($lk === "gebjaar") $lk = "Geboorte jaar" ;
                $r[] =  Ucfirst($lk);
            } 

            echo    implode ('</th><th>', $r); //nl();
            break;

        }
    }


    $qry = "
    SELECT 
        presnaam AS 'Naam President' 
        ,Gebjaar
        ,Jaarpres AS 'Aantal jaren president' 
        ,Sterfleeftijd
        ,Partij
        ,Staatgeboren AS 'Geboren in'   
    FROM president 
    WHERE presnaam 
    LIKE :zoekTerm 
    "; 

    $d = getData($qry, array(':zoekTerm' => "%$zoekTerm%")); 

    echo '<table id="outputtable"><tr>';
    echo '<th>';
    prDataKeys($d);
    echo '</th>';
    echo '<tr>';
    echo '<td>';
    prDataOp1Regel($d);
    echo '</td>';
    echo '</td>';
    echo '</tr></table>';

You can use preg_replace or str_replace to replace the string that was being searched on with some html that you can format using css. 您可以使用preg_replace或str_replace将正在搜索的字符串替换为可以使用CSS设置格式的html。

For example, "Adams" would be replaced with something like "<span class='match'>Ad</span>ams" or "<strong>Ad</strong>ams" if the search was for "Ad". 例如,如果搜索是针对“广告”,则“亚当斯”将替换为“ <span class ='match'> Ad </ span> ams”或“ <strong> Ad </ strong> ams”。

EDIT: 编辑:

In your function prDataOp1Regel, you can perform the string replacement at the line: 在函数prDataOp1Regel中,您可以在以下行执行字符串替换:

$d[] = $v;

This line would become: 该行将变为:

$d[] = str_replace ( $searchStr , "<span class='match'>{$searchStr}</span>" , $v);

However, you'd need to update your function definition to take in the search string as a parameter: 但是,您需要更新函数定义以将搜索字符串作为参数:

function prDataOp1Regel(Array $data, $searchStr) {

Also, you shouldn't do a search and replace for every row and column, it should only be done for the column that is being searched on. 另外,您不应该对所有行和列进行搜索和替换,而应该仅对正在搜索的列进行搜索和替换。 If the $data array is an associative array where the column names are the key for each value in the column you can change the code so that it is something similar to: 如果$ data数组是一个关联数组,其中列名是该列中每个值的键,则可以更改代码,使其类似于以下内容:

foreach($r as $columnName => $v) {
    if ($columnName == 'Naam President') {
        $d[] = str_replace ( $searchStr , "<span class='match'>{$searchStr}</span>" , $v);
    } else {
        $d[] = $v;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM