Denis
Denis

Reputation: 103

PHP replace TEXT(name) with IMAGE statement using DATABASE and MYSQLI

I would like to create a list of actors.

Actors added to database in this way Jhone Jhones, Tom Boras and e.t.c..

All actors in one line in database separated with ","

| And I have result:

Roles: Jhone Jhones, Tom Boras

The idea is to create a separate PHP file with a list of actors which will be used like:

IF Jhone Jhones replace with <img src="roles/JhoneJhones.jpg" >

Thank you.

This is what I have (ONLY IDEA): DO NOT KNOW how to get information from $row['vidRoles'] and insert it to array. This one question.

$array = array("Jhone Jhones","Tom Boras");

Then:

IF index[0] == to Jhone Jhones{
 replace(index[0] to <img src="roles/JhoneJhones.jpg">
}

EDIT:

I have this idea:

  $query = "SELECT vidRoles FROM videoinformation";

    if ($result = mysqli_query($con, $query)) {

        while ($row = mysqli_fetch_row($result)) {
            printf ("<img src=\"roles/%s.jpg\>", $row[0]);
        }

        mysqli_free_result($result);
    }

In one (each)vidRole field in database, I have all together divided with comma Name1,Name2,Name3.

How using this code which I showed before, take each field divided each field like:
From Name1,Name2,Name3 to:
Name1
Name2
Name3
I mean separate variables.

Finally what I need using Name1,Name2,Name3 to:

<img src="roles/Name1.jpg>
<img src="roles/Name2.jpg>
<img src="roles/Name3.jpg>

Upvotes: 0

Views: 124

Answers (1)

Antoan Milkov
Antoan Milkov

Reputation: 2228

Here is some code to get you started:

$vidRole  = 'Name1,Name2,Name3,Name4,Name5';
$strImg = '';
foreach(explode(',', $vidRole) as $name) {
    $strImg .= "<img src='roles/$name'>\n";
}
echo $strImg;

The output is:

<img src='roles/Name1'>
<img src='roles/Name2'>
<img src='roles/Name3'>
<img src='roles/Name4'>
<img src='roles/Name5'>

I hope this is what you need!

Upvotes: 1

Related Questions