0

I have in database row services where have data Live, Text, Video separated with commas

This data is stored from checkboxes with implode function.

Now I need to explode Live Text Video individually and with if conditions to show different HTML code

<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>

I know what to do with if but can't get it individually.

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

2 Answers 2

1

You are not storing anything returned by the call to $stmt->fetch(). You need to do something like this:

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

Once you have created the array of services, you can output your HTMLseveral ways, including an if() or switch(). But it would probably be more efficient this way, in which the title and class are derived from the service:

<?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
}
?>

Here is the switch() version:

<?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;
    }
}
?>
Sign up to request clarification or add additional context in comments.

9 Comments

Hi, thank you for your answer. I know how to do it in HTML with an if statement, but I can't explode with PHP code. I think something is wrong with my code.
@MarkoStevanović See my updated answer.
When I try print_r($services); show me Array ( [0] => )
@MarkoStevanović What does print_r($row)' show?
Show me 1 I print it inside in the while loop
|
0

Selected everything with one query

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

And printed with if statement

<?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 } ?>

This is working but, it is safe?

3 Comments

"Safe" in what sense? Safety is a sliding scale, not a yes/no. What are you concerned about?
I mean it is safe for use. I am concerned about the secure code. No attack can be carried out like SQL injection
SQL injection is impossible in this case because the SQL statement is hard-coded, and not dependent on external, user-supplied data. As long as there is no other means for the contents of the DB to be altered, this code is safe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.