首页 > 解决方案 > PHP高亮选中项

问题描述

在此处输入图像描述

就像上图一样,当用户单击右侧的列表之一时,它会突出显示选定的列表。

$result = mysqli_query($con, "SELECT * FROM contacts") or die(mysqli_error($con));

while($row = mysqli_fetch_array($result)){
    $company = $row['eyo_company_name'];
    $id = $row['con_id'];
    $editLinks .= "\n\t<a href=\"edit.php?id=$id\">$company</a><br>";
}

这就是我从数据库中取出列表的方式。有什么方法可以添加一个类或只在选定的一个上添加 b 标签吗?

标签: phphtmlcss

解决方案


如果我理解正确,您正试图突出显示 PHP 输出的链接之一。为此,您需要知道要突出显示哪一个。

如果您将 GET 变量设置为当前联系人 ID,则可以执行以下操作:

while($row = mysqli_fetch_array($result)){
    $company = $row['eyo_company_name'];
    $id = $row['con_id'];

    //let's check if we've pre-selected a company
    //  and if so, is this the company we selected?
    if (true === array_key_exists('CURRENT_COMPANY_ID', $_GET) && $id === $_GET['CURRENT_COMPANY_ID']) {

        //we did select this company for this PHP request
        //  let's make the HTML output custom to highlight the selected company
        $editLinks .= "\n\t<a href=\"edit.php?id=$id\"><strong>$company</strong></a><br>";
    } else {
        $editLinks .= "\n\t<a href=\"edit.php?id=$id\">$company</a><br>";
    }
}

此示例期望您对生成链接列表的 PHP 脚本的请求将包含CURRENT_COMPANY_ID=<ID HERE>在 URL 中。

例如,如果生成链接列表的请求的 URL 如下所示:

/getMySuperAwesomeList.php

它需要看起来像这样:

/getMySuperAwesomeList.php?CURRENT_COMPANY_ID=123

与您要突出显示的记录匹配123的位置。con_id

我选择使用array_key_exists()以确保请求实际上包含CURRENT_COMPANY_IDURL 中的数据。这是该功能的文档:https ://www.php.net/array_key_exists


推荐阅读