Simple PHP Gridview
If someone is migrating from ASP.Net to PHP than he must be searching for Gridview kind of funtionality in PHP.Basically this kind of job is accomplished in PHP by populating an HTML table at runtime i.e extracting each row from the database one by one and than build a table dynamically.Following code is doing the same...
<?php
$host="localhost";
$username="shoppincartUser";
$password="12345";
if (!$connect=mysql_connect($host,$username,$password))
{
echo "Error connecting with the database";
}
if (!mysql_select_db("example",$connect))
{
echo "Error selecting database";
}
$dataset = mysql_query("select * from students",$connect);
echo "<table border='1'>";
while ($row = mysql_fetch_assoc($dataset))
{
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['class']."</td>";
echo "<td>".$row['GPA']."</td>";
echo "</tr>";
}
echo "</table>";
?>
$host="localhost";
$username="shoppincartUser";
$password="12345";
if (!$connect=mysql_connect($host,$username,$password))
{
echo "Error connecting with the database";
}
if (!mysql_select_db("example",$connect))
{
echo "Error selecting database";
}
$dataset = mysql_query("select * from students",$connect);
echo "<table border='1'>";
while ($row = mysql_fetch_assoc($dataset))
{
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['class']."</td>";
echo "<td>".$row['GPA']."</td>";
echo "</tr>";
}
echo "</table>";
?>
The PHP mysql_fetch_assoc() funtion retrieves each row one by one from the dataset extracted by the mysql_query() funtion.The PHP mysql_fetch_assoc() funtion keeps returning true until all the rows are extracted.Hence, every time when a new row is retrieved the $row variable holds it and later it is used to populate the table.
0 comments:
Subscribe to:
Post Comments (Atom)
Post a Comment