SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: random ads

  1. #1
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    random ads

    Hey all...
    heres the 'problem'

    I have a collum in a site that has ads.. advertisers pay money to be listed in this collum, and want to be high up in hte page as possible. They want to be top of the collum all the time. I just told them that Their position would be picked randomly and then placed in the collum.. they were cool with that..

    solution
    I need to select all the ads in the database in a random order.. is that possible? or am I going to have to select them all, and then pick a random order though normal php... I would prefer just have them randomly pulled from database though


    thanx guys!!

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use

    SELECT * from TABLENAME order by rand();
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $result=mysql_query("select * from ads, category, cid_aid WHERE cid_aid.cid=category.cid AND cid_aid.aid=ads.aid AND category.cid = $cid order by rand()"); 
    "Error line 3You have an error in your SQL syntax near 'rand()' at line 1"

    last time I checked...that isnt very good

    Am I doing something wrong?

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What version of mysql are you running only 3.23 and above support ORDER BY RAND()
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bah...
    3.2

    so close

    I cant upgrade it, not my server... is there another way to pull random results?

  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)
    Get the results into php and then use php to sort the array using array_rand()
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm.. I looked at array_rand
    I dont think it does what i want it to do.
    By the looks of it, array_rand, moves the elements of the array around...

    I want the 'rows' to be moved arround

    this is my table in the database

    | cid | src |
    | 1 | a.jpg|
    | 2 | b.jpg|
    | 3 | c.jpg|

    This function just switches cid and src arround (I think)
    But I need to get the rows in a random order...

  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)
    Okay I would suggest you look into how you will get the row data into an array then use shuffle() to shuffle the array. I know I said array_rand() earlier but it should be shuffle. Here is an example of what I am talking about, you will need to change some stuff around to make it work for you I am sure.



    PHP Code:
    <?
    $db 
    mysql_connect("*********""******""**********") or DIE(mysql_error());
    mysql_select_db("dbname");
    $result mysql_query("select * from ip");
    while(
    $row mysql_fetch_array($result)) {
        
    extract($row);
        
    $array[] = array("cid" => $cid,
                         
    "src" => $src
                         
    );
        }
    srand ((double) microtime() * 10000000);
    shuffle($array);
    foreach(
    $array as $key => $val) {
        print 
    $array[$key]["cid"] ." - "$array[$key]["src"]."<br>";
        }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Am I doing ths wrong? cuz Im not getting the ads in a random order... everytime its the same thing..

    PHP Code:
    $result=mysql_query("select * from ads, category, cid_aid WHERE cid_aid.cid=category.cid AND cid_aid.aid=ads.aid AND cid_aid.cid = $cid")
            or die(
    "Error line 3" .mysql_error());


    while(
    $row mysql_fetch_array($result)) 
    {
        
    extract($row);
        
    $array[] = array("href" => $href,
                         
    "src" => $src
                         
    );
    }

    srand ((double) microtime() * 10000000);
    shuffle($array);

    foreach(
    $array as $key => $val
    {
        print 
    "<a href = '".$array[$key]["href"] ."'><img src='admin/"$array[$key]["src"]."' border = 0><br>";


  10. #10
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    never mind... I got it now


    thank you


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
  •