0

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>
4
  • 2
    Print the $no in first <td> in the loop. <td>'.$no.'</td> Commented Sep 26, 2018 at 6:05
  • You defined $no, now use it somewhere. Anywhere you want it to appear. Commented Sep 26, 2018 at 6:05
  • concat .$no. in first <td>. Commented Sep 26, 2018 at 6:06
  • @Siva thanx alot. I have tried it many times, but i am seriously surprised why it was not working for me. however, its working fine now.. Commented Sep 26, 2018 at 6:10

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

@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:

  • general variables moved to the top
  • PHP is not more printing HTML, but just contents
  • merged all the new variables in a single block ($total and $stu)
  • commented not used variable (may be you will use it in future development)
  • input elements moved into the td (there should not be code between cells)
  • uniformed quotes (') and double-quotes ("), HTML with double quotes, PHP with quotes
  • changed the while parentheses with a clearer syntax (you will find it useful with long HTML)

With 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.