Results 1 to 8 of 8

Thread: HTML within PHP (IMG)

  1. #1
    Join Date
    Dec 2009
    Posts
    54
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question HTML within PHP (IMG)

    Hi all,

    I am have this code exactly the way I want it but i wondered if you knew maybe how to add the same img in the html[<a href="<? '$filePath$img' ?>"] as in the php [echo "<img src='$filePath$img'/>";]

    Code:
    <?
    $string =array();
    $filePath='images/photos/';  
    $dir = opendir($filePath);
    while ($file = readdir($dir)) { 
       if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) { 
       $string[] = $file;
       }
    }
    while (sizeof($string) != 0){
      $img = array_pop($string);
    ?>
    <a href="<? '$filePath$img' ?>" class="galpop" data-galpop-group="gallery">
    <?
      echo "<img src='$filePath$img'/>";
    }
    ?>
    </a>
    Thanks

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,945
    Thanks
    16
    Thanked 304 Times in 303 Posts
    Blog Entries
    11

    Default

    I think you mean to echo it into the hyperlink href <a href="<?php echo "$filePath$img"; ?>" class="galpop" data-galpop-group="gallery"> but your description is confusing because both methods use PHP to put the image into the HTML. I'm assuming you meant to ask how to add the img into the href.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    moose86 (01-28-2014)

  4. #3
    Join Date
    Dec 2009
    Posts
    54
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Hi Beverley, thank you for responding, i have changed it and have also looked around as to y i am getting an error on the line beguining "<a href..." but canot find an answer, do you know what may be causing the problem?


    NEW CODE:
    PHP Code:
    <?
    $string 
    =array();
    $filePath='images/photos/';  
    $dir opendir($filePath);
    while (
    $file readdir($dir)) { 
       if (
    eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) { 
       
    $string[] = $file;
       }
    }
    while (
    sizeof($string) != 0){
      
    $img array_pop($string);
    ?>
       <a href="<? php echo "$filePath$img"?>" class="galpop" data-galpop-group="gallery"></a>;
    <?
    }
    ?>
    PS, im still new to PHP

    Thanks

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,946
    Thanks
    43
    Thanked 3,193 Times in 3,156 Posts
    Blog Entries
    12

    Default

    On the <a href line,

    Code:
    <? php
    needs to be either:

    Code:
    <?php
    or just (since it looks like you have the shorthand tags enabled):

    Code:
    <?
    I'd go with (array is assumed with $varname[] = whatever, ereg and eregi are deprecated and regardless, the regex's can be combined):

    PHP Code:
    <?php 
    $filePath 
    'images/photos/';   
    $dir opendir($filePath); 
    while (
    $file readdir($dir)) {  
        if (
    preg_match("/\.(png)|(jpg)|(gif)$/i"$file)) {  
            
    $string[] = $file
        } 

    while (
    sizeof($string) != 0){ 
        
    $img array_pop($string); 
    ?> 
    <a href="<?php echo "$filePath$img"?>" class="galpop" data-galpop-group="gallery"></a>
    <?php 

    ?>
    But you won't see anything on the page, so maybe (gives thumbnail images):

    PHP Code:
    <?php 
    $filePath 
    'images/photos/';   
    $dir opendir($filePath); 
    while (
    $file readdir($dir)) {  
        if (
    preg_match("/\.(png)|(jpg)|(gif)$/i"$file)) {  
            
    $string[] = $file
        } 

    while (
    sizeof($string) != 0){ 
        
    $img array_pop($string); 
    ?> 
    <a href="<?php echo "$filePath$img"?>" class="galpop" data-galpop-group="gallery"><img border=0 src="<?php echo "$filePath$img"?>" height=100></a>
    <?php 

    ?>
    Last edited by jscheuer1; 01-28-2014 at 07:22 AM. Reason: show full code examples
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    moose86 (01-28-2014)

  7. #5
    Join Date
    Dec 2009
    Posts
    54
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Thank You both for helping me with this issue, John your code works a treat

  8. #6
    Join Date
    Dec 2009
    Posts
    54
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Hi, got another question, can I add a statement to display words, if no pics exist in the specified folder?

    also can i make it so the file name is the caption of the pic?
    Last edited by moose86; 01-28-2014 at 09:03 PM.

  9. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by moose86 View Post
    Hi, got another question, can I add a statement to display words, if no pics exist in the specified folder?
    You'd have to keep track of whether or not there were any images found (e.g., by setting a flag $foundPics = true when you find one), and then you could print a message if none were.

    Quote Originally Posted by moose86 View Post
    also can i make it so the file name is the caption of the pic?
    Meaning, the value you find in $img? Sure:
    PHP Code:
    <img src="<?php echo $filePath.$img?>" alt="<?php echo $img?>">

  10. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,946
    Thanks
    43
    Thanked 3,193 Times in 3,156 Posts
    Blog Entries
    12

    Default

    The alt attribute is not a caption. The title attribute could be used to produce a hover tooltip. For an actual caption, an element like a span, div, etc. with that text or that text alone would be required. Like:

    PHP Code:
    <style type="text/css">
    .galdiv {
        text-align: center;
        float: left;
        margin-right: 3px;
    }
    .galdiv img {
        height: 100px;
        border-width: 0;
    }
    </style>
    <?php 
    $filePath
    ='album/pics/';   
    $dir opendir($filePath); 
    while (
    $file readdir($dir)) {  
        if (
    preg_match("/\.(png)|(jpg)|(gif)$/i"$file)) {  
            
    $string[] = $file
        } 
    }
    if(
    sizeof($string) < 1){
        echo 
    "No images found in $filePath";
    }
    while (
    sizeof($string) > 0){ 
        
    $img array_pop($string);
        
    $ifn substr($img0strrpos($img'.'));
    ?> 
    <div class="galdiv"><a href="<?php echo "$filePath$img"?>" class="galpop" data-galpop-group="gallery"><img src="<?php echo "$filePath$img"?>" alt="<?php echo $img?>" title="<?php echo $ifn?>"></a><br><?php echo $ifn?></div>
    <?php 

    ?>
    Last edited by jscheuer1; 01-29-2014 at 01:40 PM. Reason: add code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 1
    Last Post: 06-07-2011, 04:21 AM
  2. Replies: 2
    Last Post: 11-24-2009, 07:53 PM
  3. Replies: 5
    Last Post: 04-02-2007, 12:37 PM
  4. HTML Gallery - Need Javascript for HTML Array
    By LisaA in forum JavaScript
    Replies: 7
    Last Post: 08-12-2006, 04:35 PM
  5. HTML File or HTML Document?
    By djm in forum HTML
    Replies: 10
    Last Post: 08-02-2005, 02:30 PM

Tags for this Thread

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
  •