kexxcream
kexxcream

Reputation: 5933

Different CSS style on last SQL row using PHP

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?

Upvotes: 0

Views: 589

Answers (2)

Bhavesh G
Bhavesh G

Reputation: 3028

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"})

Upvotes: 0

galymzhan
galymzhan

Reputation: 5523

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
  }
}

Upvotes: 3

Related Questions