Solved

php apply different class to last result...

Posted on 2010-08-25
4
403 Views
Last Modified: 2013-12-13
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 }?>
0
Comment
Question by:maccaj51
  • 2
  • 2
4 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 33520470
You could introduce an incremental counter in the loop and check it's value against the number of records returned by your query.



$NumOfRecords = mysql_num_rows($result);
$counter = 1;

while ($row = mysql_return_array($result))
{
   if ($counter == $NumOfRecords) {
      echo "This is the last record";
   }

   $counter++;
}

Open in new window

0
 

Author Comment

by:maccaj51
ID: 33520486
Thanks for your reply... dont quite understand it... i need to assign a different class to the last result?? would that do this?
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 33520606
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;?>.




<?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;
?>

Open in new window

0
 

Author Closing Comment

by:maccaj51
ID: 33520649
Helped alot!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question