ROCKINDANO Posted December 8, 2009 Share Posted December 8, 2009 i was wondering if there is a way to have a list of stories and a pic with every story. grab the pic (file name ) from a db that holds the stories along with a pic related to that story. can someone point me to the right direction for this? Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/ Share on other sites More sharing options...
raytri Posted December 8, 2009 Share Posted December 8, 2009 The easiest (and fastest) thing to do is to store the photo in a folder on your server, and only store it's location in the database. Then when you pull the story from the database, you echo the img HTML you want, and plug the location information into the "src" section. Depending on your needs, you can do anything from storing the location as a full URL, to only storing the filename and writing the pathname dynamically whenever the page is called. The former is simplest to set up but hardest to change (if you change your file structure, you have to go through and change every URL in your database). It's usually best for simple sites with only a few pictures, or when the images are hosted somewhere else (like in an RSS feed). The latter takes a little more planning to set up, but lets you make file structure changes without having to update your database. If that's unclear and you need sample code, let me know. Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973402 Share on other sites More sharing options...
ROCKINDANO Posted December 8, 2009 Author Share Posted December 8, 2009 well i was thinking of just storing the flie name in the db record and trying to put the file name on the src="" tag. this is what i have don't know if its right. --> <?php $i = 1; while ($r=mysql_fetch_array($result)) { $news_id=$r["news_id"]; $title=$r["title"]; $fulldesc=$r["fulldesc"]; $pic=$r["file"]; ?> <div id="slide<?php print $i; ?>" class='slides'> <?php print '<img src="images/$pic" title="" border="0" class="floatright" />'; ?> this is my while loop where i print out everything onto the page. can you help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973407 Share on other sites More sharing options...
raytri Posted December 8, 2009 Share Posted December 8, 2009 #1, why use PRINT? You're better off using ECHO; it's faster. #2, The structure of your code is right, but you need to use concatenation to show what's a variable and what isn't. So change your img code to this: <?php print '<img src="images/'.$pic.'" title="" border="0" class="floatright" />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973410 Share on other sites More sharing options...
raytri Posted December 8, 2009 Share Posted December 8, 2009 Alternatively, do it the way you're doing the slide numbers: <img src="images/<?php echo $pic; ?>" title="" border="0" class="floatright" /> Also, I assume you're only showing me partial code, and you know that the code snippet isn't incrementing $i and the while loop isn't closed. Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973413 Share on other sites More sharing options...
salathe Posted December 8, 2009 Share Posted December 8, 2009 [ot] #1, why use PRINT? You're better off using ECHO; it's faster. Why use PHP? You're better off using <insert other, faster, language>; it's faster. [/ot] Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973416 Share on other sites More sharing options...
raytri Posted December 8, 2009 Share Posted December 8, 2009 [ot] #1, why use PRINT? You're better off using ECHO; it's faster. Why use PHP? You're better off using <insert other, faster, language>; it's faster. [/ot] Fair enough. I'm not a PHP expert by any means, but in my limited experience ***new users*** are better off using ECHO over PRINT because of familiarity and compatibility. PRINT and ECHO are almost identical, but they're different enough at the margins that you can get unexpected results in some cases. Most code examples I've seen assume ECHO, most schools and textbooks teach ECHO (the intro PHP textbook on my desk devotes a sentence to PRINT -- in order to explain why it uses ECHO exclusively). There are cases where PRINT is the better answer, but for most new users ECHO cuts down the confusion factor. Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973433 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 I'm not a PHP expert by any means, but in my limited experience ***new users*** are better off using ECHO over PRINT because of familiarity and compatibility. PRINT and ECHO are almost identical, but they're different enough at the margins that you can get unexpected results in some cases. Most code examples I've seen assume ECHO, most schools and textbooks teach ECHO (the intro PHP textbook on my desk devotes a sentence to PRINT -- in order to explain why it uses ECHO exclusively). There are cases where PRINT is the better answer, but for most new users ECHO cuts down the confusion factor. You may be interested in this OLD OLD thread: http://www.phpfreaks.com/forums/index.php/topic,132729.msg558095.html#msg558095 It has some interesting information regarding print vs echo. EDIT: The basic gist, echo tends to be faster and better then print. But it really does not matter which you use as the difference is very minor. It all depends on user preference Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973435 Share on other sites More sharing options...
ROCKINDANO Posted December 8, 2009 Author Share Posted December 8, 2009 well it still doesn't show the pic. i am incrementing the $i by one so the slideshow works. but i want it to grab a pic that is related to each story. i have the pic name in a record in a db. Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973453 Share on other sites More sharing options...
raytri Posted December 8, 2009 Share Posted December 8, 2009 well it still doesn't show the pic. i am incrementing the $i by one so the slideshow works. but i want it to grab a pic that is related to each story. i have the pic name in a record in a db. The code I gave you assumes that "$pic" is the filename of the pic (drawn from the column "file" in your database), and that it resides in the folder "images", so that the relative pathname "images/$pic" is valid. So check the following: 1. echo $pic. Does it return the proper filename? If not, the problem is back where you defined $pic. Or it may be even further back. Is your $result query properly defined? If $pic is giving you the proper string: 2. Is the picture with that filename in the images folder on your server? 3. Is the relative path to the images folder correct? Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973467 Share on other sites More sharing options...
ROCKINDANO Posted December 8, 2009 Author Share Posted December 8, 2009 well it does print out the file name in the file column in my db. Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973474 Share on other sites More sharing options...
ROCKINDANO Posted December 8, 2009 Author Share Posted December 8, 2009 thank you so so so so so so much. it worked. was uploading the pic to wrong destination. again THANK YOU SO MUCH DUDE. Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973477 Share on other sites More sharing options...
raytri Posted December 8, 2009 Share Posted December 8, 2009 You're welcome! Glad it worked for you. Quote Link to comment https://forums.phpfreaks.com/topic/184402-images-with-php/#findComment-973479 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.