Solved

php apply different class to last result...

Posted on 2010-08-25
4
399 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 42

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 42

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now