1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Displaying captions, when grabbing images from directory.

Discussion in 'PHP' started by ziando, Apr 27, 2015.

  1. #1
    Hey,

    I'm just starting out with php and I've managed to pull images from a directory, and I would like to have captions added to each file that is being pulled.

    Very basic, I have:
    <?php
    $images = glob("directory/filename.jpg");
    foreach($images as $image) {
    echo '<div class="imgholder"><img src="'.$image.'"><div class="mask">'.$caption.'</div></div>';
    }
    ?>

    Now, I suppose I should use a an array, that checks $images for its filename, and associate specific filename with an caption to be displayed in the var $captions.
    How can I do this very simple?

    PHP beginner,
    Ziando
     
    ziando, Apr 27, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    No. What you should do is store filenames, extension and caption in a database, and pull the information from the database (basically you create a db-call and check the table with the pictures), and when you get the filenames, you pull the matching file from whereever they're stored.
     
    PoPSiCLe, Apr 27, 2015 IP
  3. ziando

    ziando Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    I was planning to do it without mysql.
     
    ziando, Apr 27, 2015 IP
  4. ziando

    ziando Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Solved it myself, thanks tho.
     
    ziando, Apr 27, 2015 IP
  5. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #5
    And how did you solve it? maby nice to share the solution for others?
     
    EricBruggema, Apr 27, 2015 IP
  6. ziando

    ziando Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    I decided to create a file, that listed the caption seperated with an "," followed by the url. Example: My image title,directory/pic001.jpg
    Then i had it search for the "," and then post the both parts in an echo.

    <?php

    $f = fopen("imagedirectory/captions.txt", "r");

    while (!feof($f)) {

    $captions = explode(",",fgets($f));

    echo '<div class="imageholder"><img src="'. $captions[1].'"><div class="mask">'.utf8_encode($captions[0]).'</div></div>';

    }

    fclose($f);

    ?>

    Had to use the encode, since i use ÅÄÖ in my captions. Don't know if I used it properly. But then again, I am new at this.
     
    ziando, Apr 28, 2015 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    That's a perfectly fine solution, in that it works. But it's not a workable solution - in that it needs constant manual updates etc. whenever you upload a new picture, for instance (dunno if that is a real problem, but it should at least be considered).
    Also, there's no need for the UTF-8 encode, if the website itself is coded correctly, and the txt-file saved as UTF-8.
     
    PoPSiCLe, Apr 28, 2015 IP
  8. ziando

    ziando Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Yup, sorted out the utf-8 problem, you were right, my textfile was saved as ANSI.
     
    ziando, Apr 29, 2015 IP
  9. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    43
    #9
    Why is that? update your script to support json, usign php you can write to files you know...
    http://php.net/manual/en/function.file-put-contents.php

    step 1 : encode array to json + write to file
    step 2 : read from file & decode json to array

    Not that big of a deal...
     
    freelanceDeveloper, May 5, 2015 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Unless he has some sort of upload-function which allows him to add to the original file, whenever he uploads a new file, he'll have to add to that text-file manually (or, by all means, via a form) - granted, the same goes of course for storing the information in a database, but at least then it's possible to retrieve certain bits when needed (search, the ability to easily update, etc). Find a specific value in a text-file like he was talking about, and update the caption, for instance, is a pain in the ass, and will (more often than not) involve rewriting the whole file every time. If the file grows in size as well, you're gonna end up with other issues. So no, it's not a good solution. It's a workable, limited solution for small file-sets and not much change.
     
    PoPSiCLe, May 6, 2015 IP
  11. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    43
    #11
    true, but a session is a file as well, not often i see people care about the size it gets.

    I would opt for a db , but maybe his host doesn't support it ...
    if he doesn't want to use a database... a file is the next best thing...
     
    freelanceDeveloper, May 6, 2015 IP
  12. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #12
    If his host doesn't support or provide a database, he needs a new host. If he doesn't want to use a database... well, sure, but then again, why create a more complex way of doing something when there are better options out there. However, all this is just guesswork, and not really relevant - there are "best practices" and then there are "it works" - I know what I would chose.
     
    PoPSiCLe, May 6, 2015 IP