Solved

php apply different class to last result...

Posted on 2010-08-25
4
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitting a sting into elements 6 76
How do i use the await event in php echo 5 44
Dump data from mysql to xls php 10 56
syntax error, unexpected end of file 4 19
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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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…

752 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