phpmysqlarraysimagearray-key-exists

Need to get data value from mysql with array_key_exists


Data fetch from database table for image, in image column we have 8 different sizes image links for one single products and each image link have its size details like:- 100x100, 200x200, 500x500 and so on.

All images are specified with comma Like:-

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

And more.....

From this all images link i need to find 200x200 contains link for img src

$productImage = array_key_exists('200x200', '$imageUrlStr')? $imageUrlStr['200x200']:'';

Full Code

$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'] : '';

Solution

  • <?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>