Reputation: 337
form.php
<div> Leaving From
<input type="text" class="from" name="source_point" id="source_point" title="Departure Place" required/>
</div>
<div class="right"><b>Going To
<input type="text" class="to" name="destination" id="destination" title="Arrival Place" required />
</div>
search.php
<table>
<tr>
<td>Sl No.</td>
<td>Bus Operator</td>
<td>Bus No. </td>
</tr>
<?php
$s=mysql_query("SELECT * FROM bus_detail where source_point='$_SESSION[source_point]' && destination_point='$_SESSION[destination]'");
while($row=mysql_fetch_array($s))
{
?>
<tr>
<td> </td>
<td><?php echo $row['bus_name'];?> </td>
<td><?php echo $row['bus_no'];?></td>
</tr>
<?php }?>
</table>
I want to display serial numbers from 1 according to the search results, in the empty <td>
field, not any random number.. It should be serial numbers in proper format ...
Upvotes: 0
Views: 37885
Reputation: 9635
user a count variable as initialize with 0 and increment it by 1 in each row display and you can print the serial no as this way.
<table>
<tr>
<td>Sl No.</td>
<td>Bus Operator</td>
<td>Bus No. </td>
</tr>
<?php
$count = 0;
$s=mysql_query("SELECT * FROM bus_detail where source_point='$_SESSION[source_point]' && destination_point='$_SESSION[destination]'");
while($row=mysql_fetch_array($s))
{
$count+=1;
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $row['bus_name'];?> </td>
<td><?php echo $row['bus_no'];?></td>
</tr>
<?php }?>
</table>
Upvotes: 1
Reputation: 31
You can add incremental value inside the loop
In your search.php, include
$i=1;
while($row=mysql_fetch_array($s))
{
?>
<tr>
<td><?php echo $i </td>
<td><?php echo $row['bus_name'];?> </td>
<td><?php echo $row['bus_no'];?></td>
</tr>
<?php $i++ }?>
</table>
Upvotes: 1
Reputation: 1702
It is as simple as adding a counter variable.
<?php
$s=mysql_query("SELECT * FROM bus_detail where source_point='$_SESSION[source_point]' && destination_point='$_SESSION[destination]'");
$counter = 0;
while($row=mysql_fetch_array($s))
{
?>
<tr>
<td><?php echo ++$counter; ?></td>
<td><?php echo $row['bus_name'];?> </td>
<td><?php echo $row['bus_no'];?></td>
</tr>
<?php }?>
You need to go through the basics if you couldn't figure this out.
Upvotes: 7