Jump to content

storing and displaying image from database via filepath


Go to solution Solved by KevinM1,

Recommended Posts

I have a folder in my ftp called recipeimages and it contains all images for different recipes.

 

In my database I have a recipe table and an image path but I think I may have done it wrong and I was wondering how I would display that image to my webpage.

<?php $path = "#"; ?> <img  src=<?php echo $src; ?> />

I don't know whether or not this is the correct code I would use. The ideal way would be to display an image assigned to a certain recipe.

 

recipe.png

This is the recipe table, this is probably wrong but you get the idea of what I want to accomplish.

 

image.png

This is the table and the fields used. I have the imagepath set as varchar as I don't want to directly store the images in the database.

 

I have never done anything like this before so I would love a bit of help.

 

Thank you in advance.

 

 

 

Is the path to your image via URL something like http://example.com/public_html/recipeimage/cheesesandwhich.jpg or is it http://example.com/recipeimage/cheesesandwhich.jpg. If it's the latter you'll need to strip the public_html bit off before you echo out the path, which you can do using substr.

Basically, I have an image that is associated with a recipe. I have created a field in the recipe table called image which is set as VARCHAR and I don't know what I type in there. I don't know whether I need to put the url there and then I need to know how to retrieve that image and display it on my website. I can't find any tutorials explaining how. I'm sorry if you don't understand the question but I don't think I can explain it any clearer. 

he is asking how to get the image path stored in his MySQL database to display the image on his search result page.  Drumlegend I am fighting with this same scenario as well. I have my images uploaded to a folder and the path to the folder stored in MySQL and when I do a search it comes up like this,  http://jemtechnv.com/portal/inventory/inventory_search.php   Instead of the image being displayed at the end of each row it displays the path to the image.

PHP echos HTML.

 

If you have the path, just echo the HTML for the image tag. It's not as complicated as you guys are making it.

 

<?php
$img = 'mydir/myimg.png';
echo '<img src="'.$img.'">';
?>
Edited by Jessica

of course it's not complicated to someone that works with PHP all the time, but for most of the beginners that come here looking for help it is a huge complication some times. I've searched this topic for weeks and tried maybe 15 or 20 examples from other forums and tutorials with no success. I have limited experience using PHP and understand that PHP will echo html but it's not a simple as you put it, since both of us are pulling the path from MySQL  what you have given as an example will not work. 

<td>".$rows['id']."</td><td>".$rows['part_number']."</td><td>".$rows['model_number']."</td><td>".$rows['serial_number']."</td><td>".$rows['part_manufacture']."</td><td>".$rows['part_available']."</td><td>".$rows['part_quantity']."</td><td>".$rows['part_condition']."</td><td>".$rows['part_image']."</td>


with my line of code above I have tried every example I have found with using a relative path for my images to various versions of the code below with no success.

<img src="<?php echo $imageSrc; ?>" />

I appreciate you taking the time to offer your input to assist in this matter, every bit of input is helpful.

 

And Drumlegend I apologize for hijacking your thread that wasn't my intentions, I was merely attempting to clarify your question by using my issue as a visual example.

Edited by KevinM1
Holy shit, carriage returns

Jesus what's with the carriage returns?

 

Look, if you tried it and it didn't work, that's a lot more to go on than just saying you don't know what to do.

 

What didn't work about that method?

 

Having a file path in a database is absolutely NO different from the code I posted. A database is one thing, PHP is another, HTML is another. Your end goal is to print an image in HTML. That IS simple.

The "/public_html" is not a root directory is a sub-directory of the server.

 

But....in fact that lots of hosting providers use that directory as a home directory of the web server.

 

So.... if you want to get a proper root directory of the web server this path is completely wrong.

 

 

 

 

 

Seriously?

 

So on my machine, the full path of an image is C:\wamp\www\project\web\images\image.png.

 

My "web root" or the equivalent of public_html is C:\wamp\www\project\web

 

Are you trying to argue that the image url should be: C:\wamp\www\project\web\images\image.png and not images/image.png? Because if so my computer and every other one I've ever developed on must be special.

and for what it's worth I figured out how to do it which is what was being asked originally  and my solution was

