2

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'] : '';
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
Pratik Soni
  • 169
  • 2
  • 13

2 Answers2

1

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 : '';
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • Still its not working facing error Warning: array_key_exists() expects parameter 2 to be array, string given in – Pratik Soni May 26 '15 at 10:54
  • Show more code, obviously $imageUrlStr is not what you think it is or it did not get loaded by database query – RiggsFolly May 26 '15 at 10:56
  • From the name of the variable, it seems like `$imageURLStr` is a string, not an array. So it doesn't make sense to call `array_key_exists` on it. – Barmar May 26 '15 at 10:57
  • $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']:''; – Pratik Soni May 26 '15 at 11:00
  • can you please help me how can i make correction for preg_match to get 200x200 image link – Pratik Soni May 26 '15 at 11:12
  • From prag_match i am getting all image links. and strpos i am not getting any result or error. – Pratik Soni May 26 '15 at 11:40
  • [Check This for strpos](https://eval.in/370319) and [check this for preg_match](https://eval.in/370321) its working for me – Narendrasingh Sisodia May 26 '15 at 11:44
  • I just check your given code, thanks. but $imageUrlStre value i entered in this script and from this value i need single value of this link which have 200x200 word, can i fetch it ? please check your give link. – Pratik Soni May 26 '15 at 11:49
  • Thanks for your help. thanks lot. i just made little correction in script and run then its working fine. – Pratik Soni May 26 '15 at 16:55
  • Uchiha can you please help me for my new question ? – Pratik Soni May 31 '15 at 18:17
  • Which One?@PratikSoni – Narendrasingh Sisodia Jun 01 '15 at 04:53
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>
Pratik Soni
  • 169
  • 2
  • 13