Gradient (based on the number of records)

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alok.jethanandani
Forum Newbie
Posts: 1
Joined: Sun Jun 08, 2008 7:53 pm

Gradient (based on the number of records)

Post by alok.jethanandani »

Please visit this url to get a preview of what i'm trying to accomplish:
http://classes.design.ucla.edu/Spring08 ... /index.php

To make that gradient dynamic this is the code im working on:

Code: Select all

 
                $sql = "SELECT * FROM playlist ORDER BY DT DESC";
        $result = mysql_query($sql);
        
        $num_rows = mysql_num_rows($result);
        
        [b]for ( $i = $num_rows; $i < $num_rows + 1 && $i > 0; $i++) {
            $rgb = 255-(230/$i);
        }   [/b]
 
Does that For Loop right?

Code: Select all

 
 
<div id="media">
        <ul>
            <?php 
            $sql = "SELECT * FROM playlist ORDER BY DT DESC";
            $result = mysql_query($sql);
            
            while ( $row = mysql_fetch_assoc($result) ) {
                echo "<li><a href='#'><font style='color:rgb(255,255,255);'>{$row['title']}</font></a></li>";
            }
            
            ?>
            
         </ul>
    </div>
    
</div>  
 
I need that For Loop to count down in the rgb space. How can this be done?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Gradient (based on the number of records)

Post by onion2k »

Assuming you want to start at X and go down to Y, divide the difference by the number of records and then subtract that amount from the RGB values at each step.

eg

Code: Select all

$sql = "SELECT * FROM playlist ORDER BY DT DESC";
$result = mysql_query($sql);
 
$start = 255;
$end = 128;
$records = mysql_num_rows($result);
$step = round(($end-$start)/$records);
 
$color = $start;
 
while ( $row = mysql_fetch_assoc($result) ) {
  echo "<li><a href='#'><font style='color:rgb(".$color.",".$color.",".$color.");'>{$row['title']}</font></a></li>";
  $color -= $step;
}
 
Not tested that code..
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Gradient (based on the number of records)

Post by Benjamin »

Brilliant!
Post Reply