0

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.)

4
  • 1
    You're creating a new table for each database row. You need to only create the table (<table>) once, and create new rows (<tr>) for each row Commented Jun 7, 2013 at 9:37
  • open view source or right click and click view source then see where html is broken. and solve it. Commented Jun 7, 2013 at 9:38
  • prepare a style sheet, read css styling..use Firebug/chrome developer tool etc ..adjust margin/padding ..border table blah blah!! :) Commented Jun 7, 2013 at 9:38
  • Please look into PDO or MySQLI instead of using mysql_* functions. Commented Jun 7, 2013 at 9:45

6 Answers 6

2

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>";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that, how can I bring this to exact center of the page using <div> ?
Erm.. not entirely sure, I don't do too much with HTML and CSS anymore, but you can try setting the width of the div to 100%, and then set margin:auto on the table element
1

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

Comments

0

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>";

Comments

0

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.

Comments

0

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>';

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.