0

Problem:

To style the last row in a SQL query without using any CSS3 selectors.

PHP code:

while ($item = mysql_fetch_assoc($itemsresult))
{
    $answer = "SELECT IID, Comment FROM betyg_answers WHERE EID = '{$EID}' AND CID = '{$item['CID']}' ORDER BY ID ASC";
    $answerresult = mysql_query($answer) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
    $answer = mysql_fetch_assoc($answerresult);

    if ($answer['IID'] == $item['IID'])
    {
        $html .= '
            <tr>
                <td class="arrow" style="'.$arrow.'"><img src="./img/circle_arrow_right.png" class="arrowimage"></td>
                <td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
                <td class="text" style="'.$text.'">'.$item['Description'].'</td>
            </tr>
        ';
    }
    else
    {
        $html .= '
            <tr>
                <td class="arrow" style="'.$arrow.'">&nbsp;</td>
                <td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
                <td class="text" style="'.$text.'">'.$item['Description'].'</td>
            </tr>
        ';
    }

    $itemcounter++;
}

Last row in SQL query should instead print:

$html .= '
        <tr>
            <td class="arrow" style="'.$lastarrow.'">&nbsp;</td>
            <td class="numbers" style="'.$lastnumbers.'">'.$itemcounter.'.</td>
            <td class="text" style="'.$lasttext.'">'.$item['Description'].'</td>
        </tr>
';

Question:

What needs to be added in the while loop so it recognizes the last row and print a different code instead?

1
  • You might want to consider re-writing your SQL to retrieve the answers and items in one query. Also, what if the last row would have matched $answer['IID'] == $item['IID']? Commented Jun 17, 2012 at 5:43

2 Answers 2

3

Use a counter:

$i = 0;
$c = mysql_num_rows($itemresult);
while ($item = mysql_fetch_assoc($itemsresult)) {
  $i++;
  if ($i == $c) {
    // This is a last row
  } else {
    // Regular row
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

there is several ways to do that :

  1. use :last-child ( but this isn't working in IE8)

    table tr:last-child { background-color:#ff0000; }​

  2. using jQuery method ( this is browser independent)

in document load function, add following jQuery code,

$("td:last-child").css({background-color:"#ff0000"})

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.