Re:自动生成自动增量编号

Re:自动生成自动增量编号

问题描述:

I tried to conclude the tread I started last time on how to Auto generate serial numbers as seen here Auto generate serial numbers. On my own I have been able to come out with a code that give me what I want.

Using this line of code

   SELECT COUNT(donor_id) + 1 AS Counter
   FROM tbl_donors

I got the value I want which is 6+1; i.e. 6 being the total number of records and 1 is the additional for the new record. But now how to add it to the tableset that will show all the values as seen here http://www.netdataflow.com/rbme/ is where I am having a problem. I am using dreamweaver to builder my tableset and below is the tableset code

<div align="center">
  <table width="1100" border="0" cellpadding="0" cellspacing="0">
    <tr id="colhead">
      <td width="30" height="30" id="labels"><div align="center"><strong>ID</strong> 
 </div></td>
      <td width="200" height="30" id="labels"><div align="center"><strong>Donor  
Name</strong></div></td>
      <td width="100" height="30" id="labels"><div align="center"> 
<strong>Designation</strong></div></td>
      <td width="250" height="30" id="labels"><div align="center">
<strong>Address</strong></div></td>
      <td width="80" height="30" id="labels"><div align="center"><strong>City</strong>
</div></td>
      <td width="80" height="30" id="labels"><div align="center"><strong>State</strong>
</div></td>
      <td width="80" height="30" id="labels"><div align="center">
<strong>Country</strong></div></td>
      <td width="100" height="30" id="labels"><div align="center"><strong>Phone</strong>
</div></td>
      <td width="150" height="30" id="labels"><div align="center"><strong>Email 
Address</strong></div></td>
    </tr>
    <?php do { ?>
      <tr <?php 
 // technocurve arc 3 php bv block2/3 start
echo " style=\"background-color:$color\"";
// technocurve arc 3 php bv block2/3 end
?> id="rowlines">
        <td id="labels"><div align="center"><?php echo $row_rsdonors['donor_id']; ?>  
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['donorname']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['designation']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['address']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['city']; ?></div>
</td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['state']; ?></div>
</td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['country']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['phone']; ?></div>
</td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['emailaddr']; ?>
</div></td>
      </tr>
      <?php 
// technocurve arc 3 php bv block3/3 start
if ($color == $color1) {
$color = $color2;
} else {
$color = $color1;
 }

Can anyone help out? If possible, I will want to add to the

 SELECT COUNT(donor_id) + 1 AS Counter
 FROM tbl_donors

to the first row of the table before the ID. I am open to any other idea or way or doing it, possibly manually coding the table instead of using dreamweaver dynamic tableset.

I appreciate you help in advance.

Mike

I have been able to get the right code for my questions on "Auto generate Auto Increment numbers". I think the issue I had was the title to my question. The right title should have been "How to generate table row numbers"

Anyway find below the code I used. Here is the reference link: With MySQL, how can I generate a column containing the record index in a table?

SELECT  d.*, @curRow := @curRow + 1 AS row_number
FROM    tbl_donors d
JOIN    (SELECT @curRow := 0) r
WHERE user_name = %s"  **//*Please dont add this WHERE clause if you don't need it. In my case I filter records based on the person who entered it hence the WHERE clause** 

But because I am using Dreamweaver, Dreamweaver changed it to this format:

mysql_select_db($database_yourdatabasename, $yourdatabasename);
$query_rsdonors = sprintf("SELECT  d.*, @curRow := @curRow + 1 AS row_number 
FROM tbl_donors d JOIN    (SELECT @curRow := 0) r WHERE user_name = %s",  
GetSQLValueString($colname_rsdonors, "text"));
$query_limit_rsdonors = sprintf("%s LIMIT %d, %d", $query_rsdonors, $startRow_rsdonors,   
1$maxRows_rsdonors);
$rsdonors = mysql_query($query_limit_rsdonors, $ProjMonEva) or die(mysql_error());
$row_rsdonors = mysql_fetch_assoc($rsdonors);

Here is the table code structure

<div align="center">
  <table width="1130" border="0" cellpadding="0" cellspacing="0">
    <tr id="colhead">
      <td width="30" height="30" id="labels"><strong>SN</strong></td>
      <td width="30" height="30" id="labels"><div align="center"><strong>ID</strong>  
</div></td>
      <td width="200" height="30" id="labels"><div align="center"><strong>Donor 
Name</strong></div></td>
      <td width="100" height="30" id="labels"><div align="center">
<strong>Designation</strong></div></td>
      <td width="250" height="30" id="labels"><div align="center">
<strong>Address</strong></div></td>
      <td width="80" height="30" id="labels"><div align="center"><strong>City</strong>
</div></td>
      <td width="80" height="30" id="labels"><div align="center"><strong>State</strong>
</div></td>
      <td width="80" height="30" id="labels"><div align="center">
<strong>Country</strong></div></td>
      <td width="100" height="30" id="labels"><div align="center"><strong>Phone</strong>
</div></td>
      <td width="150" height="30" id="labels"><div align="center"><strong>Email 
Address</strong></div></td>
    </tr>
    <?php do { ?>   
      <tr <?php 
// technocurve arc 3 php bv block2/3 start
echo " style=\"background-color:$color\"";
// technocurve arc 3 php bv block2/3 end
?>id="rowlines">
        <td id="labels"><?php echo $row_rsdonors['row_number']; ?></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['donor_id']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['donorname']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['designation']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['address']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['city']; ?></div>
</td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['state']; ?></div>
</td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['country']; ?>
</div></td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['phone']; ?></div>
</td>
        <td id="labels"><div align="center"><?php echo $row_rsdonors['emailaddr']; ?>
</div></td>
      </tr>
      <?php 
// technocurve arc 3 php bv block3/3 start
if ($color == $color1) {
$color = $color2;
} else {
$color = $color1;
}

Here is a brief of what I am getting

SN       Donor Name              Designation
 1    Mr Michael Nwuzor       Chief Consultant
 2    Mr Michael Nwuzor       Chief Consultant
 3    South-Sea Datcomm Ltd

Here is the link from where I was able to get the right code: With MySQL, how can I generate a column containing the record index in a table?

Just take one variable "$counter" and use this to display the serial number

I am adding just one example using your code. Please go through it.

<div align="center">
  <table width="1100" border="0" cellpadding="0" cellspacing="0">
    <tr id="colhead">
  <td width="30" height="30" id="labels"><div align="center"><strong>SI NO.</strong>     
      </div></td>
      <td width="30" height="30" id="labels"><div align="center"><strong>ID</strong>
      </div></td>
      <td width="200" height="30" id="labels"><div align="center"><strong>Donor Name</strong></div></td>
      <td width="100" height="30" id="labels"><div align="center"><strong>Designation</strong></div></td>
    </tr>
    <?php
    $counter = 1;
            for($i=0;$i<$total_rec;$i++) //"$total_rec" is the total number of records found in your required table
    {
?>
   <tr id="rowlines">
      <td id="labels"><div align="center"><?php echo $counter; ?></div></td>
  <td id="labels"><div align="center"><?php echo $row_rsdonors['donor_id']; ?></div></td>
      <td id="labels"><div align="center"><?php echo $row_rsdonors['donorname']; ?></div></td>
      <td id="labels"><div align="center"><?php echo $row_rsdonors['designation']; ?></div></td>
  </tr>
<?php 
    $counter++;
 }
?>