catergory rotate

Discussion in 'PHP' started by irdogg, Sep 27, 2012.

  1. #1
    if(!$_GET['categ_id']) $_GET['categ_id'] = 6;
    Code (markup):

    tried
    $category = isset($_GET['categ_id']) ? $_GET['categ_id'] : rand(1,3,8,5);
    Code (markup):
    can i call random numbers?
     
    irdogg, Sep 27, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Since mt_rand and rand use a range, I would use an array of available numbers.

    Something like this.

    $allowed = array(1,3,8,5);
    
    $category = isset($_GET['categ_id']) ? (int) $_GET['categ_id'] : $allowed[array_rand($allowed)];
    PHP:
     
    jestep, Sep 27, 2012 IP
  3. irdogg

    irdogg Well-Known Member

    Messages:
    358
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    135
    #3
    nope didnt work :(
     
    irdogg, Sep 27, 2012 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    What's not working? I tested it and it returns one of the values every time.
     
    jestep, Sep 27, 2012 IP
  5. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #5
    Works for me, too. I was going to post similar code, but John beat me to it. :D
     
    lektrikpuke, Sep 27, 2012 IP
  6. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #6
    Sorry, J beat me to it
     
    lektrikpuke, Sep 27, 2012 IP
  7. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #7
    I was going to post:
    
    $category = isset($_GET['categ_id']) ? $_GET['categ_id'] : rand(1,8); // if you want a random number between 1 and 8
    echo $category . "<br />\n";
    
    $arr = array(1,3,8,5);
    $category2 = isset($_GET['categ_id2']) ? $_GET['categ_id2'] : $arr[array_rand($arr,1)]; // if you want only the numbers listed in the array
    echo $category2;
    
    Code (markup):
     
    lektrikpuke, Sep 27, 2012 IP