【问题标题】:PHP - how to get image from numeric arrayPHP - 如何从数字数组中获取图像
【发布时间】:2019-07-16 03:11:23
【问题描述】:

我有两个页面cloth.php,第二个是pickup.php

在cloth.php 上,我可以使用具有图像、价格和标题的复选框选择多个产品(来自DB),并在提交后重定向到包含产品详细信息的pickup.php 页面。

所以我在pickup.php 上得到了一个数组(文本和图像的组合) 我可以使用

foreach ($dada as $value) {
     echo "<h1>$value<br></h1>";
}

我正在获取演示产品(产品名称)、455(产品价格)、image.jpg(产品图片)。 我在这里得到图像名称,但我想显示图像,我该怎么做?

注意:数组中的行数每次都会根据产品的选择而变化。

请帮忙 提前致谢

【问题讨论】:

  • 从 $_FILES 数组中获取图像名称并将其放入您的
  • Array ( [0] => 前面有 Hooks1.png [1] => 8 [2] => 后面有 Hooks [3] => bls 2.png [4] => Dart/普通衬衫 [5] => 0 [6] => 5 天 deli.jpg [7] => 5 天内交货 [8] => 100 [9] => Task-106-Add-Ons1.jpg [10] => 优质棉衬里 [11] => 150 )
  • 请告诉我如何从这个数组中实现这一点?
  • 您是否将图像存储在服务器中?这样我们就可以给出路径并显示?
  • 图片名存DB,图片存文件夹

标签: php arrays image


【解决方案1】:

过滤您的主图像数组并将其放入一个数组中并从服务器获取路径并使用 img 标签显示它

$DisplayImages[0] =  "Front with Hooks1.png";
$DisplayImages[1] =  8;
$DisplayImages[2] =  "Back with Hooks";
$DisplayImages[3] =  "bls 2.png";
$DisplayImages[4] =  "Dart/Regular Blouse";
$DisplayImages[5] =  0;
$DisplayImages[6] =  "5 Days deli.jpg";
$DisplayImages[7] =  "Delivery in 5 days";
$DisplayImages[8] =  100;
$DisplayImages[9] =  "Task-106-Add-Ons1.jpg";
$DisplayImages[10] =  "Premium Cotton Lining";
$DisplayImages[11] =  150;

$supported_image = array(
'gif',
'jpg',
'jpeg',
'png'
);

echo ('<!DOCTYPE html>');
echo ('<html>');
echo ('<head>');
echo ('<title></title>');
echo ('</head>');
echo ('<body>');

 foreach ($DisplayImages as $key => $value) 
 {
    $ext = strtolower(pathinfo($value, PATHINFO_EXTENSION)); // Using strtolower to overcome case sensitive
    if (in_array($ext, $supported_image)) 
    {
    //Give your server path of image here and  Display it.
        $ImgPath = "../I/Profiles".$value;  // In this case I have image in my I folder profiles
        echo ( '<img src='.$ImgPath.' alt="Image" height="42" width="42">');   
    } 
    else
    {
        echo $value;  // Not an Image
    }
}


echo ('</body>');
echo ('</html>');
// echo "<pre>";print_r($ImagePath);
// exit();

【讨论】:

  • 非常感谢先生的努力,代码看起来不错,但问题是 -> 在数组中,我既有文本又有图像,而不仅仅是图像。我希望以适当的方式显示结果,图像后的第一个产品名称和最后的价格
  • 先生,这可能吗?否则我会改变我在其他页面上获取数据的概念。
猜你喜欢
  • 1970-01-01
  • 2013-05-08
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多