1

MySQL

$selectSize     = "SELECT * FROM products";
$querySize      = $db->select($selectSize);
while ($product = $db->fetcharray($querySize)) {

HTML

<ul>
<li>Product A</li>
<li>Product B</li>
<li class='right'>Product C</li>
<li>Product D</li>
<li>Product E</li>
<li class='right'>Product F</li>
</ul>

Question

While getting the product, I want the Product C and Product F or any product after 3 loops will have class='right' to the list style. Let me know

Thanks

3 Answers 3

3

Use something like the following (your code snippet was a little short)

$index = 1;
while ($product = $db->fetcharray($querySize)) {
    if ($index % 3 == 0) {
        //add your class here
    }
    $index++;
    //...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jonathan Fingland. You rock!
3

.

$count = 0;
while ($product = $db->fetcharray($querySize)) {
    echo "<li" . ((++$count % 3) ? "" : " class=\"right\"") . ">" 
        . $product['name']
        . "</li>\n";
}

Comments

0
foreach($product as $key=>value){
echo "<li" . (!(!is_nan($key) && $key % 3) ? " class=\"right\"" : "") . ">"
. $product['name']
. "</li>\n"; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.