SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dispalying images

    i have created some records in mysql and have managed to display these records on my site.

    im struggling to display images. at the moment (not sure if this correct) i have inserted the location of the pic as a record in the db. all the pics are on my server.

    am i going about this the correct way?

    any help would be great!

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its common to store a picture path, or at least store the data neccesary to craft the path.

    for example, storing
    images/foo.gif
    images/food/cake.jpg

    another method could be to just store the filename, and have a seperate field to store some type of id which relates the record to a record in another table.

    eg
    Code:
    filename    catid
    cake.gif    1
    cupcake.gif 1
    apple.gif   1
    bear.gif    2
    dog.gif     2
    
    
    
    catid path
    1     images/food/
    2     images/animals/

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Sounds like you are going about it correctly.
    e.g take this example:
    ww.website.com/articles/images/myimage.php

    Your main decision will be how much to store in the database.

    You can either store just the name of the file "myimage.jpg" and then let PHP work out and write the path to the file,

    echo '<img src="articles/images' . $row['image'] . '" alt = ' . $row['alt_text'] .'>';

    Or store they whole thing from the main directory in the database:

    echo '<img src="'. $row['image'] . '" alt = ' . $row['alt_text'] .'>';

    It will depend on your need, and what you think your future need will be. For example, if you think the folder name might change, then get PHP to write the full path to the file.

    HTHY

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Code:
    id  article_name        article_heading
    1  'New happened'    'Sh*t went down....
    2  'Something else'   'Nobody cared....
    Of course you could be a lazy b like me and call your images after the id of articles, ie "1.jpg".

    if(file_exists('/images/' . $id . '.jpg'))
    echo '<img src=/images/' . $id . '.jpg>';

  5. #5
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the advise. wasnt too sure on the php. ill give yur egs a go.

  6. #6
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one other thing. what field type should i use? just text?

  7. #7
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi cups ive used your eg but am still struggling.

    this is the error msg i am getting
    Code:
    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/sites/petesnetwork.co.uk/public_html/autosaver/index.php on line 41
    heres my php code
    Code:
    <?php
    $username = "web29-jubilee";
    $password = "garage";
    $hostname = "localhost"; 
    
    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password) 
      or die("Unable to connect to MySQL");
    echo "<br>";
    
    //select a database to work with
    $selected = mysql_select_db("web29-jubilee",$dbhandle) 
      or die("Could not select examples");
    
    
    //execute the SQL query and return records
    $result = mysql_query("SELECT make_id, make_title, make_pic FROM Make");
    
    
    echo 
    '<table width="100%" border="1" cellspacing="2" cellpadding="2"> 
      <thead> 
        <td>ID</td> 
        <td>Pic</td> 
        <td>Name</td> 
      </thead> 
    '; 
    
    
    //fetch tha data from the database
    while ($row = mysql_fetch_array($result)) 
    {
    
    
    
    
    // loop the table row 
    echo ' 
        <tr> 
            <td>'. $row['make_id'] .'</td> 
            <td>'<img src="'. $row['make_pic'] . '" alt = ' . $row['alt_text'] .'>'; </td>
            <td>'. $row['make_title'].'</td> 
        </tr>'; 
    } 
    
    echo ' 
      <tr> 
        <td>&nbsp;</td> 
        <td>&nbsp;</td> 
        <td>&nbsp;</td> 
      </tr> 
    </table> 
    '; 
    
    //close the connection
    mysql_close($dbhandle);
    ?>
    any help would be great!

  8. #8
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <td>'<img src="'. $row['make_pic'] . '" alt = ' . $row['alt_text'] .'>'; </td>

    Note the ' before <img and the '; after the > for the same tag.

    Also, the alt tag for a image must be encased in quotes (" "), because if the alt has spaces in it, which it more then likely will, you will start getting very strange errors depending on what is contained in the alt, and what standard, if any that you are using.

  9. #9
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have made those alterations. no error msg is appearing although none of the pics in my db are being recognised.


    heres the link

    *** fixed it now ***


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •