Thread: php help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    22
    Rep Power
    0

    php help


    Inside my your function, i have to use the rand function, along with the count function, to randomly select one of the array values. Use the return keyword to return this value and end the function. The value returned should be one of the provided file names. What i have so far is below, any help would be good


    Code:
    <html>
    <head>
    <title>JFQ Turnings</title>
    </head>
    <body bgcolor="#ffffff" text="#000000">
    <img src="jfqturnings.gif" alt="JFQ Turnings, coming soon." width="864" height="100">
    <br /><img src="table.jpg" alt="" width="864" height="567">
    <?php>
    function random image(){
    	$images = array("candlestick.jpg", "rollingpin.jpg", "table.jpg", "table2.jpg");
        $random = rand(0, 3);
    	
    }
    ?>
    </body>
    </html>
    <?php
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,473
    Rep Power
    653
    First please use the proper tags for you PHP code. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    It is not clear what you are asking. You generate a random number but don't use it, presumable as an index for the array so it appears you did not post all the code. What error are you getting or what do you get that is different form what you expect?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Burb of Detroit, Michigan
    Posts
    107
    Rep Power
    97
    If you just want to randomize the images each time someone visits the website, you can simply do something like:

    PHP Code:
    $images = array("candlestick.jpg""rollingpin.jpg""table.jpg""table2.jpg");

    echo 
    '<h2>Array as is</h2><pre>'print_r($images1) . '</pre>';

    shuffle($images);

    echo 
    '<h2>Array randomized : </h2><pre>'print_r($images1) . '</pre>'
    Visiting php.net can help you a lot.
    Last edited by Strider64; February 14th, 2015 at 11:47 AM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    22
    Rep Power
    0
    I want just a single item, not the entire array. This code will print the entire array, shuffle it, and then print the entire array again. I need to use rand() to set the index to different numbers (as opposed to shuffling the array so different elements appear at the same index)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,473
    Rep Power
    653
    Understood, which is what you have done but you don't use that index anywhere. What is the issue?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    425
    Rep Power
    0
    As already mentioned, do read the sticky messages. They would tell you to use proper tags for your code,
    and to also use a REAL description of your problem as your subject. Your subject of "PHP help" fits every
    single thread in this forum, and is not specific to your question.


    The simple answer is, you are missing:
    PHP Code:
       return $images[$random]; 
    I'd combine a couple of lines, and use mt_rand(), but rand() will be fine for your situation.
  12. #7
  13. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,424
    Rep Power
    1036
    PHP Code:
    $images = array("candlestick.jpg""rollingpin.jpg""table.jpg""table2.jpg");
    $randimage $images[rand(0,count($images))]; 

    Comments on this post

    • ttremain agrees : Even better, by using count()
    Last edited by Hammer65; February 16th, 2015 at 12:59 PM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    425
    Rep Power
    0
    Originally Posted by Hammer65
    PHP Code:
    $images = array("candlestick.jpg""rollingpin.jpg""table.jpg""table2.jpg");
    $randimage $images[rand(0,count(images))]; 
    Even better, by using count, but you can't forget the $ in front of images...

    PHP Code:
    $randimage $images[rand(0,count($images))]; 
  16. #9
  17. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,424
    Rep Power
    1036
    Originally Posted by ttremain
    Even better, by using count, but you can't forget the $ in front of images...

    PHP Code:
    $randimage $images[rand(0,count($images))]; 
    Har har what are you the syntax police?

    sorry about that but yes using count you don't have to manually count the number of elements. If you add any it will still be right.
    Last edited by Hammer65; February 16th, 2015 at 01:01 PM.
    Coder Central Tutorials, news and information for the programming community at large.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    22
    Rep Power
    0
    This is my code below, what i wanna do is to make one image is being displayed to the visitor, which is why i am using rand(), so they see a different image each time. My problem is, Instead of table.jpg

    <br /><img src="table.jpg" alt="" width="864" height="567">

    it should be the image filename that is being returned from the function.
    Since i am new to php, i have no idea what im supposed to do.

    <html>
    <head>
    <title>JFQ Turnings</title>
    </head>
    <body bgcolor="#ffffff" text="#000000">
    <img src="jfqturnings.gif" alt="JFQ Turnings, coming soon." width="864" height="100">
    <br /><img src="table.jpg" alt="" width="864" height="567">
    <?php
    echo getRandomImage();
    function getRandomImage()
    {
    $images = array("candlestick.jpg", "rollingpin.jpg", "table.jpg", "table2.jpg");
    $random = rand(0, 3);
    return $images[$random];
    }
    ?>
    </body>
    </html>
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2015
    Posts
    22
    Rep Power
    0
    Sorry guys forgot to put code in proper tags
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    425
    Rep Power
    0
    Originally Posted by Hammer65

    Har har what are you the syntax police?
    Nope, just the OP seemed a bit inexperienced, and I didn't want it to trip him up.
  24. #13
  25. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,569
    Rep Power
    131
    Turn
    <img src="table.jpg" alt="" width="864" height="567">
    into one of
    <img src="<?php echo getRandomImage(); ?>" alt="" width="864" height="567">
    <img src="<?=getRandomImage(); ?>" alt="" width="864" height="567">(If short_open_tag is enabled, or using PHP 5.4+)

    Comments on this post

    • BarryG disagrees : Don't use If shortcut php tags is not enabled (and it shouldn't be), you'll get the code and not the image.
    Last edited by Triple_Nothing; February 17th, 2015 at 05:05 PM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.

IMN logo majestic logo threadwatch logo seochat tools logo