rahul888
rahul888

Reputation: 413

MySql Table formatting in PHP

So this PHP Code would print all the table data from my database on a Web-Page.

<?php
$hostname = '127.0.0.1:3306';        
$dbname   = 'login'; // Your database name.
$username = 'root';             // Your database username.
$password = '';                 // Your database password. If your database has no password, leave it empty.

mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
mysql_select_db($dbname) or DIE('Database name is not available!');
$query="SELECT * FROM markers";
$result=mysql_query($query);

$fields_num = mysql_num_fields($result);
echo "<table border='2'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td></div>";
}
echo "</tr>\n";
// printing table rows

while($row = mysql_fetch_row($result))

{
    echo "<table>";
    echo "<tr>";

    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td>$row[2]</td>";
    echo "<td>$row[3]</td>";
    echo "</tr>\n";
    echo "</table></div>";

}
?>

But what Essentially it's doing is that. It's not properly formatted.It comes like this.

Unformatted Table

What I wanted to do Is to bring this table exactly in the center of the web-page, Give it some fancy borders(curvy edges, Great fonts.)

Upvotes: 0

Views: 772

Answers (6)

sanj
sanj

Reputation: 85

You don't need to put table header in the loop because header will be the same all the time. For header you can try this:

<table border="1" align="left">
<tr>
<th>Employee ID</th>
<th>Designation ID</th>
<th>First Name</th>
</tr>
if(!result)
while($row = mysql_fetch_row($result))
{
   <tr>
           <td><?php $row[0];?></td>
   </tr>
 } 
</table>

Upvotes: 0

Asenar
Asenar

Reputation: 7010

You need 2 loops (the 2nd inside the first),

$result = mysqli_query('your sql');
// begin table, only once
echo '<table>';
// use thead for the table header :)
echo '<thead>';
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_field($result);
    echo '<th>'.$field->name.'</th>';
}
echo '</thead>';
echo '<tbody>';
// 1st loop, each rows
while($row = mysqli_fetch_assoc($result))
{
    // each rows start with  <tr> / ends with </tr>
    echo '<tr>';
    // 2nd loop, each field
    foreach($row as $field => $value)
    {
        // you can also add class name in each case of your table, to add custom style
        // for each field in your css
        echo '<td class="'.$field.'">'.$value.'</td>';
    }
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';

Upvotes: 0

James Paterson
James Paterson

Reputation: 2905

Use

echo "<table>";
while($row = mysql_fetch_row($result))

{
    echo "<tr>";
    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td>$row[2]</td>";
    echo "<td>$row[3]</td>";
    echo "</tr>\n";
}
echo "</table>";

Then in the head of the document, use the following code:

<style>
table {
margin: 0 auto;
width: 50%;
}
</style>

In order to correctly center the table, it is necessary to set a width for it. It won't work otherwise.

Upvotes: 0

Pudge601
Pudge601

Reputation: 2068

You don't need to recreate the table for each row you are outputting, so change

while($row = mysql_fetch_row($result))

{
    echo "<table>";
    echo "<tr>";

    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td>$row[2]</td>";
    echo "<td>$row[3]</td>";
    echo "</tr>\n";
    echo "</table></div>";

}

to

echo "<div><table>";
while($row = mysql_fetch_row($result))

{
    echo "<tr>";
    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td>$row[2]</td>";
    echo "<td>$row[3]</td>";
    echo "</tr>\n";
}
echo "</table></div>";

Upvotes: 2

zkanoca
zkanoca

Reputation: 9918

Change

    while($row = mysql_fetch_row($result))

{
    echo "<table>";
    echo "<tr>";

    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td>$row[2]</td>";
    echo "<td>$row[3]</td>";
    echo "</tr>\n";
    echo "</table></div>";

}

to

 echo "<table>";
 while($row = mysql_fetch_row($result))

{
    echo "<tr>";
    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td>$row[2]</td>";
    echo "<td>$row[3]</td>";
    echo "</tr>\n";    
}
echo "</table>";

Upvotes: 0

Purpletoucan
Purpletoucan

Reputation: 6572

You're creating a new table for every iteration through rows. Move the <table> tags outside of the loop.

Upvotes: 1

Related Questions