Jousi Jousi - 17 days ago 6
MySQL Question

Comparing array to database table, if any matches are found, get the img url and insert into PHP

so basicaly my conundrum is folowing:

I have a database like this:

Word | imgUrl | imgSize
volvo | C:\Users\..| 15


And I have an array like this:

$cars = array("Volvo", "BMW", "Toyota");


I need PHP to compare what in the array with whats in the database column Word , then retrieve a corresponding value of imgURL for the column value and store it in a variable
$img
.

Then I have this functionality prepared to run the rest of my program:

$size = 100;
$lines = file_get_contents(basename($_FILES["fileToUpload"]["name"]), FILE_USE_INCLUDE_PATH);
$words = explode(" ", $lines);
$words = array_splice($words, 0, count($words));
$img = Database entry

foreach ($words as $x => $word) {
print '<div class="result" style="position: relative; float: left;
width:' . $size . 'px; margin: 5px; height:' . $size . 'px;
background-image:url('. $img .'); background-size: 100% 100%;">
<a id="word' . $x . '">' . $words[$x] . '</a></div>';
}


As you can see, I am trying to put each word of an array into a separate
<div>
and then display it. All works well, I just need to show the corresponding img to the word as the background of the
<div>


Anyone has any idea ?
Any and all suggestions appreciated!

Cheers

Answer

You can do it without database.

  1. Put all images to one directory(for example "Words")
  2. Name all images corresponding to word they represent (for example Volvo.png,BMW.png)
  3. Go thought array and set images paths

Code sample

for($x=0;$x<Count($x);$x++)
{
    echo'<div class="result" style="background-image:url("https://website.com/Words/'.$words[$x].'.png");background-size: 100% 100%;"><a id="word'. $x .'">'. $words[$x] .'</a></div>';
}

Notes

It is better solution, because don't need connect to database and will generate the page a bit faster.

Its downside is a space requirements to store all images.

Comments