php - 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 标签吗?
解决方案
如果我理解正确,您正试图突出显示 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_ID
URL 中的数据。这是该功能的文档:https ://www.php.net/array_key_exists
推荐阅读
- ruby-on-rails - 如何使用 google-api-ruby-client People API 传递 OAuth 令牌?
- python - Selenium - 网站在使用常规浏览器与 webdriver 时显示不同的值
- r - 数值的 %in% 返回错误的结果
- elasticsearch - 将新文档添加到生产 Elasticsearch 集群
- amazon-web-services - 当父字段不在视觉对象中时,AWS QuickSight 计算字段不显示数据
- bash - 在 shell 脚本中打印字符串的确切值作为输出
- drake - 刷新输出到 drake::log()
- r - 错误:找不到对象“skim_without_charts”
- python - 神经网络中的特征重要性(分类问题)
- html - 更新 HTML/CSS 以使内容显示在右侧而不是下方