I am a self taught coder so I may be overlooking some very simple answer I didn’t pick up in class, but…
I have a DB full of different businesses, where their Unique IDcorresponds with the name of their picture on my server. Every time a page on my website loads I want php to grab the business (and subsequent ad) with the fewest views. In case it is pertinent I also have two different sizes for ads. I will try to illustrate.
Table:
Business Name | Unique ID | Views | Size
--------------+-----------+-------+------
Business 1 | 1 | 50 | Small
Business 2 | 2 | 9 | Small
Business 3 | 3 | 67 | Large
Business 4 | 4 | 9 | Large
so I use this script to retrieve the business with the lowest views
SELECT * FROM ads WHERE size='Large' ORDER BY views ASC
Now to my question – this works until all views get to “9” then it seems like it just picks two ads (one of each size) to always display. So in the example you would expect Business 4 to be selected, but for some reason business 3 is always the one chosen.
Also, when I investigate in PhpMyAdmin and I click on the column name views to sort by views and the two businesses are always together at the top or bottom (ASC and DESC respectively) never mixing in with the list. Right now when I sort ASC I get:
Views
50
67
9
9
9
9
The 50 and 67 are obviously not in correct order and they are the two that are always shown on the webpage. It seems like after “9” these two are assigned some sort of priority, I can’t figure out.
I should also mention the way the table is handled when called by the specific web page:
$result = mysql_query("SELECT * FROM ads WHERE size='Large' ORDER BY views ASC") or die(mysql_error());
$productCount = mysql_num_rows($result); // count the output amount
if ($productCount > 0) {
if($row = mysql_fetch_array($result)){
$id = $row["id"];
$url = $row["url"];
$views = $row["views"];
$businessName = $row["businessName"];
}
}
$views = $views + 1;
$sql = mysql_query("UPDATE ads SET views='$views' WHERE id='$id'");
This is in the php code of every page on my website.
The ads are displayed with this html code:
<div id="ad1">
<a href="<?php echo $url; ?>" target="_blank"> <img src="inventory_images/<?php echo $id; ?>.jpg" alt="<?php echo $businessName; ?>" height="144" width="216"/></a>
</div>
Hopefully that is enough information, your help is greatly appreciated!
Make sure your “views” column is set to type “int” and not “char” or “varchar”, or it won’t sort as you expect.