正在创建从数据库存储路径显示图像的函数


crreating function to display image from database stored path

我正在创建一本食谱书(只是为了继续练习使用数据库),再一次,我卡住了…

我已经能够显示图像,但在一个坏的方式(我认为)…到目前为止我所做的:

In my 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>';

:显然也

 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 . '" />';
 }

}"recipes"表的列:

Id、名称、创建、持续时间、来源、categorories_id、attachment_id、chef_id

attachments表的列数:Id、attachment_path、recipe_id

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

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

因此,正如您所看到的,我的display_image()根本没有工作,我知道foreach循环没有做任何事情....我只是想不出别的办法,如有任何建议,我将不胜感激:)

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

功能不能再基本了....

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>';

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