TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-04-2010, 02:42 AM   #1 (permalink)
The Visitor
Newcomer 
 
Join Date: Dec 2010
Posts: 1
Thanks: 1
rrfive is on a distinguished road
Default 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.
rrfive is offline  
Reply With Quote
Old 12-04-2010, 09:09 PM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

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
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
The Following User Says Thank You to maeltar For This Useful Post:
rrfive (12-05-2010)
Old 12-06-2010, 09:37 PM   #3 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

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;
    
}
?>
__________________
Eric
wGEric is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 08:09 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 04:55 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design