doutan2228 2014-02-08 15:27
浏览 102
已采纳

是否可以将图像路径插入到数组中以及如何使用PHP进行操作?

I need help in inserting image path into arrays. Is it even possible? Firstly, I have created a table for accepting random comments from the user then inside that table I have a path for images that I want to call at the same time as the text. I have no problem displaying the texts such as username, password but when I add the path to it, it only displays the URL literally. How would I display the image?

Here is my PHP code:

<?php

require("admin.config.inc.php");

//initial query
$query = "Select * FROM adComments";

//execute query
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll();


if ($rows) {
    $response["success"] = 1;
    $response["message"] = "Post Available!";
    $response["posts"]   = array();

    foreach ($rows as $row) {
        $post             = array();
    $post["post_id"]  = $row["post_id"];
    $post['img'] = 'http://www.stagconnect.com/StagConnect/admin/pictures/'.$row["image_name"];
    $post["username"] = $row["username"];
    $post["title"]    = $row["title"];
    $post["message"]  = $row["message"];
    $post["time"] = $row["time"];


        //update our repsonse JSON data
        array_push($response["posts"], $post);
    }

    // echoing JSON response
    echo json_encode($response);


} else {
    $response["success"] = 0;
    $response["message"] = "No Post Available!";
    die(json_encode($response));
}

?>

The only problem here is the post['img']. It doesn’t display an image. What other method can I try? Any help will do.

  • 写回答

1条回答 默认 最新

  • dongqian9013 2014-02-08 16:03
    关注

    The fact that the variable name contains the word "img" means nothing. What you need to do is where you are processing the response from the Database into HTML, you need to specifically state that this is an image and you want it displayed as such.

    <img src="value from post["img"]" alt="Image" height="50" width="50">
    

    You can omit the width and height parameters if you don't need to restrict the size of the images.

    Embedding an img tag inside the post string may work, but from a design point of view, you are then restricting what what you can do with the returned data.

    A more flexible solution would be to separate the URL from the image name:

    $post["img_url"] = 'http://www.stagconnect.com/StagConnect/admin/pictures/';
    $post["image_name"] = $row["image_name"];
    

    And then:

    <img src="value from post["img_url"] plus value from post["image_name"]" alt="Image" height="50" width="50">
    

    This would enable you to (if you wanted to) have the URL in the Database, and possibly have a different URL for each image. In other words, no hard-coding.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