Jump to content

need some help with my PHP :(


OrganizedFellow
 Share

Recommended Posts

Can I get some help with my terrible PHP logic?

I am attempting to generate alternating CSS classes, even and odd. So I wrote this little function:

function doAlt($n) {
	if ($n % 2) {
		echo('odd');
	}else{
		echo('even');
	}
}

I figured how to output like this:

for ($i = 0; $i <= 10; $i++)  {
	echo doAlt($i).$i."<br>";
}

Resulting HTML is this:

even0
odd1
even2
odd3
even4
odd5
even6
odd7
even8
odd9
even10

So far, so good!

Here is where I inserted it into my code:

<section>
	<ul>
	<?php
		$blogpages = $pages->get("/blog/");
		$children = $blogpages->children;
		$children->prepend($blogpages);
		foreach($children as $child) {
			// my function to print even or odd for alternating CSS classes
			for($i=0;$i<=10;$i++){
				echo "<li>";
				echo "<h3>{$child->title}</h3> \n";
				echo doAlt($i).$i;
				echo "{$child->snippet} \n";
				echo "</li> \n";
			}
		}
	?>
	</ul>
</section>

The resulting HTML is each blog entry repeating multiple times, in this case $i=<10, so ten times.

I did it like that to see how it would print/echo out before I stuck it in my CSS.

How can I give each entry a different CSS class?

Link to comment
Share on other sites

You can use a slightly different form of foreach to get also the index on each iteration, like this:

foreach($children as $i => $child) {
    echo doAlt($i) . $i;
}

That should do the trick. Without the inner foreach of course.

  • Like 1
Link to comment
Share on other sites

You can use a slightly different form of foreach to get also the index on each iteration, like this ... 

That should do the trick. Without the inner foreach of course.

SUPERB!

My PHP is still at a beginner level. Thank you!

Regarding the function. I read it as:

if $n is divisible by 2

        echo('odd');
    }else (if not){
        echo('even');
 
But I expect if $n is indeed divisible by 2, then the first each should read even, not odd. Or am I reading it wrong?
Link to comment
Share on other sites

$count = 1;
foreach($children as $child) 
{
    $type = $count % 2 == 0 ? "even" : "odd";
    echo $type;
    $count++;
} 

No need for a function :)

Or pure css:

section ul li:nth-child(even) {
   /* Do stuff on even list items */
}

Written in the browser, but you might get the idea. 

  • Like 2
Link to comment
Share on other sites

Regarding the function. I read it as:

if $n is divisible by 2

        echo('odd');
    }else (if not){
        echo('even');
 
But I expect if $n is indeed divisible by 2, then the first each should read even, not odd. Or am I reading it wrong?

You are reading it wrong :).

'%' is a modulus operator giving you the remainder of the division of its operands. For example 10 % 3 = 1 (10 = 3 * 3 + 1). So your first if actually means "if $n is NOT divisible by 2".

  • Like 1
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...