I have a page that displays random adds. The ads have a weight to them based on how big the ad is (6 = full page, 1 = 1/6 page). What I am wanting to do is only display as many ads as it takes to make a total weight of 6. For example if I have 6 ads that weigh 1 then it will show all 6. If I have ads that weigh 2,3 and 1 then it will only show those 3 adds because that is a total weight of 6. Here is what I have so far:
<?
include("inc/db.inc.php");
mysql_connect($db_localhost,$db_username,$db_password);
@mysql_select_db($db_database) or die( "Unable to select database");
$sql = "SELECT SUM(rank) FROM midcolumn WHERE status = 'Active'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$row[0];
$sql = "SELECT * FROM midcolumn WHERE status LIKE 'Active' AND ".$row[0]." < 6 ORDER BY RAND()";
$result=mysql_query($sql);
$num = mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$adimg=mysql_result($result,$i,"adimg");
$link=mysql_result($result,$i,"link");
$target=mysql_result($result,$i,"target");
echo " <a href='$link' target='$target'><img src='test_adds/$adimg' border='0'></a>";
echo " <hr width='95%'>";
++$i;
}
?>
This is obviously not working because it is giving me the SUM of all rows marked active and will only show them if that total < 6. I think you might be able to see where I was going with the code though. Anyone have any ideas on how to acomplish this?