显示序列号

时间:2014-01-30 11:06:49

标签: php mysql sql

如何在我的html表格中使用mysql查询结果显示基于原始计数的序列号。 哪个帮助我的表用户计数记录的存在。我怎么能编码呢?我试着用

    <?php if(count($records) > 0) { ?>
<?php
            foreach ($records as $row){?>

但它显示错误:

  <table border="1" cellspacing="0" cellpadding="2" >
 <thead>
<tr>
 <th> Serial No </th>

    <th> Agent ID </th>
    <th> Name of Agent</th>
    <th> Agent Mobile</th>
    <th> Agent Card No</th>

    <th> POS Terminal </th>
    <th> APN Mobile No</th>
    <th> Update</th>
</tr>
 </thead>
  <tbody>
<?php
    include('connect.php');
    $result = $db->prepare("SELECT  `Agentid`,`agentname`,`phone`,             

     `meghna_c_no`, `pos_no`, `apn_mobile` FROM `agent` where `status`=2 
      ORDER BY  Agentid DESC");
    $result->execute();
    for($i=0; $row = $result->fetch(); $i++){
?>

<tr class="record">


    <td><?php echo $row['Agentid']; ?></td>
    <td><?php echo $row['agentname']; ?></td>
    <td><?php echo $row['phone']; ?></td>
    <td><?php echo $row['meghna_c_no']; ?></td>
    <td><?php echo $row['pos_no']; ?></td>
    <td><?php echo $row['apn_mobile']; ?></td>
    <td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>"> 
        Insert </a>   </td>
</tr>

<?php
    }
?>

  </tbody>
  </table>

2 个答案:

答案 0 :(得分:1)

使用计数器变量并在每行中递增它

$result->execute();
$counter = 0; // initialize the counter
        for($i=0; $row = $result->fetch(); $i++){
$counter+=1; // increment the counter by 1
    ?>

    <tr class="record">

        <td><?php echo $counter; ?></td> <!-- display the counter -->
        <td><?php echo $row['Agentid']; ?></td>
        <td><?php echo $row['agentname']; ?></td>
        <td><?php echo $row['phone']; ?></td>
        <td><?php echo $row['meghna_c_no']; ?></td>
        <td><?php echo $row['pos_no']; ?></td>
        <td><?php echo $row['apn_mobile']; ?></td>
        <td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>"> 
            Insert </a>   </td>
    </tr>

    <?php
        }
    ?>

你也可以使用你的$ i变量。像这样

<td><?php echo ($i+1); ?></td> 

而不是

<td><?php echo $counter; ?></td>

答案 1 :(得分:0)

使用$ count变量打印序列号,它会不断添加1个数字直到循环运行。

<table border="1">

    <thead>
      <tr>
        <th> Serial No </th>
        <th> Agent ID </th>
    <th> Name of Agent</th>
    <th> Agent Mobile</th>
    <th> Agent Card No</th>

    <th> POS Terminal </th>
    <th> APN Mobile No</th>
    <th> Update</th>
  </tr>
</thead>
<tbody>
`<?php
    include('connect.php');


     $result = $db->prepare("SELECT  `Agentid`,`agentname`,`phone`,`meghna_c_no`, `pos_no`, `apn_mobile` FROM `agent` where `status`=2 
      ORDER BY  Agentid DESC");
    $result->execute();
    $count = 0; // Initializing The Counter
    for($i=0; $row = $result->fetch(); $i++)
    {
    $count++; //increment the counter ; ?>`
<tr class="record">

    <td><?php echo $count; ?></td>
    <td><?php echo $row['Agentid']; ?></td>
    <td><?php echo $row['agentname']; ?></td>
    <td><?php echo $row['phone']; ?></td>
    <td><?php echo $row['meghna_c_no']; ?></td>
    <td><?php echo $row['pos_no']; ?></td>
    <td><?php echo $row['apn_mobile']; ?></td>
    <td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>"> 
        Insert </a>   
    }
    </tr>
</td>