SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with this Pseudo Random Algorithm

    Hello

    I have a situation, where I added some links in the database with their IMPORTANCE from 1 to 10. (where 10 is HIGHEST).

    Now, i have an Ad Rotator that picks the link randomly and present that.
    BUT what I want is that if a link is given the HIGHER importance that should be presented more than once (on page loads). So For example :

    Suppose there are 5 AD Links are stored. Ad no. 2 is given a higher importance.

    User A visits PAGE and the PAGE shows AD No. 1
    User B visits PAGE and the PAGE shows AD No. 2
    User C visits PAGE and the PAGE shows AD No. 3
    User D visits PAGE and the PAGE shows AD No. 4
    User E visits PAGE and the PAGE shows AD No. 2
    User F visits PAGE and the PAGE shows AD No. 5
    User G visits PAGE and the PAGE shows AD No. 2
    ------------ a cycle of random links complete --- now it will start again
    User H visits PAGE and the PAGE shows AD No. 2
    User I visits PAGE and the PAGE shows AD No. 5
    User J visits PAGE and the PAGE shows AD No. 1
    User K visits PAGE and the PAGE shows AD No. 2
    User L visits PAGE and the PAGE shows AD No. 3
    User M visits PAGE and the PAGE shows AD No. 2

    you can see that in picking the RANDOM links, AD No. 2 was shown multiple times.

    Please help me how this can be achieved.

    Thanks
    Zeeshan Hashmi

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT ad
      FROM ads
    ORDER
        BY importance * rand()
     LIMIT 1
    This could possibly yield the same ad multiple times in a row.

    This isn't the most efficient query though if the table were to get large. A really easy way around that(if needed) would be to fake randomness. Just generate the list of ad id's ahead of time, give them a sequence, and cycle through them.

  3. #3
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your ideas !

    I will use and will let you know the results !

  4. #4
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the idea do not works.

    Some one, please guide me how this can be done!

    Thanks
    Zeeshan

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does "work", although maybe I musunderstand what you want, or maybe you implemented it wrong.

  6. #6
    SitePoint Addict goodmast3r's Avatar
    Join Date
    Apr 2005
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    higher importance? How much higher. 3 out of 10? 4 out of 10? If you have 5 ads, and ads 2 have higher importance, then randomize from 1-10. if it shows 6-10, then show ad number 2. So ad no 2 will show more than the other ads.

  7. #7
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add an "impressions" column to the table.
    Add an "importance" column to the table.

    Select a random row where "impressions" > 0.

    If there are no rows with impressions > 0 then update the table so that "impressions" column = the value of the rows "importance" column and select again.
    Code:
    UPDATE table SET impressions = importance
    Decrement the impression column for the returned row.


    A row with a higher importance is going to therefore have more impressions.

  8. #8
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hummmm I like the idea of Impressions,

    Thanks joebert !


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
  •