How to add template in codeigniter |how to create template in codeigniter| template in codeigniter 4
Auto increment result while fetch data from database in PHP
Auto increment result in PHP: this tutorial will be helpful for you, if you want to set auto increment result while loop is running and fetch records in a table from MySQL table.
I will give you the source code in the example. It helps you to set serial no in the table with auto number list and calculate sum of value while loop fetch records from database. That’s like
Auto number list:
<table class="table" style="width: 100%;"> <tr> <th>SL No:</th> <th style="text-align: center;">Student Name</th> <th style="text-align: center;">Student Address</th> </tr> <?php $i=0; foreach ($print_val as $pvalue) { ?> <tr> <td style="text-align: center;"><?php echo $pvalue->student_name; ?></td> <td style="text-align: center;"><?php echo $pvalue->student_address; ?></td> </tr> <?php $i++; } ?> </table>
Sum of value:
<table class="table" style="width: 100%;"> <tr> <th>SL No:</th> <th style="text-align: center;">User Name</th> <th style="text-align: center;">Amount</th> </tr> <?php $i=0; $total_price=0; foreach ($print_val as $pvalue) { $total_price=$total_price + $pvalue->price; ?> <tr> <td style="text-align: center;"><?php echo $pvalue->name; ?></td> <td style="text-align: center;"><?php echo $total_price; ?></td> </tr> <?php $total_price++; } ?> </table>
Related posts
foreach is a bad habbit for read from database.