Jump to content

images with php


ROCKINDANO

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

#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" />'; ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

[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. :sarcastic:

[/ot]

 

Fair enough.  :P

 

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.