Liam
Liam

Reputation: 9855

Individual classes on PHP rows

I have the following thats within a loop...

 echo "<span class='srch-val'>".apply_filters(" $value\n", $value)."</span>";

I want to somehow add a number or something after the class 'srch-val-1' without using jQuery, is this possible using PHP? If so where can I find out how to do this? thanks

Its currently within a foreach() loop...

 foreach ( (array) $keys as $key ) { //Stuff }

Upvotes: 0

Views: 73

Answers (2)

Obmerk Kronen
Obmerk Kronen

Reputation: 15949

echo '<span class="srch-val-'.$your_value.'">'.apply_filters(" $value\n", $value)."</span>";

$your_value can be everything you want , including a number..

A very simple loop example

for ($i = 1; $i <= 10; $i++) {
 echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>";
}

will return

<span class="srch-val-1">
<span class="srch-val-2">
<span class="srch-val-3">
.... 10

or for example - in a wordpres loop , you can even do

echo '<span class="srch-val-'.$post->ID.'">'.apply_filters(" $value\n", $value)."</span>";

or

echo '<span class="srch-val-'.the_title().'">'.apply_filters(" $value\n", $value)."</span>";

and so on ... any available value can be applied . of course you then have to think how to TARGET an over changing class with CSS - but that is for another question,.

EDIT I - as for comment for the loop inside loop - the above was just a generic example - you need to put the counter in the beginning of your original loop and increment it on the end . (if it is a wordpress loop like i suspect, put $i=1 , or any other number , after the if (have_posts()) or after the while (have_posts()) -depending on where you need it - and the incrementing $i=$i++ at the end. (before the endif; or endwhile; - again depending on your needs...)

Upvotes: 1

KernelM
KernelM

Reputation: 8916

Why not?

 echo "<span class='srch-val".$something."'>".apply_filters(" $value\n", $value)."</span>";

Where $something is a counter, custom text, whatever.

Upvotes: 0

Related Questions