Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php apply different class to last result...

Posted on 2010-08-25
4
Medium Priority
?
408 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 44

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 44

Accepted Solution

by:
Chris Stanyon earned 1500 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

Independent Software Vendors: 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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