Reputation: 91
In the following coding data is being fetched from mysql table, in the first column of table heading 'Sr.No.' i want to display loop counter variable $no in table data. Can anyone please tell the right syntax..
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>''</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student_name" value='.$row['student_name'].'>
<td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
Upvotes: 0
Views: 401
Reputation: 931
@Akhtar, I understand you are quite new to the PHP world. All the previous answers you have received are already right, but I would like to contribute in your question with some small styles suggestions that will help you writing good code and finding eventual issues by yourself.
Changes:
$total
and $stu
)input
elements moved into the td
(there should not be code between cells)'
) and double-quotes ("
), HTML with double quotes, PHP with quotesWith these changes, if you are going to use a modern IDE, you will have better suggestions and the code highlight will help you.
New Code:
<?php
$no = 0;
$total = 0;
?>
<table id="result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_array($query)): ?>
<?php
$total += $row['stu_id'];
//aren't you using this variable?
//$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
?>
<tr>
<td><?php echo ++$no ?></td>
<td><?php echo $row['student_id'] ?></td>
<td><?php echo $row['student_name'] ?></td>
<td>
<input type="hidden" name="student_id" value="<?php echo $row['student_id'] ?>">
<input type="hidden" name="student_name" value="<?php echo $row['student_name'] ?>">
<div class="search-block clearfix">
<input name="obtmarks" placeholder="" type="number">
</div>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
Upvotes: 1
Reputation: 245
You can print $no variable value:
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student_name" value='.$row['student_name'].'>
<td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
Upvotes: 1