从数据库表中获取图像数据,在图像列中,我们有8种不同尺寸的图像链接,用于单个产品,每个图像链接的大小详细信息如下: - 100x100,200x200,500x500等。
所有图像都以逗号指定 如: -
http://example.com/images/data/baby-care-100x100.jpg,
http://example.com/images/data/baby-care-200x200.jpg,
还有更多......
从这个所有图像链接我需要找到200x200包含img src的链接
$productImage = array_key_exists('200x200', '$imageUrlStr')? $imageUrlStr['200x200']:'';
完整代码
$result = $mysqli->query("SELECT * FROM store where store= 'deals' AND bestOffer= '1' AND inStock= 'true' ");
while ($rows = $result->fetch_assoc()) {
$store = $rows["store"];
$productId = $rows["productId"];
$title = $rows["title"];
$description = $rows["description"];
$imageUrlStr = $rows["imageUrlStr"];
$productNewImage = array_key_exists('200x200', $imageUrlStr) ? $imageUrlStr['200x200'] : '';
答案 0 :(得分:1)
您引用了array
$imageUrlStr
。将您的代码更新为
array_key_exists('200x200', $imageUrlStr) ? $imageUrlStr['200x200'] : '';
您需要了解array_key_exists
如何运作
bool array_key_exists ( mixed $key , array $array )
<强>键强> 值得检查。
<强>阵列强> 带有要检查的键的数组。
第二个参数必须是数组而不是字符串。所以$imageUrlStr
必须是数组而不是字符串
<强>编辑:强>
使用preg_match
$imageUrlStr = "http://example.com/images/data/baby-care-100x200.jpg";
$pattern = '/200x200/';
$productNewImage = preg_match($pattern, $imageUrlStr)) ? $imageUrlStr : '';
您也可以使用strpos
作为
$productNewImage = strpos($imageUrlStr, $pattern) !== false) ? $imageUrlStr : '';
答案 1 :(得分:1)
<?php
$result = $mysqli->query("SELECT * FROM store where store= 'deals' AND bestOffer= '1' AND inStock= 'true' ");
while ($rows = $result->fetch_assoc()) {
$store = $rows["store"];
$productId = $rows["productId"];
$title = $rows["title"];
$description = $rows["description"];
$imageUrlStr = $rows["imageUrlStr"];
$string=$imageUrlStr;
$array=explode(",",$string);
$pattern = "/200x200/";
?>
<li class="offer-best-box als-item">
<div class="offer-best-box-img">
<a href="<?php echo $rows['productUrl']; ?>" target="_blank"><img src="<?php foreach($array as $value){ $productNewImage = (preg_match($pattern,$value)) ? $value : ""; echo $productNewImage;} ?>" alt="" /></a>
</div>
<div class="offer-best-box-img2">
<div class="offer-best-box-discount"><span><?php echo round($percentage); ?>%</span><i>OFF</i></div>
<div class="offer-best-box-view"><img src="img/store/<?php echo $rows['store']; ?>-small.png" alt="available on store"></div>
</div>
<div class="offer-best-box-text"><a href="<?php echo $rows['productUrl']; ?>" target="_blank"><?php echo $rows['title']; ?></a></div>
<div class="offer-best-box-price">
<div class="offer-best-box-price-mrp">MRP: Rs. <?php echo $mrpPrice[0]; ?></div>
<div class="offer-best-box-price-offer">Price: Rs. <?php echo $offerPrice[0]; ?></div>
</div>
<div class="mobile-box-button"><a href="<?php echo $rows['productUrl']; ?>" target="_blank">Buy Now</a></div>
</li>