Reputation: 7
How to display Serial Numbers based on the number raw count with mysql query result in my html table. Which help my table user count the no of record exist.How can I code this? I have try to use
<?php if(count($records) > 0) { ?>
<?php
foreach ($records as $row){?>
but it shows error:
<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>
Upvotes: 1
Views: 4527
Reputation: 1
In case you are using while loop, initiate $i = 0, followed by while loop, then echo ($i=$i+1); to print serial number.
Upvotes: 0
Reputation: 1210
Use The $count Variable For Printing Serial Number, It Will continuously adding 1 Number Until The Loop Running.
<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>
Upvotes: 0
Reputation: 9635
use a counter variable and increment it in each row like this
$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
}
?>
you can also use your $i variable for it. like this
<td><?php echo ($i+1); ?></td>
instead of
<td><?php echo $counter; ?></td>
Upvotes: 2