<td>".$rows['part_condition']."</td><td><img src='".$rows['part_image']."'height='50' width='50'/></td>

and you can see from my original link posted above that it works  and look at that no path in there anywhere huh? it pulls the path from the database which both Drumlegend and I were asking how!

Jessi, no, no and no :)

 

Every root directory of the web (nfs, samba, ftp and ect...) servers should be represent only by "/" symbol in the unix machines, so if his own web root directory is "public_html" the correct path is "/", not "/public_html".

Thanks for everyone's help but the million dollar question is how to pull a recipe image that is associated with that recipe.

 

If you check out the site you will understand what I mean. http://www.cupboard2stomach.com/php/get.php

 

This is the php code I have used.

 

 

$ingredients = array(); //We use an array here to store data as we'll be using the ingredients query data twice, and we can't loop through with mysql_fetch_row twice on the same query


$query = mysql_query("SELECT * FROM `ingredients`");
while ($row = mysql_fetch_assoc($query)) {
    $ingredients[$row['id']] = $row['name']; //now we have all ingredients in an array with the array keys as the ingredient ids and the array values as the names
}




?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="POST">
<h3> Search your recipe here </h3>
<fieldset>
Ingredient 1:<select name="dropdown1" id = "drop1">
    <?php foreach($ingredients as $id => $name) { ?>
        <option value="<?php echo $id; ?>"><?php echo $name;?> </option>
    <?php } ?>
</select>
Ingredient 2:<select name="dropdown2" id = "drop2">
    <?php foreach($ingredients as $id => $name) { ?>
        <option value="<?php echo $id; ?>"><?php echo $name;?> </option>
    <?php } ?>
    </select>
Ingredient 3:<select name="dropdown3" id = "drop3">
    <?php foreach($ingredients as $id => $name) { ?>
        <option value="<?php echo $id; ?>"><?php echo $name;?> </option>
    <?php } ?>
    </select>
    <input id="search" name="search" type="Submit" value="Search" >
</fieldset>
</form>

 

 

 

<?php
if (isset($_POST['search'])) {
    $ingredient1 = mysql_real_escape_String ($_POST['dropdown1']);
    $ingredient2 = mysql_real_escape_String ($_POST['dropdown2']);
$ingredient3 = mysql_real_escape_String ($_POST['dropdown3']);


    $recipes = mysql_query("
        SELECT DISTINCT `name`, `step1`, `step2`, `step3`, `image`
        FROM `recipe` r
        INNER JOIN `recipe_ingredients` ri
        ON r.id = ri.recipe_id
        WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.")
    ");
echo '<section id="results">';
    while ($recipe = mysql_fetch_assoc($recipes)) {


echo 'Recipe Name: ';
echo $recipe['name'].'<br />'; 
echo '<input id="search" name="Look" type="Submit" value="Search" >';
echo '<br />';
echo 'Step 1: ';
echo $recipe['step1'].'<br />';
echo '<br />';
echo 'Step 2: ';
echo $recipe['step2'].'<br />';
echo '<br />';
echo 'Step 3: ';
echo $recipe['step3'].'<br />';
echo '<br />';
echo $recipe['image'].'<br />';
echo '<br />';








    }
}
echo '</section>';
?>
Edited by Drumlegend

and for what it's worth I figured out how to do it which is what was being asked originally and my solution was

<td>".$rows['part_condition']."</td><td><img src='".$rows['part_image']."'height='50' width='50'/></td>

and you can see from my original link posted above that it works and look at that no path in there anywhere huh? it pulls the path from the database which both Drumlegend and I were asking how!

Jesus Christ.

That is exactly what I SAID TO DO

Jessi, no, no and no :)

 

Every root directory of the web (nfs, samba, ftp and ect...) servers should be represent only by "/" symbol in the unix machines, so if his own web root directory is "public_html" the correct path is "/", not "/public_html".

Again ... That is exactly what I said! What is wrong with this thread?

1. Are you sure your query is working?  Have you run it in phpMyAdmin?

2. Have you simply tried (with the code above)

echo "<img src=\"{$recipe['image']}\" /><br />";

??

The query runs fine. It pulls out recipes that contain them ingredients. I tried that code and it didn't work. Maybe I have put the wrong path in the database.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.