用逗号分隔数据库中的字符串



我有在数据库行services,其中有数据Live, Text, Video用逗号分隔

此数据存储在具有内爆功能的复选框中。

现在我需要单独爆炸LiveTextVideoif条件来显示不同的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>

我知道怎么处理if,但不能单独得到它。

$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);
}

您没有存储调用$stmt->fetch()返回的任何内容。你需要做这样的事情:

while ($row = $stmt->fetch()) {
$servis = $row['services'];
$services = explode(', ', $servis);
}

一旦创建了服务数组,就可以用几种方式输出html,包括if()switch()。但是这样做可能会更有效,其中标题和类派生自服务:

<?php
foreach($services as $svc) {
$title = ['Live' => 'Live', 'Text' => 'Text', 'Video' => 'Photo'][$svc];
$c = ['Live' => 'fa-video', 'Text' => 'fa-align-justify', 'Video' => 'fa-camera'][$svc];
?>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="<?= $title ?> service">
<i class="fas <?= $c ?>"></i>
</div>
<?php
}
?>

switch()版本:

<?php
foreach($services as $svc) {
switch ($svc) {
case 'Live':
?>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Live service"><i class="fas fa-video"></i></div>
<?php
break;
case '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>
<?php
break;
case 'Video':
?>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Photo service"><i class="fas fa-camera"></i></div>
<?php
break;
}
}
?>

用一个查询选择所有内容

$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];
}

并打印if语句

<?php
$service = explode(", ", $new['services']);
if (in_array('Live', $service)) { ?>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Live servis"><i class="fas fa-tower-broadcast"></i></div>
<?php }
if (in_array('Text', $service)) {  ?>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Tekst servis"><i class="fas fa-align-justify"></i></div>
<?php }
if (in_array('Video', $service)) {  ?>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Video servis"><i class="fas fa-video"></i></div>
<?php }
if (in_array('Photo', $service)) {  ?>
<div class="icon-sm bg-black-soft bg-blur text-white rounded-circle" title="Foto servis"><i class="fas fa-camera"></i></div>
<?php } ?>

这是工作,但它是安全的吗?

最新更新