SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone know how to do this?

    I set up this array today which contains the following numbers:

    6 | 7 | 9 | 10 | 11 | 47 | 360 | 1108 | 1124 | 1125 | 1129 | 1159 | 1410 | 1427 | 1431 | 1453 | 1463 | 1490 | 1496 | 1507 | 1533 | 1534 | 1546 | 1590 | 1614 | 1644 | 1648 | 1649 | 1664 | 1680 | 1687 | 1702 | 1715 | 1717 | 1733 | 1740 | 1743 | 1756 | 1762 | 1763 | 1771 | 1775 | 1793 | 1828 | 1829 | 1831 | 1833 | 1860 | 1875 | 1894 | 1932 | 1944 | 1945 | 1946 | 1947 | 1976 | 1999 | 2001 | 2025 | 2026 | 2087 | 2104 | 2106 | 2137 | 2138 | 2170 | 2171 | 2197 | 2224

    The name of the array is '$data_getsohhliveID'.

    So I used the function 'array_rand($data_getsohhliveID);' to pick a random number from the array but it's picking numbers that aren't contained in the array.

    Any suggestions would be great.
    <///////~

    www.sohh.com

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember that array_rand return the KEY for your array and NOT the value at that key.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok........so how do I get it to return the value?
    <///////~

    www.sohh.com

  4. #4
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $random_element $data_getsohhliveID[array_rand($data_getsohhliveID)]; 
    That should do the trick - if not then try this:
    PHP Code:
    $rand_id array_rand($data_getsohhliveID);
    $random_element $data_getsohhliveID[$rand_id]; 
    Cheers,

    Skunk

  5. #5
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't seem to be working. It doesn't cause an error, but it prints out the word "Array".

    This is how I have it implemented:

    $query_getsohhliveID = "SELECT tbl_content.contentID, tbl_subsection.sectionID FROM tbl_content, tbl_subsection WHERE tbl_content.contentsubsectionID = tbl_subsection.subsectionID AND tbl_subsection.sectionID = 2";

    $result_getsohhliveID = mysql_query($query_getsohhliveID);

    if (!$result_getsohhliveID) {
    echo "<b>Error performing query/b> " . mysql_error();
    exit;
    }

    while($row_getsohhliveID = mysql_fetch_array($result_getsohhliveID)) {
    $data_getsohhliveID[] = array("contentID" => $row_getsohhliveID["contentID"]);
    }

    $random_element = $data_getsohhliveID[array_rand($data_getsohhliveID)];
    echo $random_element;

    See it in action: http://www.sohh.com/templates/sitewi...sidebar_op.php

    The end result would be that it would randomly change both piece of content.

    weird...somedays I just hate PHP ...and other days I love it...
    Last edited by ibeblunt; Feb 16, 2001 at 10:31.
    <///////~

    www.sohh.com

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Becaus eyou made your array an associatev array one easy fix to your solution is to change
    PHP Code:
    $random_element $data_getsohhliveID[array_rand($data_getsohhliveID)]; 
    echo 
    $random_element
    PHP Code:
    $random_element $data_getsohhliveID[array_rand($data_getsohhliveID)]["contentID"]; 
    echo 
    $random_element
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That seemed to do the trick. It's just not very random. Seems that the top area keeps picking #6 and the bottom area shows up every now and then, when it should always show up. But thats no fault of yours.

    You mentioned I made my array an associate array. Are there other arrays? and if so would another type of array work better for me?
    <///////~

    www.sohh.com

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is another option and it may give you a better chance for randomizing


    $query_getsohhliveID = "SELECT tbl_content.contentID, tbl_subsection.sectionID FROM tbl_content, tbl_subsection WHERE tbl_content.contentsubsectionID = tbl_subsection.subsectionID AND tbl_subsection.sectionID = 2";

    $result_getsohhliveID = mysql_query($query_getsohhliveID);

    if (!$result_getsohhliveID) {
    echo "<b>Error performing query/b> " . mysql_error();
    exit;
    }

    while($row_getsohhliveID = mysql_fetch_array($result_getsohhliveID)) {
    $data_getsohhliveID[] = $row_getsohhliveID["contentID"];
    }


    srand((double)microtime()*1000000);
    $randval = rand (0,(count($array)-1));
    $radnom_element = $array[$randval];
    print $random_element;
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As Freddy pointed out, before you call any random function, call the random generator and throw it a seed. (srand or mt_srand) This will make thing more random-ish
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


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
  •