Putting "weight" on my query (pulling from MySQL DB)

Discussion in 'PHP' started by Doug the Great, Dec 2, 2007.

  1. #1
    Hello guys, I am a good "copycatter" if you would like to say. Currently I made a very basic randomizer that will display 8 fields that is pulled from a MySQL db table. But I want to know how I can put weight on some of the queries so they will show more often when it's pulling the 8 random queries. Here is my code.

    <?PHP
    $q=mysql_query("SELECT Name AS feat_name, URL AS feat_url, Thumbnail AS feat_thumb FROM `featured` ORDER BY RAND() ASC LIMIT 8");
    while ($line=mysql_fetch_assoc($q)) {
        echo "<td align=\"center\" valign=\"middle\">";
        echo "<a href=\"$line[feat_url]\" class=\"maintext\" target=\"_blank\"><img src=\"/images/$line[feat_thumb]\" class=\"thumbnail\" align=\"middle\"><br> <small>$line[feat_name]</small></a>";
        echo "</td>";
    }
    
    
    ?>
    PHP:
    I'm assuming I would have to add another field to the MySQL db called "weight" or something. But I'm stuck at that.

    Thank you!
     
    Doug the Great, Dec 2, 2007 IP
  2. *louie*

    *louie* Peon

    Messages:
    48
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    yes you can add an extra field in the db or find a keyword that you want to put more weight on it and use it to filter the results "like..."
     
    *louie*, Dec 2, 2007 IP
  3. Doug the Great

    Doug the Great Peon

    Messages:
    548
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm sorry? You will have to be more descriptive, i'm a noob when it comes to PHP. :D

    Say i'm doing a 1-10 weight system, 10 getting the most views, 1 getting the least.

    How would I do that?
     
    Doug the Great, Dec 2, 2007 IP
  4. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #4
    $weight[10] = 10;
    $weight[9] = 19;
    $weight[8] = 27;
    $weight[7] = 34;
    $weight[6] = 40;
    $weight[5] = 45;
    $weight[4] = 49;
    $weight[3] = 52;
    $weight[2] = 54;
    $weight[1] = 55;
    
    $random = rand(1, 55);
    
    switch ($random) {
    case <= $weight[10]:
        echo "SELECT * FROM table WHERE weight = 10;
        break;
    case <= $weight[9]:
        echo "SELECT * FROM table WHERE weight = 9;
        break;
    case <= $weight[8]:
        echo "SELECT * FROM table WHERE weight = 8;
        break;
    case <= $weight[7]:
        echo "SELECT * FROM table WHERE weight = 7;
        break;
    case <= $weight[6]:
        echo "SELECT * FROM table WHERE weight = 6;
        break;
    case <= $weight[5]:
        echo "SELECT * FROM table WHERE weight = 5;
        break;
    case <= $weight[4]:
        echo "SELECT * FROM table WHERE weight = 4;
        break;
    case <= $weight[3]:
        echo "SELECT * FROM table WHERE weight = 3;
        break;
    case <= $weight[2]:
        echo "SELECT * FROM table WHERE weight = 2;
        break;
    case <= $weight[1]:
        echo "SELECT * FROM table WHERE weight = 1;
        break;
    }
    Code (markup):
    As usual that was off the top of my head so could be wrong.


    Edit: Actually this wouldn't work unless each weighting had the same number of rows, but at least you can work from here. Maybe.
     
    live-cms_com, Dec 2, 2007 IP
  5. Doug the Great

    Doug the Great Peon

    Messages:
    548
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    There has to be an easier way.

    Someone on another board said this but I don't understand what he meant.

    
    ALTER TABLE featured ADD COLUMN weight int unsigned DEFAULT 0;
    
    Code (markup):
    Give that column some values of 0 - 9 (or whatever)

    Then, in your query, do something like
    
    SELECT RAND()*Weight AS Weighted
      FROM blah
    ORDER BY Weighted
    
    Code (markup):







    EDIT: got it!
     $q=mysql_query("SELECT Name AS feat_name, URL AS feat_url, Thumbnail AS feat_thumb, RAND() * Weight AS Weighted FROM `featured` ORDER BY Weighted DESC LIMIT 8");
    while ($line=mysql_fetch_assoc($q)) {  
    PHP:
     
    Doug the Great, Dec 2, 2007 IP