SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    articles with images in a MySql db

    Hello,

    this is my first visit to sitepoint... I think like many before me I've been inspired by Kevin Yank's article to do some database backing up of my site. But i've run into a problem.

    I'd like to put all my articles into a database, and the articles have images. I was thinking of doing something similar to vB code in placing tags for where the images will appear in the article's text. Something like [[image=filename.jpg]]. When the article is displayed it should take the filename.jpg and insert the appropriate html img tag.

    With ereg_replace I can do most of want I want but not all. The problem I'm having is that I would like to include the width and height of the image in the tag. That requires looking this up dynamically for each image. I tried doing this with ereg_replace by capturing the image filename and passing it to a function which would use the filename to look up the dimensions, but I found out that the only thing passed to the function is the string '\1' and not the actual filename. So I can't actually use this to look up the file, although I can use it fine to define the replacement string in ereg_replace!

    I don't know if I'm being terribly clear here... if it's a problem anybody can help me with I could post some code. I know that I can get by without specifying the width and height in an img tag, but I would also like to be able to look up other info on the image which is in my db, like a caption.

    Any ideas for how I could solve this problem, or even just organize my data differently/better ??

    thanks in advance,
    john nash

  2. #2
    SitePoint Zealot Overgrow's Avatar
    Join Date
    Jan 2001
    Location
    Your basement
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I feel your pain but one clarification needed before answering:

    How are you looking up the width/height? Using PHP functions or are you storing this info in the DB when you store the file?

    ...sounds like you are storing the info right?
    Overgrow the Government: The Edge Forums
    presented by Overgrow Marijuana Magazine

    *warning: adult content

  3. #3
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, I'm just trying to design things now, but I was thinking of looking up the dimensions of the image with a php function, and getting the caption information from a db table.

    But I'm flexible to anything that will work...

    thanks,
    john

  4. #4
    SitePoint Zealot Overgrow's Avatar
    Join Date
    Jan 2001
    Location
    Your basement
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmmmmmmmmmmmmmmmmmmmmm

    You have a challenging problem but one that I might see in the future with my CMS... so here is probably how I would tackle it.

    Your main issue is that you are having problems calling functions from inside of ereg_replace. I'm not surprised at that but I haven't done testing on my own. My first idea (and likely yours too) would be something like this:

    PHP Code:
    $story=preg_replace ("|<img>(.*)</img>|"
    "<a target=\"_blank\" href=\"image.php?topic=$topic&articleid=$articleid&imagename=\\1\"><img src=\"/articles/images/$topic/\\1_t.jpg\" border=0 class=\"img-art\" width=\"".getImageWidth(\1height=\"".getImageHeight(\1)."."></a>", 
    $story); 
    If that didn't work.. because as you said the function is only receiving \1 and not the image name then you are probably going to have to put these images in an array, fetch the appropriate info, and then re-replace the image tags with another set of ereg_replace (or preg). Ouch.. I'm open to new ideas on this too.
    Overgrow the Government: The Edge Forums
    presented by Overgrow Marijuana Magazine

    *warning: adult content

  5. #5
    SitePoint Member
    Join Date
    Jun 2001
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Overglow, thanks for tackling my problem!
    I tried what you suggested and ran into the same problem, with //1 being passed rather than the matched parameter. But I learned from your post about preg_replace which i didn't know about and found out in a php.net posting that it can do what I needed. Here's the code:


    PHP Code:
    $newstr preg_replace("/\[(\w+)\]/e""dostuff('\\1')"$str); 
    The trick is to add /e at the end of the pattern. Don't know why ereg_replace can't do this... now I need to learn perl expressions, it never ends ...

    john


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
  •