2

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

Jousi
  • 456
  • 4
  • 26
  • So all the `
    `s will have the same image? I ask because it looks like you just assign it just one time before the loop. What if you have one row with `BMW` and one row with `Volvo`, which image do you pick?
    – Cave Johnson Nov 15 '16 at 19:00
  • No the images have to corespond to the words of course. But thats what I have a problem with. – Jousi Nov 15 '16 at 19:42
  • In that case you should move your `$img = Database entry` line into your loop and run the query for each word. See my answer. – Cave Johnson Nov 15 '16 at 19:53

2 Answers2

2

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.

0

Not knowing the type of database you are using, I assume mysql. This code should suffice:

//                    IP            username       password    database
$mysqli = new mysqli('127.0.0.1', 'your_user', 'your_pass', 'sakila');
$sql = "select imUrl from  tableName where word = '".$cars[0]."'";
$result = $mysqli->query($sql));
$car_img = $result->fetch_assoc();
//access image using $car_img['imgUrl'];

Hope that helps. If you have multiple images you can through a loop around this and run it as often as you want, provided you store the $car_img somewhere so it is not overwritten.

jacksonecac
  • 284
  • 4
  • 15