SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Posting individual images

    I've been trying to fiddle with this code to no avail, my goal is to... well basically what the title says, post the individual images, but all the code does is post everything from the folder and that's not what I want... Here is the code:

    PHP Code:
        $dir_contents scandir($dir);

            foreach (
    $dir_contents as $file
            {
            
    $file_type strtolower(end(explode('.'$file)));
                
                    if (
    $file !== '.' && $file !== '..' && in_array($file_type$file_display) == true
                    {
                    
                    
    $animeimg '<img src="' $dir '/' $file '" alt="' $file '" width="80px;" height="100px" />';
                    echo 
    $file;
                    
                    
                    
    $template->assign_block_vars('animeimg', array(
                        
    'IMG'        => $animeimg,
                    ));
                    
                    
    $sql "SELECT image, name, description FROM phpbb_anime";
                    
    $result $db->sql_query($sql);

                    
                    }
            } 

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Where did you define $file_display?

    And.. what's that SQL query doing down there?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad, forgot to post that...

    PHP Code:
    $file_display = array('jpg''jpeg''png''gif'); 
    and does it matter where I put the SQL query? (PHP noob here, sry) and if that's not the right spot, where should I put it?

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    It wasnt a question of positioning so much as "what is it actually DOING?" It doesnt appear to have any use - as soon as the next iteration of the loop is called, your query re-executes itself, and...still does nothing with the result.

    If you need the information from the query later on in the script, pull the query out of the loop so you only run it once, and still have the result available.

    When you say 'post the individual images'... what do you mean? This script is clearly designed to post all image files in a directory.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I guess that's idiotic of my part that SQL statement actual refers to another code that I haven't posted lol

    PHP Code:
    while ($row $db->sql_fetchrow($result))
                    {
                        
    $template->assign_block_vars('anime', array(
                            
    'ANIME_NAME'    => $row['name'],
                            
    'ANIME_DESC'    => $row['description'],
                        ));
                    } 
    and when I say post the individual images, I'm wanting to be able to define a variable that can loop through all the images like have the template variable 'IMG' and so I can put in the HTML template with the description and name, and keep looping until no more images are available, I don't want to post all the images at the same time. I hope that makes sense.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Okay. Think we need to adjust some stuff.

    You want the name, description, and image all tied together. So, lets put them all together.

    I assume that the 'image' field in the sql table holds the file name of the image file.

    SO. We dont need to scan the directory at all.

    PHP Code:
                    if(substr($dir,-1) != "/") { $dir .= "/"; }
                    
    $sql "SELECT image, name, description FROM phpbb_anime";
                    
    $result $db->sql_query($sql); 
    while (
    $row $db->sql_fetchrow($result))
                    {
                        
    $template->assign_block_vars('anime', array(
                            
    'ANIME_NAME'    => $row['name'],
                            
    'ANIME_DESC'    => $row['description'],
                            
    'ANIME_IMG'      =>  (file_exists($dir.$row['image']) ? '<img src="'.$dir.$row['image'].'" alt="'.$row['image'].'" width="80px;" height="100px" />' '<img src="missing.jpg" alt="Image Missing" width="80px;" height="100px" />')
                        ));
                    } 
    Now we just need to modify your template file to loop correctly through the array we just sent. So if you could post the template file you're using (or at least the relevant section), we can fix that.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I just tried to modify it myself, and when i did the image turned into this
    PHP Code:
    <img src="missing.jpg" alt="Image Missing" width="80px;" height="100px"
    but here is the template you requested

    PHP Code:
    <!-- BEGIN anime -->
             <
    div class="anime">
            {
    anime.ANIME_IMG}
            <
    h2>{anime.ANIME_NAME}</h2>
                <
    p>{anime.ANIME_DESC}</p>
            </
    div>
            <!-- 
    END anime --> 

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    What is in the image field of the database?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the file path, the actual image is stored in a folder.

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    if it's the full filepath, remove the $dir entirely, because you dont need it :P
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did that and still nothing, but I think the

    PHP Code:
    <img src="missing.jpg" alt="Image Missing" width="80px;" height="100px"
    error is coming

    PHP Code:
    (file_exists($dir.$row['image']) ? '<img src="'.$dir.$row['image'].'" alt="'.$row['image'].'" width="80px;" height="100px" />' '<img src="missing.jpg" alt="Image Missing" width="80px;" height="100px" />'
    after the colon is where I see it happening, but i take it out, it just screws up entirely (very confused right now)

  12. #12
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    It's a Ternary condition.

    (ifcondition) ? trueresult : falseresult;

    It's evaluating (file_exists($dir.$row['image']) as False. Which, if $row['image'] contains the full path, is the correct result. It should be (file_exists($row['image'])
    instead.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  13. #13
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I'm starting to understand it now, but I still can't get the images to show up, and when I put the file path into the browser URL, the image will show up there, but not for the site, and I took out that first line of code, since you said $dir wasn't needed anymore, I hope that didn't mess anything up...

  14. #14
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Is it still sending Image Missing out, instead of the image?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  15. #15
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, still getting that missing.jpg

  16. #16
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Couldn't edit the post, but I finally got it working, the file path in the database was wrong lol, I had it as localhost/images/, when it was supposed to be just images/ but thank you for all your help StarLion, couldn't have done without you

  17. #17
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    hehe well yes, that would certainly do it
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •