[How] Random image + fixed text

Discussion in 'PHP' started by tovidebne, Mar 28, 2012.

  1. #1
    hi,
    random image php script
    
    <?php
    $imgpath = "img";
    $handle = opendir( "$imgpath" );
    
    $imgArray = array();
    
    while($file = readdir($handle))
    {
        if( $file != "." && $file != ".." )
        {
            array_push( $imgArray, $file );
        }
    }
    closedir( $handle );
    
    mt_srand( (double)microtime( ) * 1000000 );
    $randval = mt_rand( 0, sizeof( $imgArray ) - 1 );
    
    print( "<IMG SRC=\"$imgpath/" . $imgArray[ $randval ] . "\">" );
    ?>
    
    Code (markup):
    How to add text(not random) to every random image
    example:
    pic1.jpg here is text for image 1
    pic2.jpg here is text for image 2
    etc
    when random image is pic1.jpg, it must always be "here is text for image 1"
    Is it possible to make a txt files with names pic1.txt, pic2.txt ....etc and when pic1.jpg appear to show and pic1.txt ?
    any help is welcome
     
    Solved! View solution.
    tovidebne, Mar 28, 2012 IP
  2. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #2
    This should work:
    
    <?php
    $text = array();
    
    $file = file_get_contents("text.txt");
    $lines = explode("\n", $file);
    
    foreach ($lines as $line){
    	$splitLine = explode(";", $line);
    	$text[$splitLine[0]] = $splitLine[1];
    }
    ?>
    
    Code (markup):
    example of text.txt:
    
    pic1.jpg;text for pic1
    pic2.jpg;text for pic2
    pic3.jpg;text for pic3
    
    Code (markup):
    Usage:
    echo $text[$randval];
     
    Arttu, Mar 28, 2012 IP
  3. tovidebne

    tovidebne Active Member

    Messages:
    251
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    this piece of code sounds good but not working :(
     
    tovidebne, Mar 28, 2012 IP
  4. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #4
    It should work, did you get any errors?
     
    Arttu, Mar 28, 2012 IP
  5. earnnet

    earnnet Member

    Messages:
    100
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #5
    you should try the easier way with
    $i=rand(0,count($imagearray));
    $randomimage= $imagearray[$i];
     
    earnnet, Mar 28, 2012 IP
  6. tovidebne

    tovidebne Active Member

    Messages:
    251
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #6
    nothing
    no error, no text, just nothing
    may be im wrong
     
    tovidebne, Mar 28, 2012 IP
  7. #7
    Sorry, my bad.
    echo $text[$randval];
    should be
    echo $text[$imgArray[$randval]];
     
    Arttu, Mar 29, 2012 IP
  8. tovidebne

    tovidebne Active Member

    Messages:
    251
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #8
    its work like a charm
    ty
     
    tovidebne, Mar 29, 2012 IP