Dinesh DiNu
Dinesh DiNu

Reputation: 699

Serial number not working in PHP MySql Table

I would like to add serial number to my table content via PHP mySql data. I have tried with my code but I am not able to complete the table result. I hope there is no error in my code, but there is an empty field has been shown in my table's serial number data. Here it is my PHP code..,

<?php
    include('config.php');
    $data_content = '';
    $qry = "SELECT DISTINCT areaName FROM area_Info";
    $result = mysql_query($qry);
    $serial = 1;
    while($row = mysql_fetch_array($result))
    {   
        $data_content .= "<tr><td><?php echo $serial; ?></td><td><a href=\"areaData.php?area=".$row['areaName']."\">". $row['areaName'] ."</a></td></tr>";
    $serial++;
    }
    mysql_close();
    ?>

This is my html code

<html lang="en">
<head>
<body>
<table class='area-table'>
    <?php echo $data_content; ?>
</table>
</body>
</html>

Upvotes: 1

Views: 600

Answers (2)

Sashant Pardeshi
Sashant Pardeshi

Reputation: 1095

Issue is with php tag inside of string quotes. Please try replace your code

$data_content .= "<tr><td><?php echo $serial; ?></td><td><a href=\"areaData.php?area=".$row['areaName']."\">". $row['areaName'] ."</a></td></tr>";

with this below code

$data_content .= "<tr><td>".$serial."</td><td><a href=\"areaData.php?area=".$row['areaName']."\">". $row['areaName'] ."</a></td></tr>";

Also try using mysqli as mysql is deprecating now.

Upvotes: 2

someOne
someOne

Reputation: 1675

Change the line to:

$data_content .= "<tr><td>$serial</td><td><a href='areaData.php?area={$row['areaName']}'>{$row['areaName']}</a></td></tr>";

And avoid using the monstrous mysql_*.

Upvotes: 1

Related Questions