SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Help me please

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Malaysia
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me please

    PHP Code:
    <?php 
    //
    //this is to generate a random picture from a selected directory.
    //the image files must follow the pattern: 1.jpg, 2.jpg, 3.jpg.
    //

    //
    //Things to check and change.
    //
    $folder "../pic/"//path to the folder. (html style) (leave trailing slash)
    $number 20//number of pictures in the folder

    //
    //The actual program itself.
    //

    //getting a random number from 1 to whatever user specified.
    $random_number rand(120);

    //changing the number into a string for use with the image path.
    $random_number settype($random_number"string");

    //generating the image path.
    $image=$folder.$random_number.".jpg";

    //displaying the image.
    echo "<img src=\"$image\">";

    //Volia :)

    ?>
    The code is commented and pretty self explanatory, the only problem is that I am getting only one image (1.jpg) shown all the time.

    Could someone tell me where I went wrong? Thanks

  2. #2
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take out the
    PHP Code:
    //changing the number into a string for use with the image path.
    $random_number settype($random_number"string"); 
    and you should be fine.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Malaysia
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me try...

  4. #4
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Malaysia
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it works.

    But why?

  5. #5
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To be honest with you, I'm not sure why; I just tested it out on my server and it seemed to do the trick.

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's because settype returns a boolean true on success, not the value of its argument. So you had assigned TRUE to $random_number, and since you'd also set $random_number's type to string it became "1".

    So you should have just done this:
    PHP Code:
    //changing the number into a string for use with the image path.
    settype($random_number"string"); 
    But of course as you've discovered you didn't need to do that.

  7. #7
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Malaysia
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys


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
  •