【问题标题】:Need to get data value from mysql with array_key_exists需要使用array_key_exists从mysql获取数据值
【发布时间】:2015-08-08 00:01:54
【问题描述】:

从数据库表中获取图像的数据,在图像列中,我们有一个产品的 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'] : '';

【问题讨论】:

  • 首先添加$imageUrlStr的值。那么只能给出解决方案。谢谢。
  • 我从数据库中获取它的价值,它的价值因每种产品而异,我该怎么办?
  • 请显示更多代码。我们需要看看你是如何设置$imageUrlStr
  • 打印出来。如果它是一个数组,那么print_r($imageUrlStr)。否则回显$imageUrlStr。还是动态一个一个来?
  • 匹配它的字符串而不是数组,您需要使用preg_match 而不是array_keys_exits

标签: php mysql arrays image array-key-exists


【解决方案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 : '';

【讨论】:

  • 仍然无法正常工作,面临错误警告:array_key_exists() 期望参数 2 为数组,字符串中给出
  • 显示更多代码,显然 $imageUrlStr 不是你想的那样或者它没有被数据库查询加载
  • 从变量名看来$imageURLStr是个字符串,不是数组。所以在上面调用array_key_exists是没有意义的。
  • $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']:'';
  • 你能帮我如何纠正 preg_match 以获得 200x200 图像链接
【解决方案2】:
<?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多