Page 1 of 1

Specific class for loop - first and last

Posted: Tue Jan 13, 2009 4:44 pm
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.

Re: Specific class for loop - first and last

Posted: Tue Jan 13, 2009 5:16 pm
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';
}
 

Re: Specific class for loop - first and last

Posted: Tue Jan 13, 2009 5:19 pm
by rorysmyth
appreciate your help here.

How would I go about integrating that into my code?

Thanks again.

Re: Specific class for loop - first and last

Posted: Tue Jan 13, 2009 6:18 pm
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 )

Re: Specific class for loop - first and last

Posted: Tue Jan 13, 2009 6:39 pm
by rorysmyth
Care to explain how I would integrate this into the existing code jshpro2?

Re: Specific class for loop - first and last

Posted: Tue Jan 13, 2009 7:00 pm
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.

Re: Specific class for loop - first and last

Posted: Tue Jan 13, 2009 7:46 pm
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.

Re: Specific class for loop - first and last

Posted: Tue Jan 13, 2009 7:57 pm
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.

Re: Specific class for loop - first and last

Posted: Tue Jan 13, 2009 9:16 pm
by josh
rorysmyth wrote:Perhaps I'll charge you for some introductory courses to English.
Maybe you should take a course in life