Specific class for loop - first and last

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rorysmyth
Forum Newbie
Posts: 5
Joined: Tue Jan 13, 2009 4:33 pm

Specific class for loop - first and last

Post by rorysmyth »

Hi there.

I'm new to this forum aswell as php and I'm having some trouble, and would appreciated some help.

I have this simple query

Code: Select all

 
mysql_select_db($database_conn1, $conn1);
$query_portfolio = "SELECT * FROM portfolio ORDER BY dateadded DESC";
$portfolio = mysql_query($query_portfolio, $conn1) or die(mysql_error());
$row_portfolio = mysql_fetch_assoc($portfolio);
$totalRows_portfolio = mysql_num_rows($portfolio);
 
and the following xhtml

Code: Select all

 
        <?php do { ?>
        <div class="grid_3">
        <div>
        <img src="<?php echo $thumbnail->Execute(); ?>" alt="<?php echo $row_portfolio['name']; ?>" class="pimage" />
        </div>
        <div>
        <h2 class="mtp10"><?php echo $row_portfolio['name']; ?></h2>
        <?php echo $row_portfolio['desc']; ?>
        </div>
        </div>
        <?php } while ($row_portfolio = mysql_fetch_assoc($portfolio)); ?>
        </div>
 
The css and php work fine. The problem is that the grid I'm working on is very tight and in doing this loop, I'm loosing control over my css.
There is a row with 3 items on it. Doing it manually, I apply a class to the first and second items to fit the grid......or a class to the middle item. The classes affect the margins. This means that everything fits nice and tightly. I do this for each row.

With the php, I'm looking for a way to single out either the 1st and 3rd items on each row so I can apply a different css class to them.

Is this possible?

Theoretical answers probably won't work as I'm a designer not a developer..ergo, it would be lost on me. ;)

Thanks in advance.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Specific class for loop - first and last

Post by josh »

You need to increment a variable each time you iterate thru your result set. Use the modulus operator to act conditionally on a value based on it's denominators.

eg.

Code: Select all

 
for( $i = 0; $i <= 100; $i++ )
{
     if( $i % 14 ) echo $i . ' is divisible by 14';
}
 
rorysmyth
Forum Newbie
Posts: 5
Joined: Tue Jan 13, 2009 4:33 pm

Re: Specific class for loop - first and last

Post by rorysmyth »

appreciate your help here.

How would I go about integrating that into my code?

Thanks again.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Specific class for loop - first and last

Post by josh »

Actually if you want first and last you'd output the class tag only if $i == 0 || $i == $total ( $total being the total # of rows )
rorysmyth
Forum Newbie
Posts: 5
Joined: Tue Jan 13, 2009 4:33 pm

Re: Specific class for loop - first and last

Post by rorysmyth »

Care to explain how I would integrate this into the existing code jshpro2?
rorysmyth
Forum Newbie
Posts: 5
Joined: Tue Jan 13, 2009 4:33 pm

Re: Specific class for loop - first and last

Post by rorysmyth »

It's not just the first and last either. It's the first and last in each row. Maybe looking at it in a row is a bad idea. the CSS handles the illusion of a row. it would go like this

class1 class class2 class1 class class2

Currently googling modulus operators, but all I'm seeing is it being used with tables - alternating row colours etc.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Specific class for loop - first and last

Post by josh »

I kinda already did, if you want me to write it for you I'd have to charge. Maybe someone else trying to practice has the spare moments to do it for you. Best of luck.
rorysmyth
Forum Newbie
Posts: 5
Joined: Tue Jan 13, 2009 4:33 pm

Re: Specific class for loop - first and last

Post by rorysmyth »

jshpro2 wrote:I kinda already did, if you want me to write it for you I'd have to charge. Maybe someone else trying to practice has the spare moments to do it for you. Best of luck.
Theoretical answers probably won't work as I'm a designer not a developer..ergo, it would be lost on me.
..

You need to increment a variable each time you iterate thru your result set. Use the modulus operator to act conditionally on a value based on it's denominators.

Perhaps I'll charge you for some introductory courses to English.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Specific class for loop - first and last

Post by josh »

rorysmyth wrote:Perhaps I'll charge you for some introductory courses to English.
Maybe you should take a course in life
Post Reply