所以基本上我的难题如下:
我有一个这样的数据库:
Word | imgUrl | imgSize
volvo | C:\Users\..| 15
我有一个像这样的数组:
$cars = array("Volvo", "BMW", "Toyota");
我需要 PHP 将数组中的内容与数据库列 Word 中的内容进行比较,然后检索列值的 imgURL 对应值并将其存储在变量$img
.
然后我准备好此功能来运行程序的其余部分:
$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>';
}
如您所见,我试图将数组的每个单词放入单独的 <div>
中然后显示它。一切正常,我只需要显示该单词对应的img作为<div>
的背景即可。
有人有什么想法吗? 任何和所有建议表示赞赏!
干杯
最佳答案
无需数据库即可完成。
- 将所有图片放入一个目录(例如“Words”)
- 根据它们所代表的单词来命名所有图像 (例如Volvo.png、BMW.png)
- 思考数组并设置图像路径
代码示例
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>';
}
注释
这是更好的解决方案,因为不需要连接到数据库并且生成页面的速度会更快一些。
它的缺点是存储所有图像需要空间。
关于php - 比较数组和数据库表,如果找到任何匹配项,则获取 img url 并插入到 PHP 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614719/