Results 1 to 8 of 8

Thread: How to add links to this script...

  1. #1
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to add links to this script...

    Hello,

    How can I add links to the images that get output from the following php code?

    PHP Code:
     <?php 
     $i
    =0
     
    $path="flags";  
     
    $ext "png";  
     
    $extra"alt=\"Random Image\" float=\"left\""
     if (
    $handle opendir($path)) {  
        while (
    false !== ($file readdir($handle))) {  
        if (
    substr($file,strlen($file)-3,3)==$ext)  
    {        
    $imgs[$i++]=$file

        } 
       
    closedir($handle);  
      
    $today=getdate(); 

    srand($today['mday']+$today['month']+$today['year']);  

       
    $r=rand(0,$i-1); 

       echo 
    "<img src=\"flags/{$imgs[$r]}\" alt=\"Photo\" />";

     }

    ?>
    Thank you
    Brian

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You say "links". How many links? I mean an image as would be generated here can only have one link, but I'm guessing you may want to draw from a pool of possible links. Would those also be random? Or would they correspond somehow to the image file chosen? If it's just one link, you can just add it normally around the php code block.

    Anyways, from this I think I see what the main problem may have been before with that other code we were working on. It didn't include .png images. Those could easily be included if you like.

    Out of curiosity, because it could matter at some point, what version of PHP are you using?
    - John
    ________________________

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

  3. #3
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Each link will correspond to the the image being displayed. The old script I already tried changing the code to work with png, by replacing the code
    PHP Code:
    $imar glob("$path{*.png}"GLOB_BRACE); // grab images as array (include/exclude image patterns if/as desired) 
    still didn't work, it gave me a broken image of 1082018 in the url. I am using php PHP Version 7.0.28 on Ubuntu.16.04.1

    Brian

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Did you delete the images.txt file first? And I'm wondering about the path var. From what you've said it should likely be:

    $path= "flags/";

    Anyways, works fine here, and that version of PHP should support it.

    Getting back to your question, I would use an associative array then, using the flag image filenames as keys with each value being the link you want associated with it, or you could just use the base name of the flag image file and give it a different extension to make the link. Assuming the pages you want to link to could easily be so named. Since I don't know that - Here's a simple associative array example along those lines (you would use the actual names of your flag files and the pages you want linked to each):

    PHP Code:
    <?php
    $links 
    = array("usflag.png" => "uspage.htm",
            
    "ukflag.png" => "ukpage.htm");
    print_r($links);
    echo 
    '<br>' $links["ukflag.png"];
    ?>
    Tested and works fine.

    So then applied to you code:

    Code:
     <?php  
     $i=0;  
     $path="flags";   
     $ext = "png";   
     $extra= "alt=\"Random Image\" float=\"left\"";  
     if ($handle = opendir($path)) {   
        while (false !== ($file = readdir($handle))) {   
        if (substr($file,strlen($file)-3,3)==$ext)   
    {        $imgs[$i++]=$file;  
    }  
        }  
       closedir($handle);   
      $today=getdate();  
    
    srand($today['mday']+$today['month']+$today['year']);   
    
       $r=rand(0,$i-1);  
    $links = array("usflag.png" => "uspage.htm",
    		"ukflag.png" => "ukpage.htm");
    
       echo '<a href="' . $links[$imgs[$r]] . '">' . "<img src=\"flags/{$imgs[$r]}\" alt=\"Photo\" /></a>"; 
    
     } 
    
    ?>
    As I say though, in the links array use the actual flag filenames and the actual page names you want associated with each one.

    Note: This last bit I haven't tested, might contain typo(s). But will work if done as I've outlined. Hopefully you get the idea.


    One more thing here about that other code we were working on, I'm like 99% sure it can work with your setup, so - since you did request it, perhaps we should keep trying to work it out - in that thread though, not here. You haven't addressed there the issues I asked you about, and there could be more things if you're willing to go through them there one at a time. Anything we do with links here should easily translate to that code.
    Last edited by jscheuer1; 04-18-2018 at 08:05 PM. Reason: add info about previous code/thread
    - John
    ________________________

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

  5. #5
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Did you delete the images.txt file first? And I'm wondering about the path var. From what you've said it should likely be:

    $path= "flags/";

    Anyways, works fine here, and that version of PHP should support it.

    Getting back to your question, I would use an associative array then, using the flag image filenames as keys with each value being the link you want associated with it, or you could just use the base name of the flag image file and give it a different extension to make the link. Assuming the pages you want to link to could easily be so named. Since I don't know that - Here's a simple associative array example along those lines (you would use the actual names of your flag files and the pages you want linked to each):

    PHP Code:
    <?php
    $links 
    = array("usflag.png" => "uspage.htm",
            
    "ukflag.png" => "ukpage.htm");
    print_r($links);
    echo 
    '<br>' $links["ukflag.png"];
    ?>
    Tested and works fine.

    So then applied to you code:

    Code:
     <?php  
     $i=0;  
     $path="flags";   
     $ext = "png";   
     $extra= "alt=\"Random Image\" float=\"left\"";  
     if ($handle = opendir($path)) {   
        while (false !== ($file = readdir($handle))) {   
        if (substr($file,strlen($file)-3,3)==$ext)   
    {        $imgs[$i++]=$file;  
    }  
        }  
       closedir($handle);   
      $today=getdate();  
    
    srand($today['mday']+$today['month']+$today['year']);   
    
       $r=rand(0,$i-1);  
    $links = array("usflag.png" => "uspage.htm",
    		"ukflag.png" => "ukpage.htm");
    
       echo '<a href="' . $links[$imgs[$r]] . '">' . "<img src=\"flags/{$imgs[$r]}\" alt=\"Photo\" /></a>"; 
    
     } 
    
    ?>
    As I say though, in the links array use the actual flag filenames and the actual page names you want associated with each one.

    Note: This last bit I haven't tested, might contain typo(s). But will work if done as I've outlined. Hopefully you get the idea.


    One more thing here about that other code we were working on, I'm like 99% sure it can work with your setup, so - since you did request it, perhaps we should keep trying to work it out - in that thread though, not here. You haven't addressed there the issues I asked you about, and there could be more things if you're willing to go through them there one at a time. Anything we do with links here should easily translate to that code.
    Thank you for the update John it's finally working!
    Brian

  6. #6
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    One more thing here about that other code we were working on, I'm like 99% sure it can work with your setup, so - since you did request it, perhaps we should keep trying to work it out - in that thread though, not here. You haven't addressed there the issues I asked you about, and there could be more things if you're willing to go through them there one at a time. Anything we do with links here should easily translate to that code.
    Sure...But you have to understand, I have really been struggling to get my site on line since January of this year, so I will try to do my best in testing your code as you did help me, so I will do my best to continue to work with making your code work...If we get it to work, I will try and compare the one I am using now on localhost, with your code if/when we get it working... To answer your latest message regarding the path, I did set it to flags with the trailing slash...If you want, I will post the current code that I have of yours in this thread, let me know. John...

    Brian

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yeah sure, post the entire current code, but please post it in the thread where we were working on this, for continuity for us, as well as others, on both of these separate branches of coding. That other thread is:

    http://www.dynamicdrive.com/forums/s...ight-every-day

    I'm beginning to wonder about case (upper and lower, though your current code in this thread seems to say that all your .png extensions are lower case, which would be fine) and whether or not GLOB_BRACE with only one option is supported in your version (if all you want is *.png, you don't need GLOB_BRACE, so it might not work in later versions, though works here, it can be done with a simpler GLOB expression though). But there could be a wide variety of issues. The file we used, images.txt, to track things should perhaps be changed to one that never existed. And there are tests we can do by injecting diagnostic echo statements in the code. If you're up for it, I am. I find it interesting at this point. Other potential issues may present themselves.

    No rush though, if your primary concern is going with what you have, that's fine.
    - John
    ________________________

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

  8. #8
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Yeah sure, post the entire current code, but please post it in the thread where we were working on this, for continuity for us, as well as others, on both of these separate branches of coding. That other thread is:

    http://www.dynamicdrive.com/forums/s...ight-every-day

    I'm beginning to wonder about case (upper and lower, though your current code in this thread seems to say that all your .png extensions are lower case, which would be fine) and whether or not GLOB_BRACE with only one option is supported in your version (if all you want is *.png, you don't need GLOB_BRACE, so it might not work in later versions, though works here, it can be done with a simpler GLOB expression though). But there could be a wide variety of issues. The file we used, images.txt, to track things should perhaps be changed to one that never existed. And there are tests we can do by injecting diagnostic echo statements in the code. If you're up for it, I am. I find it interesting at this point. Other potential issues may present themselves.

    No rush though, if your primary concern is going with what you have, that's fine.
    I am up for it, but right now I am sorting through some files to backup, but I will post my current code that you were working on, so you can continue to work on it...

    Thank you
    Brian

Similar Threads

  1. Replies: 0
    Last Post: 04-12-2009, 09:20 PM
  2. Sub Links Script
    By pixelzim in forum Looking for such a script or service
    Replies: 4
    Last Post: 10-25-2008, 04:42 PM
  3. Script for links?
    By satinkisses2004 in forum PHP
    Replies: 1
    Last Post: 07-19-2006, 09:42 PM
  4. links not working in script
    By mike444 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 01-26-2006, 10:55 PM
  5. Rainbow Links Script
    By bfboy in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 08-07-2005, 08:07 AM

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
  •