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

时间:2014-03-07 13:21:08

标签: php mysql sql

我试着总结一下我上次开始如何自动生成序列号的步骤,如Auto generate serial numbers所示。就我自己而言,我已经能够提供一个能够满足我想要的代码。

使用这行代码

   SELECT COUNT(donor_id) + 1 AS Counter
   FROM tbl_donors

我得到了我想要的值6 + 1;即6是记录总数,1是新记录的附加值。但是现在如何将它添加到将显示此处 http://www.netdataflow.com/rbme/ 所见的所有值的表格集中,这是我遇到问题的地方。我使用Dreamweaver构建我的表集,下面是表集代码

<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;
 }

任何人都可以帮忙吗?如果可能的话,我想添加到

 SELECT COUNT(donor_id) + 1 AS Counter
 FROM tbl_donors

到ID之前的表的第一行。我对任何其他想法或方式或做到这一点持开放态度,可能手动编码表而不是使用Dreamweaver动态表集。

我感谢您提前帮助。

麦克

2 个答案:

答案 0 :(得分:0)

只需取一个变量“$ counter”并使用它来显示序列号

我只使用您的代码添加一个示例。请仔细阅读。

<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++;
 }
?>

  

答案 1 :(得分:0)

我已经能够在“自动生成自动增量数字”上获得正确的代码。我认为我遇到的问题是我的问题的标题。正确的标题应该是“如何生成表格行号”

无论如何在下面找到我使用的代码。以下是参考链接: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** 

但是因为我使用Dreamweaver,Dreamweaver将其更改为以下格式:

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);

这是表格代码结构

<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;
}

以下是我的内容简介

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

以下是我能够获得正确代码的链接:With MySQL, how can I generate a column containing the record index in a table?