Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 12th, 2008, 04:40 AM
Authorized User
 
Join Date: Jun 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to have Sequential numbers in a recordset?

Hi,

I am retrieving a recordset from a simple database table of two fields.

Within the recordset I want to display a sequential number within the html for each row in the recordset, for example
Image1
Image2
Image3
etc.

This is because each row contains javascript mouseover images and they need a unique number.

To make things clearer I have listed my code below and made bold where I want the sequential number to appear....

Code:
<?php do { ?>
      <tr>
          <td width="125" class="bodycopy"><?php echo "<a href=products_subcategory.php?subcatid=".$row_result['subcatid'].">".$row_result['subcatname']."</a>"; ?></td>
          <td width="35" valign="top" align="left"><a href="#" target="_blank" onMouseOver="MM_swapImage('Image1','','/images1/arrow1.jpg',1)" onMouseOut="MM_swapImgRestore()"><img src="/images1/arrow.gif" name="Image1" width="15" height="15" border="0"></a></td>
          <td width="374">&nbsp;</td>
      </tr>
      <tr>
          <td colspan="3">&nbsp;</td>
      </tr>
<?php } while ($row_result = mysql_fetch_assoc($rs_result)); ?>
Any help would be much appreciated :)
 
Old June 21st, 2008, 07:23 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi

You just need to set up a variable which counts up each time you do the loop, then add it in at the appropriate places:

<?php
$i = 0; // initialise our counter to 0
do {
  $i++; // add 1 to $i each loop. The first value will be 1, the next value 2.
  // we can now drop $i in wherever we need it
?>
      <tr>
          <td width="125" class="bodycopy"><?php echo "<a href=products_subcategory.php?subcatid=".$row_resu lt['subcatid'].">".$row_result['subcatname']."</a>"; ?></td>
          <td width="35" valign="top" align="left"><a href="#" target="_blank" onMouseOver="MM_swapImage('Image<?php echo $i; ?>','','/images1/arrow1.jpg',1)" onMouseOut="MM_swapImgRestore()"><img src="/images1/arrow.gif" name="Image<?php echo $i; ?>" width="15" height="15" border="0"></a></td>
          <td width="374">&nbsp;</td>
      </tr>
      <tr>
          <td colspan="3">&nbsp;</td>
      </tr>
<?php } while ($row_result = mysql_fetch_assoc($rs_result)); ?>

Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sequential numbering help gear1 Access 1 April 9th, 2007 06:31 AM
Sequential Numbers ricespn Access 5 February 17th, 2007 02:54 AM
Sequential Numbers ricespn SQL Server 2000 14 November 1st, 2006 12:08 PM
HELP!!!!! Sequential DTS e_cabrera_g SQL Server DTS 1 October 11th, 2006 11:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.