2

从数据库表中获取图像的数据,在图像列中,我们有一个产品的 8 个不同尺寸的图像链接,每个图像链接都有其尺寸详细信息,例如:- 100x100、200x200、500x500 等。

所有图像都用逗号指定,例如:-

http://example.com/images/data/baby-care-100x100.jpg ,

http://example.com/images/data/baby-care-200x200.jpg ,

http://example.com/images/data/baby-care-250x250.jpg ,

http://exampple.com/images/data/baby-care-500x500.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'] : '';
4

2 回答 2

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 : '';
于 2015-05-26T10:51:05.597 回答
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>
于 2015-05-26T16:58:27.010 回答