tags:

views:

65

answers:

5

Hello,

The table below prints of the results of a query called $sqlStr3. It works fine. The query is set to return the top 25 results.

I would like to apply a unique CSS class (called "class1" for purposes of this question) to the top ten results in the query. How can I do this?

Thanks in advance,

John

$result = mysql_query($sqlStr3);
$count = 1;  
$arr = array();
echo "<table class=\"samplesrec1edit\">";
    while ($row = mysql_fetch_array($result)) { 
        echo '<tr>';
        echo '<td class="sitename1edit2a">'.$count++.'.</td>';
        echo '<td class="sitename1edit1"><a href="http://www...com/../members/index.php?profile='.$row["username"].'"&gt;'.stripslashes($row["username"]).'&lt;/a&gt;&lt;/td&gt;';
        echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
        echo '</tr>';
        }
    echo "</table>";
+1  A: 
... class="...<?php if ($count <= 10) { ?> class1<?php } ?>" ...

And move the increment to the end of the loop.

Ignacio Vazquez-Abrams
Thanks... where in the code should I put this?
John
Where it assigns the `class` attribute for the tag.
Ignacio Vazquez-Abrams
Okay... for the table CSS or the column CSS?
John
Wherever you would put it if you were just writing straight HTML.
Ignacio Vazquez-Abrams
Okay... but where do I put the non-top-10 classes?
John
look at my answer
ITroubs
A: 

How about assigning each result from TOP10 a unique class called top_result_[n]?

while ($row = mysql_fetch_array($result)) { 
        echo '<tr'.($count++ <=10 ? ' class="top_result_'.$count.'"' : '').'>';
        echo '<td class="sitename1edit2a">'.$count.'.</td>';
        echo '<td class="sitename1edit1"><a href="http://www...com/../members/index.php?profile='.$row["username"].'"&gt;'.stripslashes($row["username"]).'&lt;/a&gt;&lt;/td&gt;';
        echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
        echo '</tr>';
        }
    echo "</table>";

I'm not sure wether you want to apply ONE class for all 10 results or UNIQUE class for each from 10 results, as for the first case, the code would be:

while ($row = mysql_fetch_array($result)) { 
        echo '<tr'.($count++ <=10 ? ' class="top_result"' : '').'>';
        echo '<td class="sitename1edit2a">'.$count.'.</td>';
        echo '<td class="sitename1edit1"><a href="http://www...com/../members/index.php?profile='.$row["username"].'"&gt;'.stripslashes($row["username"]).'&lt;/a&gt;&lt;/td&gt;';
        echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
        echo '</tr>';
        }
    echo "</table>";
cypher
+1  A: 

use this:

$result = mysql_query($sqlStr3);
$count = 1;  
$arr = array();
echo "<table class=\"samplesrec1edit\">";
    while ($row = mysql_fetch_array($result)) { 
        if($count < 11){
            echo '<tr class="top-10">';
        }else{
            echo '<tr class="non-top-10">';
        }
            echo '<td class="sitename1edit2a">'.$count++.'.</td>';
        echo '<td class="sitename1edit1"><a href="http://www...com/../members/index.php?profile='.$row["username"].'"&gt;'.stripslashes($row["username"]).'&lt;/a&gt;&lt;/td&gt;';
        echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
        echo '</tr>';
        }
echo "</table>";

access the td of top10 using this css

.top-10 td{
//definitions
}

and access the td of non top10 lines with

.non-top-10 td{
//definitions
}
ITroubs
+1  A: 

you can do it in the server side via php like:

 echo '<tr '.($count <=10 ? 'myclass' : '').' > ;

or in jquery in front side like:

$("someTableSelector").find("tr:lt(10)").addClass('myclass')
Haim Evgi
A: 
Josh Smeaton
One can only dream... :(
Darren Westall
I'm in a very rare but fortunate situation where I can guarantee that users of my application all use html5 webkit browsers. I only belatedly put the new browser caveat into my answer as it slips my mind regularly.
Josh Smeaton