Question : php apply different class to last result...

is there any way of applying a different result to the last result in this code?

    <?php
$result = mysql_query("SELECT * FROM news WHERE published='yes' AND section='competition' ORDER BY date DESC LIMIT 4");
while($row = mysql_fetch_array($result))
  {$myDate = $row['date'];$day = date("d", strtotime($myDate));$month = date("m", strtotime($myDate));$year = date("Y", strtotime($myDate));?>
  <div class="item-220 item-short">
    <div class="item-img">
      <div class="item-220-hover"></div>
      <a href="/skiing-snowboarding-news/<?php echo "". $row['filename'] ."_". $row['id'] .""; ?>.html"><img src="/graphics/img/skiing-snowboarding-news/<?php echo "". $row['image'] .""; ?>" alt="<?php echo "". $row['image_alt'] .""; ?>" width="218" height="218" border="0" /></a></div>
    <div class="item-details">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td class="date"><img src="/graphics/img/date/day/<?php echo "$day"; ?>.gif" width="35" height="25" alt="<?php echo "$day"; ?>" /><img src="/graphics/img/date/month/<?php echo "$month"; ?>.gif" width="35" height="10" alt="<?php echo "$month"; ?>" /><img src="/graphics/img/date/year/<?php echo "$year"; ?>.gif" width="35" height="10" alt="<?php echo "$year"; ?>" /></td>
          <td class="title"><a href="/skiing-snowboarding-news/<?php echo "". $row['filename'] ."_". $row['id'] .""; ?>.html"><?php echo "". $row['title'] .""; ?></a></td>
        </tr>
        <tr>
          <td colspan="2" class="description"><?php echo "". $row['description'] .""; ?></td>
        </tr>
      </table>
    </div>
  </div>
  <?php }?>

Answer : php apply different class to last result...

You have 7 different classes within each iteration. Not sure which one you want to change.

Inside your loop, where you echo all the DIVs, you would need to check to see if you're in the last record. If you are, then echo a different class.

In the code below, the DIV will have a class of "NormalClass" unless it's the last record - then it will have a class of "LastRecordClass"

FYI:  <?=Something?> is the same as <?php echo Something;?>.



1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
<?php
$NumOfRecords = mysql_num_rows($result);
$counter = 1;

while ($row = mysql_return_array($result)):
?>

<div class="<?=($counter==$NumOfRecords) ? "LastRecordClass" : "NormalClass"?>">
   ...SomeContent...
</div>

<?php
$counter++;
endwhile;
?>
Random Solutions  
 
programming4us programming4us