You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何拆分数据库中逗号分隔的services字段并匹配对应HTML代码

问题描述

我的数据库news表的services字段存储着以逗号分隔的Live、Text、Video数据,是通过implode函数把复选框内容拼接后存入的。现在我需要把这个字段值拆成单个元素,通过if条件判断展示对应的HTML代码。我懂if逻辑的用法,但没法正确拆分获取单个元素。

目标HTML示例:

<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Live service"><i class="fas fa-video"></i></div>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Text service"><i class="fas fa-align-justify"></i></div>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Video service"><i class="fas fa-camera"></i></div>

我的PHP查询代码:

$stmt = $con->prepare('SELECT `id`, `title`, `text`, `main_image`, `tags`, `services` FROM `news` ORDER BY `id` DESC LIMIT 8');

// Retrieve query results
$stmt->execute();
$stmt->bind_result($id, $title, $text, $naslovna, $tags, $services);
while ($stmt->fetch()) {
    // Add result to accounts array
    $news[] = ['id' => $id, 'title' => $title, 'text' => $text, 'naslovna' => $naslovna, 'tags' => $tags, 'services' => $services];
    $services = explode(", ", $services);
}
解决方案

你的代码存在两个核心问题:

  • 拆分顺序错误:先把未拆分的原始字符串存入$news数组,之后才执行拆分,导致数组里保存的还是完整的逗号分隔字符串,后续无法直接获取单个元素。
  • 拆分符匹配风险:如果存入时implode用的是纯逗号(没有空格),用", "拆分就会失败,需要兼容空格问题。

修改后的查询代码

$stmt = $con->prepare('SELECT `id`, `title`, `text`, `main_image`, `tags`, `services` FROM `news` ORDER BY `id` DESC LIMIT 8');

$stmt->execute();
$stmt->bind_result($id, $title, $text, $naslovna, $tags, $services);
while ($stmt->fetch()) {
    // 先拆分字段,用trim处理每个元素的空格,兼容纯逗号或逗号加空格的情况
    $serviceItems = !empty($services) ? array_map('trim', explode(',', $services)) : [];
    
    // 将拆分后的数组存入$news
    $news[] = [
        'id' => $id,
        'title' => $title,
        'text' => $text,
        'naslovna' => $naslovna,
        'tags' => $tags,
        'services' => $serviceItems
    ];
}

渲染对应HTML的实现

方式一:if条件判断

foreach ($news as $item) {
    // 渲染新闻其他内容...
    
    // 遍历拆分后的服务列表,输出对应图标
    foreach ($item['services'] as $service) {
        if ($service === 'Live') {
            echo '<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Live service"><i class="fas fa-video"></i></div>';
        } elseif ($service === 'Text') {
            echo '<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Text service"><i class="fas fa-align-justify"></i></div>';
        } elseif ($service === 'Video') {
            echo '<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Video service"><i class="fas fa-camera"></i></div>';
        }
    }
}

方式二:映射数组简化代码(推荐)

用关联数组定义服务与HTML的对应关系,避免大量if判断:

// 定义服务到HTML的映射
$serviceHtmlMap = [
    'Live' => '<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Live service"><i class="fas fa-video"></i></div>',
    'Text' => '<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Text service"><i class="fas fa-align-justify"></i></div>',
    'Video' => '<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Video service"><i class="fas fa-camera"></i></div>'
];

foreach ($news as $item) {
    // 渲染新闻其他内容...
    
    foreach ($item['services'] as $service) {
        // 存在对应映射则输出HTML
        if (isset($serviceHtmlMap[$service])) {
            echo $serviceHtmlMap[$service];
        }
    }
}

内容的提问来源于stack exchange,提问作者Marko Stevanović

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 00:15:38