Drumlegend Posted May 4, 2013 Share Posted May 4, 2013 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. This is the recipe table, this is probably wrong but you get the idea of what I want to accomplish. 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. Quote Link to comment Share on other sites More sharing options...
Drumlegend Posted May 5, 2013 Author Share Posted May 5, 2013 bump Can no one help me with this? Quote Link to comment Share on other sites More sharing options...
kicken Posted May 5, 2013 Share Posted May 5, 2013 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. Quote Link to comment Share on other sites More sharing options...
Drumlegend Posted May 5, 2013 Author Share Posted May 5, 2013 No, it's not hosted online if that's what you mean. It's uploaded to the web server via ftp in a folder called recipeimages. Quote Link to comment Share on other sites More sharing options...
Drumlegend Posted May 5, 2013 Author Share Posted May 5, 2013 How do I store it in the database? Like what path do I type in? Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 The one where the image is... I don't understand your question at all. Is the folder in the web root? Then the path is that folder and the image filename. Quote Link to comment Share on other sites More sharing options...
Drumlegend Posted May 5, 2013 Author Share Posted May 5, 2013 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 You don't know whether to put the URL there or... What?? Do you know how to display an image in HTML? Quote Link to comment Share on other sites More sharing options...
Drumlegend Posted May 5, 2013 Author Share Posted May 5, 2013 I don't know what I type in the image field in the recipe table. I know how to display an image in HTML but I don't need help with that. I need help with the PHP. I can't explain it any clearer. Quote Link to comment Share on other sites More sharing options...
Vegas1 Posted May 5, 2013 Share Posted May 5, 2013 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 (edited) 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 May 5, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
Vegas1 Posted May 5, 2013 Share Posted May 5, 2013 (edited) 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 May 6, 2013 by KevinM1 Holy shit, carriage returns Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 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. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 5, 2013 Share Posted May 5, 2013 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 If you want to store an image path to display the image in HTML, that IS the path you want. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 6, 2013 Share Posted May 6, 2013 If you want to store an image path to display the image in HTML, that IS the path you want. No. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 6, 2013 Share Posted May 6, 2013 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. Quote Link to comment Share on other sites More sharing options...
Vegas1 Posted May 6, 2013 Share Posted May 6, 2013 Jessica you're very arrogant and your answers are not helpful at all, especially to a person like Drumlegend who states he's never done anything like this before and he's trying to learn. I know I'll never come back to this forum ever again! Quote Link to comment Share on other sites More sharing options...
Vegas1 Posted May 6, 2013 Share Posted May 6, 2013 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! Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 6, 2013 Share Posted May 6, 2013 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". Quote Link to comment Share on other sites More sharing options...
Drumlegend Posted May 6, 2013 Author Share Posted May 6, 2013 (edited) 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 May 6, 2013 by Drumlegend Quote Link to comment Share on other sites More sharing options...
Solution KevinM1 Posted May 6, 2013 Solution Share Posted May 6, 2013 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 />"; ?? Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 6, 2013 Share Posted May 6, 2013 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 6, 2013 Share Posted May 6, 2013 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? Quote Link to comment Share on other sites More sharing options...
Drumlegend Posted May 6, 2013 Author Share Posted May 6, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.