General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 11th, 2003, 04:47 AM
markrenn markrenn is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 23 markrenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Arrow showing random images with priorities

i have this code displaying random images from a database, they work just well.

Now i need to give priorities(HI , MED, LOW) to the images, where the HI-priority image will be displayed more often than MED-priority image, and the LOW-priority images will be the least to be displayed.

any ideas how i can implement this?

PHP Code:
if(!isset($action)){
 
$query "select * from $ads_image where status = 1";
 
$result doquery($query);
 
$num mysql_num_rows($result);
 if(
$num 1){
  
$num $num 1;
  
$val rand(0$num);
  
mysql_data_seek($result$val);
 }
 
$row mysql_fetch_array($result);
 
$id $row['id'];
 
$adtype $row['adtype'];
 
$image $row['image'];
 
$alt $row['alt'];
 
$link $row['link'];
 
$adtext $row['adtext'];
 
$today date("d m Y");
 
$query "select * from $stats where DATE_FORMAT(amdate, '%d %m %Y') = '$today' and id = $id";
 
$result doquery($query);
 
$num_results mysql_num_rows($result);
 
$today date("Y-m-d");
if(
$num_results == 0){
  
$query "INSERT INTO $stats (id, amdate, displays, clicks) VALUES ($id, '$today', 1, 0)";
 }else{
  
$query "UPDATE $stats SET displays = displays + 1 WHERE id = $id AND amdate = '$today'";
 }
  
$result doquery($query);
 if(
$adtype == 1){
  echo 
"<a href='adman.php?id=".$id."&action=show' target='_blank'><img border='0' src='$image' alt='$alt'></a>";
 }else{
  echo 
$adtext;
 }



thanks in advance

Reply With Quote
  #2  
Old September 11th, 2003, 06:30 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
I would suggest using a weighted random selection. For example doing rand % 6 where -

0 = HI
1 = HI
2 = HI
3 = MED
4 = MED
5 = LO

Then add an extra clause to your SQL select statement where priority=HI/MED/LO (or rather the numerical equivalent of HI/MED/LO) based on the outcome of the random selection.

Hope that helps,

-KM-

Reply With Quote
  #3  
Old September 11th, 2003, 09:49 PM
markrenn markrenn is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 23 markrenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post

Thanks kode_monkey for your reply!
what do you mean by rand % 6
and can you please explain further?

Reply With Quote
  #4  
Old September 12th, 2003, 07:23 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Markrenn, I believe kode_monkey was suggesting that you give your images a priority code corresponding to those listed and then add a where clause correlating that priority code to a randomly chosen number. I see a problem with that, though, in that if you use numeric indices in your query, you then have to make sure that you insert your values in the correct proportions. That is, if you use

0 = HI
1 = HI
2 = HI
3 = MED
4 = MED
5 = LO

as your key/value map and stick the numeric indices in the database rather than the string "HI," "MED," or "LO," you have to keep up with the frequency with which you insert the different index numbers in order to ensure that your weighting is intact. I think I'd consider inserting the text strings into the database and then having code that creates an array in your PHP code correlating numbers to the text strings, selecting a random array element, and building your query using the value for that array key. So for example, assuming you've inserted "HI," "MED," and "LO" as the weight values in your database:

PHP Code:
 priority=array(
    
"HI",
    
"HI",
    
"HI",
    
"MED",
    
"MED",
    
"LO"
);

$pri=rand(0,sizeof($priority)-1);
$query="Select * FROM $ads_image WHERE status=1 AND priority=" $priority[$pri]; 


This also makes it a snap to change the weights. If all of a sudden you need to make the HI images appear more frequently, you just add a "HI" element to your array and it's all taken care of. If you insert numeric keys into the database, then you'd have to do some tricky updating to guarantee that the number of zeroes, ones, and twos were evened out so that the weighting among the HIs was equal.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > showing random images with priorities


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT