TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Adding a [CSS] class every 3rd result (http://www.talkphp.com/absolute-beginners/5653-adding-css-class-every-3rd-result.html)

rrfive 12-04-2010 02:42 AM

Adding a [CSS] class every 3rd result
 
Hello all, new to PHP so please, bare with me.

I'm trying to add a class to every third result of a mySQL query. I've tried to figure it out but have had no luck.

PHP Code:

<?php
                $result 
mysql_query("SELECT * FROM table WHERE publish='y' ORDER BY id DESC");
                while(
$row mysql_fetch_array($result))            
                {
                echo 
"<div class=\"newsBlock ADDCLASSHERE\"><a href=\"article.php?article=" $row['url'] . " \"><img src=\"images//" $row['image'] . "\" width=\"198\" height=\"106\"></a><h2>" $row['title'] . "</h2><div class=\"date\">" $row['date'] . "</div><p>" $row['teaser_copy'] . "</p><a href=\"article.php?article=" $row['url'] . "\" class=\"link\">" $row['teaser_link'] . " <span>&raquo;</span></a></div>";                mysql_close($con);
                }
            
?>

I've added 'ADDCLASSHERE' where I'd like to add the additional class. If it's not the third result no class is required.

Any help or guidance would be greatly appreciated.

Thanks in advance.

maeltar 12-04-2010 09:09 PM

Just count as you go and use an "if" to do something every 3rd, so am using the modulo operator to find out if it's the the 3rd, or is divisiable by 3 to echo the line with the class, or if it's false it echo's the line without the ADDCLASSHERE

PHP Code:

<?php
                $result 
mysql_query("SELECT * FROM table WHERE publish='y' ORDER BY id DESC");
        
$counter 1;
                while(
$row mysql_fetch_array($result)) {

        if ( 
$counter ){
                    echo 
"<div class=\"newsBlock ADDCLASSHERE\"><a href=\"article.php?article=" $row['url'] . " \"><img src=\"http://www.talkphp.com/images//" $row['image'] . "\" width=\"198\" height=\"106\"></a><h2>" $row['title'] . "</h2><div class=\"date\">" $row['date'] . "</div><p>" $row['teaser_copy'] . "</p><a href=\"article.php?article=" $row['url'] . "\" class=\"link\">" $row['teaser_link'] . " <span>&raquo;</span></a></div>";
        } else {
            echo 
"<div class=\"newsBlock\"><a href=\"article.php?article=" $row['url'] . " \"><img src=\"http://www.talkphp.com/images//" $row['image'] . "\" width=\"198\" height=\"106\"></a><h2>" $row['title'] . "</h2><div class=\"date\">" $row['date'] . "</div><p>" $row['teaser_copy'] . "</p><a href=\"article.php?article=" $row['url'] . "\" class=\"link\">" $row['teaser_link'] . " <span>&raquo;</span></a></div>";
        }

        
$counter $counter 1;
        
// does $counter++ work ?
                
}
mysql_close($con);
?>

I think that should do what you want, someone will probably come along and show a simpler way

wGEric 12-06-2010 09:37 PM

Here is a way that makes it so that you don't have to have two lines of the same HTML. Makes it easier to maintain. maeltar's code is correct though and will work although it looks like the conditions might be backwards.

PHP Code:

<?php
    $result 
mysql_query("SELECT * FROM table WHERE publish='y' ORDER BY id DESC");
    
    
$counter 0;
    while(
$row mysql_fetch_array($result))            
    {
        
$class = ($counter == 0) ? 'ADDCLASSHERE' '';
        echo 
"<div class=\"newsBlock {$class}\"><a href=\"article.php?article=" $row['url'] . " \"><img src=\"images//" $row['image'] . "\" width=\"198\" height=\"106\"></a><h2>" $row['title'] . "</h2><div class=\"date\">" $row['date'] . "</div><p>" $row['teaser_copy'] . "</p><a href=\"article.php?article=" $row['url'] . "\" class=\"link\">" $row['teaser_link'] . " <span>&raquo;</span></a></div>";

        
$counter += 1;
        
// These all do about the same thing
        // $counter = $counter + 1;
        
        // make sure you know the difference between these two before using them
        // you might have unexpected results because the majority of
        // the time people want ++$var instead of $var++ but the use $var++
        // $counter++;
        // ++$counter;
    
}
?>



All times are GMT. The time now is 06:37 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0