Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Adding images directory to PHP echo statement

New Here ,
Aug 05, 2009 Aug 05, 2009

Copy link to clipboard

Copied

Hi

I'm developing a website where images are displayed by pulling down the image details (e.g. picture.jpg) from a field in a MySql database. This works fine although the image has to be located in the root directory of the website.I wish to move the 'picture.jpg' to the images/ directory so that I don't have images in the root directory (best practice) but would like to know if there is a way of modifying the PHP statement I'm using:

<?php echo $row_Recordset1['Picture']; ?>

so that it 'points' to the 'images' sub-directory? I know that a workaround would be to modify the MySql field entry to show images/picture.jpg rather than simply picture.jpg, but I wondered if there was a way of using PHP to do the work instead?

Really appreciate any help on this one.

Many thanks...Tony

TOPICS
Server side applications

Views

1.3K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Aug 05, 2009 Aug 05, 2009

Copy link to clipboard

Copied

Modify the img src directory on the page. It's best just to have imagename.ext by itself in the database. If you want directory in the DB then store it in its own field.

<img src="/images/<?php echo $row_Recordset1['Picture']; ?>" alt="img" />

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 05, 2009 Aug 05, 2009

Copy link to clipboard

Copied

Hi

That's great! Sorry to pose what seems like such a basic question but I'm

only just getting to grips with PHP.

Really appreciate the fast response as well :>)

Regards...Tony

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 06, 2009 Aug 06, 2009

Copy link to clipboard

Copied

Use something like this in your code:

<?php if($row_viewtypes['thumblink'] > " ") {$photo = $row_viewtypes['thumblink'];}
   else {$photo = "blank.jpg";} ?>
     <a href="bigships.php5?<?php echo "ref"; ?>=<?php echo $row_viewtypes['ref']; ?>">
     <img src="../thumbs/<?php echo $photo; ?>" alt="The <?php echo $row_viewtypes['shipname']." - Click for a bigger picture"; ?>" name="photo" width="160" height= "120" id="photo" /></a>

-----------------------------------------------------------------------------------------------

Now the explaination:

Code with remarks follows:

<!--set $photo to the image file name if it exists--> <?php if($row_viewtypes['thumblink'] > " ") {$photo = $row_viewtypes['thumblink'];}

<!-- if not provide a dummy image-->   else {$photo = "blank.jpg";} ?>

<!-- Provide a link to a nother page to show a bigger photo and pass the reference number of the record for that page to use-->

    <a href="bigships.php5?<?php echo "ref"; ?>=<?php echo $row_viewtypes['ref']; ?>">

<--set the path to the image file which is in the thumbs folder, relative to the current page at ../thumbs/  -->

    <img src="../thumbs/<?php echo $photo; ?>"

<!-- add in the alt code from the database --> alt="The <?php echo $row_viewtypes['shipname']." - Click for a bigger picture"; ?>" name="photo" width="160" height= "120" id="photo" /></a>      </div>

See it in action at http://www.tyneships.co.uk

Select searches, Type, and select from the list.

All the photos are store as filenames only in the database, with the actual files being stored as jpg files in the thumbs directory.

Hope this is explained well enough.

Howard Walker

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 07, 2009 Aug 07, 2009

Copy link to clipboard

Copied

LATEST

Hi Howard

Really appreciate the suggestions which I will look at to see if they could

figure in my site.

Many thanks...Tony

PS Very much like the 'tyneships' site :>)

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines