Results 1 to 9 of 9

Thread: Need Help with script

  1. #1
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Need Help with script

    multiple-random-image-php-script

    Hi guys...
    I nee help of how to call multiples images from a folder....

    I have a folder called images and it has many images in it. but i need only 10 images to display in a php webpage in 5 rows, ( 5 x 2 images in a row ) with links and random alt text from random txt files from a folder. When the webpage is reloaded, it displays another images with another alt text.

    After long search I have found this script that seems to be nearly to these i am looking for but i can't get i to work:

    Code:
        <?php
        session_start();
        $thispage = 'images.php';
        $imageDir = 'test_images/';
        $perPage = 10; //total images to display per page
        $perRow = 2; //total images per row
        if ( isset( $_GET['reset'] ) ) {
        	unset( $_SESSION['chunks'] );
        }
        if ( !isset( $_SESSION['chunks'] ) ) {
        	if ( is_dir( $imageDir ) ) {
        		if ( $dir = opendir( $imageDir ) ) {
        			while ( ( $file = readdir( $dir ) ) !== false ) {
        				if ( $file !== '.' && $file !== '..' ) {
        					$images[] = $file;
        				}
        			}
        			closedir( $dir );
        		}
        		else {
        			die("Unable to read directory - {$imageDir}");
        		}
        	}
        	shuffle( $images );
        	$_SESSION['chunks'] = array_chunk( $images,$perPage );
        }
        $page = 1;
        if ( isset( $_GET['page'] ) && $_GET['page'] > 0 ) {
        	$page = (int) $_GET['page'];
        }
        $chunk =& $_SESSION['chunks'];
        $total = count( $chunk );
        if ( isset( $chunk[$page-1] ) ) {
        	$images = $chunk[$page-1];
        	$html = "<table cellspacing=\"0\" cellpadding=\"3\">\n\t<tr>\n";
        	$i = 0;
        	foreach( $images as $image ) {
        		if ( $i == $perRow ) {
        			$html .= "\t</tr>\n\t<tr>\n";
        			$i = 0;
        		}
        		$html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
        		$i++;
        	}
        	$html .= "\t<tr>\n";
        	if ( $total > 1 ) {
        		$nav = '';
        		if ( $page > 1 ) {
        			$nav .= "<a href=\"{$thispage}?page=1\">[FIRST]</a><a href=\"{$thispage}?page=" . ( $page - 1 ) . "\">[PREV]</a>";
        		}
        		$i = 1;
        		while( $i < $total + 1 ) {
        			$nav .= "<a href=\"{$thispage}?page={$i}\">[{$i}]</a>";
        			$i++;
        		}
        		if ( $page < $total ) {
        			$nav .= "<a href=\"{$thispage}?page=" . ( $page + 1 ) . "\">[NEXT]</a><a href=\"{$thispage}?page={$total}\">[LAST]</a>";
        		}
        		$html .= "\t<tr>\n\t\t<td colspan=\"{$perRow}\">{$nav}</td>\n\t</tr>\n";
        	}
        	$html .= "</table><br /><br /><a href=\"{$thispage}?reset=true\">Shuffle Images</a>";
        	echo $html;
        }
        ?>

  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

    Tested and works here:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <table><tr>
    <?php
    $path = "../"; // set path to images, include trailing slash
    $txtpath = "./"; // set path to texts, include trailing slash
    $imar = glob("$path{*.jpg, *.gif}", GLOB_BRACE); // grab images (include other image patterns if/as desired)
    $txtar = glob("$txtpath*.txt"); // grab texts
    shuffle($txtar); // random order for texts
    shuffle($imar); // random order for images
    $tnum = -1; // counter for texts
    $eol = PHP_EOL; // end of line reference
    foreach($imar as $image){
    	if(++$tnum < 10){ // limit to 10
    		$txt = file($txtar[$tnum], FILE_IGNORE_NEW_LINES); // get link and alt
    		if($tnum){echo !($tnum % 2)? "</tr><tr>$eol" : $eol;} // 2 to a row
    		echo '<td><a href="' . $txt[0] . '"><img src="' . $image . '" alt="' . $txt[1] . '"></a></td>'; // table cell template
    	} else {echo $eol; break;} // all done
    }
    ?>
    </tr></table>
    </body>
    </html>
    Typical text file (must each use the .txt extension and must be at least ten of them):

    Code:
    http://www.google.com/
    some text
    The first line is the link, the second line is the alt text.
    Last edited by jscheuer1; 08-21-2017 at 05:46 AM. Reason: code improvement
    - John
    ________________________

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

  3. #3
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default

    thank you, it works. I have two questions:
    I have observed that many duplicates are shown. Is it possible that the script can check if an image is already being shown? 2. Question: Is it possible to define the link outside the alt texts?

  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

    I'm not sure what you mean by duplicates. Unless you have duplicate images, only one of any particular image should appear on any particular page load. However, since random means random, the same image may appear on one or more subsequent page loads. The fewer images you have, the more likely this is to happen. If you have like 100 images, links, and alts, this shouldn't happen too much. But it is possible to set things up so that as things are viewed, they're taken off a list until there are not enough left (less than 10) then the list is repopulated from the full pool of files again. But that is only an effective strategy to minimize duplication for a single user. If there's much traffic, or time between visits, it won't look any different to any particular user than it does now. If you have limited images, or if this is a critical issue, each user could be tracked on a session basis, but that could run into a lot of overhead for the server.

    As for defining the link outside the alt text, you can do that, but why? You actually could define all possible links and alts in a single flat file and have those all shuffled and picked from randomly. Or, if you really like having many files lying around, you could define the alts, one per file, and the links one per file. Or you could have all links in one file, and all alts in another file. That last might be easiest to maintain and to randomize independently. That's assuming you want a given link to have the opportunity to be paired with any given alt. Do you?

    Other possibilities for listing things exist. If I new the overall thrust of the project better, I could be more specific about what might work best.
    Last edited by jscheuer1; 08-22-2017 at 02:18 PM. Reason: add info
    - John
    ________________________

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

  5. #5
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default

    Sorry, my english is not the best. I will try to explain again.if I understand it right, and so does the script work by me,
    I must have at least 10 different alt texts in 10 individual txt files. In each of these files I have to add also the link
    as the first line. All in a single txt file does not work for me or I did not understand how to do it right.
    I meant all alt texts in a single txt file line for line in a defined directory and same for links. from which the text and the link will be randomly selected...

  6. #6
    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

    I'm still not completely clear about what you want. I think it might be easiest to put all links in one file, one link per line, and all alts in another file, one alt per line. If we do that, the code would have to be changed, but I will do that once I know what you want. With that setup, each link and each alt could be chosen separately and randomly from each other, which I think is what you want. We could also have one link per file and one alt per file, either in different folders or with different file naming to distinguish links from alts and still get the same result and the code would also need to be changed, but in a different way, and I would do that, but it would be more work for the server, and I think harder to maintain if you were to want to add or remove links or alts. Either way, as long as you had at least ten links and at least ten alts, you could have more links than alts or more alts than links, or of course, if you wanted, the same number of each.

    The code I wrote before though makes it so that each link and alt are always paired, the same link always gets the same alt, and there have to be the same number of each. That's what I think you want to change, right? You want it so that any link can have any alt, right?
    Last edited by jscheuer1; 08-23-2017 at 06:10 PM.
    - John
    ________________________

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

  7. #7
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default

    Right, thats what I want - any link can have any alt. All links in one file, one link per line, and all alts in another file, one alt per line. Each link and each alt could be chosen separately and randomly from each other

  8. #8
    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

    OK, here's the modified code (tested and working here):

    PHP Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <table><tr>
    <?php
    $path 
    "../"// set path to images, include trailing slash
    $linkfile "links.txt"// set path and filename to links file
    $altfile "alts.txt"// set path and filename to alts file
    $imar glob("$path{*.jpg, *.gif}"GLOB_BRACE); // grab images as array (include other image patterns if/as desired)
    $links file($linkfileFILE_IGNORE_NEW_LINES); // grab links as array
    $alts file($altfileFILE_IGNORE_NEW_LINES); // grab alts as array
    shuffle($links); // random order for links
    shuffle($alts); // random order for alts
    shuffle($imar); // random order for images
    $tnum = -1// counter for texts
    $eol PHP_EOL// end of line reference
    foreach($imar as $image){
        if(++
    $tnum 10){ // limit to 10
            
    if($tnum){$html[] = !($tnum 2)? "</tr>$eol<tr>" $eol;} // 2 to a row
            
    $html[] = '<td><a href="' $links[$tnum] . '"><img src="' $image '" alt="' $alts[$tnum] . '"></a></td>'// table cell template
        
    } else {$html[] = $eol; break;} // all done
    }
    echo 
    implode($html); // write result
    ?>
    </tr></table>
    </body>
    </html>
    And here is my links.txt (needs at least ten lines, use your own, one of these is local to my system, so won't work for you anyway):

    Code:
    http://www.google.com/
    http://decoymag.com/classifieds/classifi.htm
    http://www.dynamicdrive.com/
    http://localhost/demos/tidbits/mlb/mlbjqhwformatednest.php
    http://www.dynamicdrive.com/forums/showthread.php?81310-Need-Help-with-script
    http://www.wussu.com/laotzu/laotzu48.html
    https://code.tutsplus.com/tutorials/quick-tip-loop-through-folders-with-phps-glob--net-11274
    http://php.net/manual/en/function.array-pop.php
    http://1lineart.kulaone.com/#/
    https://www.mlb.com/
    The alts.txt I used (again, must have at least ten lines):

    Code:
    some text
    Decoys
    Dynamic Drive
    Games
    The Question
    Tao
    Glob Loops
    Array_Pop
    Line Art
    MLB DOT COM
    The more unique images, links, and alts you use, the less repetitions you will see. I used 21 images, which was OK, I'd suggest at least 50 of each though.

    Any problems or questions, just let me know.
    - John
    ________________________

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

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

    Marquis (08-24-2017)

  10. #9
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default

    Thank you very very much. This was exactly what i was searching for !

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
  •