Need to get data value from mysql with array_key_exists

826 Views Asked by At

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

There are 2 best solutions below

0
On BEST ANSWER
<?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>
11
On

You have quoted your array $imageUrlStr. Update your code into

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

You need to know that how array_key_exists work

bool array_key_exists ( mixed $key , array $array )

key Value to check.

array An array with keys to check.

Second parameter must be an array not a string. So $imageUrlStr must be an array not a string

Edited: Using preg_match

$imageUrlStr = "http://example.com/images/data/baby-care-100x200.jpg";
$pattern = '/200x200/';
$productNewImage = preg_match($pattern, $imageUrlStr)) ? $imageUrlStr : '';

You can also use strpos as

$productNewImage = strpos($imageUrlStr, $pattern) !== false) ? $imageUrlStr : '';