【问题标题】:crreating function to display image from database stored path创建函数以显示来自数据库存储路径的图像
【发布时间】:2016-11-09 10:01:54
【问题描述】:

我正在创建一本食谱书(只是为了继续练习使用数据库),但我再次陷入困境......

我已经能够显示图像,但是以一种糟糕的方式(我认为)... 到目前为止我做了什么:

在我的 index.php 中:

$recipes = get_recipes();
$attachments = get_attachments();
$attachments_paths = get_image_path();

echo '<h2>recipes</h2>';
echo '<table id="recipesTable" border="1">';
echo '<tr>';
echo '<th>Recipe ID</th>';
echo '<th>Recipe Name</th>';
echo '<th>Attachment ID</th>';
echo '<th>Attachment path</th>';
echo '<th>Attachment image</th>';
echo '</tr>';
foreach ($recipes as $recipe) {   
  echo '<tr>';
    echo '<td>' . $recipe['id'] . '</td>';
    echo '<td>' . $recipe['name'] . '</td>';
    echo '<td>' . $recipe['attachment_id'] . '</td>';
    foreach ($attachments_paths as $attachment_path) {
        if ($recipe['attachment_id'] === $attachment_path['id']) {
            echo '<td>' . $attachment_path['attachment_path'] . '</td>';
        }
    }
    echo '<td>' . echo display_image(); . '</td>';
  echo '</tr>';
}
echo '</table>';

functions.php:

 function get_recipes() {
    include'db_connection.php';
    try {
        return $conn->query("SELECT * FROM recipes");
    } catch (PDOException $e) {
        echo 'Error:' . $e->getMessage() . "<br />";
        return array();
    }
    return true;
}

function get_attachments() {
    include'db_connection.php';
    try {
        return $conn->query("SELECT * FROM attachments");
    } catch (PDOException $e) {
        echo 'Error:' . $e->getMessage() . "<br />";
        return array();
    }
    return true;
}

function get_image_path() {
    include 'db_connection.php';

    $sql = 'SELECT recipes.name, attachments.id, attachments.attachment_path FROM attachments LEFT JOIN recipes ON recipes.attachment_id=attachments.id';

    try {
        $results = $conn->prepare($sql);
        $results->execute();
    } catch (PDOException $e) {
        echo 'Error: ' . $e->getMessage() . '<br />';
        return array();
    }
    return $results->fetchAll(PDO::FETCH_ASSOC);
}

function display_image() {
     foreach($attachments as $attachment) {
         $file = $attachment['attachment_path'];   
         return '<img src="' . $file . '" />';
 }

} “食谱”表的列: id, name, created, duration, source, categories_id, attachment_id, chef_id

“附件”表的列: id、attachment_path、recipe_id

还要提一下,我将图像的路径保存为: http://localhost/cooking_book/images/mistique.jpeg

如果有更好的方法,请告诉我!

所以,如您所见,我的 display_image() 根本不工作,我知道 foreach 循环在那里什么也没做......我只是没有其他可以做的事情,任何建议都会不胜感激:)

【问题讨论】:

  • HTML 源代码如何不符合您的期望?
  • 您不应该存储完整的 URL,这会使您的设置非常不灵活。例如,如果域发生变化怎么办?还是基础布局,这样就不再是/images/了?
  • 我知道!或多或少我知道......我昨天读了它,我尝试了很多次用 dirname(FILE) 来做,但一段时间后我感到沮丧......应该我得到了 dirname(FILE) 的真实路径,对吗?
  • Ignacio Vazquez,我想这是一种礼貌的说法,我的 html 不好......对吧?我知道它可能会更好,但我挣扎的地方是当我必须在数据库中检索和存储数据时,所以我现在不担心 html;我想,如果我“完成”这个项目,我会改进 html,但目前我只想了解数据库

标签: php database image


【解决方案1】:

好吧,我确信这不是最好的方法,我会继续努力,但至少,我现在正在显示图像;

功能再简单不过了....

function get_image_path() {
    include 'db_connection.php'; 

      $sql = 'SELECT recipes.name, attachments.id, attachments.attachment_path FROM attachments LEFT JOIN recipes ON recipes.attachment_id=attachments.id'; 

     try {
      $results = $conn->prepare($sql);   
      $results->execute();
     } catch(PDOException $e) {
        echo 'Error: ' . $e->getMessage() . '<br />';
        return array();
    }
      return $results->fetchAll(PDO::FETCH_ASSOC);    
}

function display_image() {
     include 'db_connection.php'; 

       return $image_path = get_image_path();       
}

为了显示它,在 index.php 上,我只是在我的表中添加了另一个字段:

echo '<td>';
    foreach ($images as $img) {
        echo '<img class="attachment" src="' . $img['attachment_path'] . '"/>';
  } 
 echo '</td>';

不过,如果有人有更好的选择建议,请告诉我:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2017-04-17
    相关资源
    最近更新 更多