I'm currently working on a Pin Code search directory. I want to display districts count and district names of selected state as given in the following example. If there are 4 districts in Delhi state, the data should be rendered as:
<p>There are 4 districts in Delhi</p>
<ul>
<li>District 1</li>
<li>District 2</li>
<li>District 3</li>
<li>District 4</li>
</ul>
Please note that there are various rows of each district but I would like to use rowcount here to count all the rows of each district as 1, e.g. if there are 50 rows containing all 4 different districts, the total number should be 4.
The URL of this page is something like domain.com/pincode/state.php?state=StateName
Here is my code:
<?php
$state = $_GET['state'];
$con = mysqli_connect('localhost','dbuser','pass','dbname');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT stateName, districtName FROM pincodes WHERE stateName='$state'";
$result = $con->query($sql);
$rowcount = mysqli_num_rows($result);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "" . $row["districtName"]. "";
echo " | | ";
}
} else {
echo "<p>No results Found</p>";
}
mysqli_close($con);
?>
<h1><?php echo $state; ?> Pincode</h1>
<p>There are a total of <?php echo $rowcount; ?> districts in <?php echo $state; ?>.</p>
Please let me know how can I do this.
Thanks
Vikas