1

I am styling a page that has PHP in the middle that echos out a table. I have the html centered through an external style sheet but the PHP section(where the table is wont center) so I am thinking the best way to style this is for inline css in the php section. any help is appreciated.

// display data in table




echo "<table border='1'  cellpadding='2' class='footable mdl-data-table mdl-js-data-table mdl-data-table--selectable mdl-shadow--4dp full-width'>";

echo "<tr> <th>ID</th> <th>Administration Console</th> <th>Product Version</th> <th>Platform</th> <th>Database</th> <th>Owner</th> <th>Status</th> <th></th> <th></th></tr>";



// loop through results of database query, displaying them in the table

while($row = mysql_fetch_array( $result )) {



// echo out the contents of each row into a table

echo "<tr>";

echo '<th>' . $row['id'] . '</th>';

echo '<td><a href="'.$row['curl'].'">'.$row['curl'].'</a></td>';

echo '<td>' . $row['pversion'] . '</td>';

echo '<td>' . $row['platform'] . '</td>';

echo '<td>' . $row['dversion'] . '</td>';

echo '<td><a href="mailto:'.$row['email'].'">' . $row['email'].'</a></td>';

echo '<td>' . $row['status'] . '</td>';

echo '<td><a href="php/edit.php?id=' . $row['id'] . '">Edit</a></td>';

echo '<td><a onclick="javascript:confirmationDelete($(this));return false;" href="php/delete.php?id=' . $row['id'] . '"onclick="return confirm("Do you want to delete this?")">Delete</a></td>';

echo "</tr>";

}





// close table>

echo "</table>";

?> 
9
  • 2
    Why can't you simply style the table with the same external stylesheet? Commented Mar 27, 2017 at 14:27
  • I have an external style that is styling the rest of the page( centering it) but the PHP part(table) is not moving to the center Commented Mar 27, 2017 at 14:29
  • 1
    If you're writing new code, please don't use the mysql_* functions. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use PDO or mysqli_* with prepared statements and parameter binding instead. See stackoverflow.com/q/12859942/354577 for details. Commented Mar 27, 2017 at 14:29
  • The table should be style-able via CSS no problem. Make sure the positioning of your table is within the specificity range of the style sheet. You are confusing one problem to be the result of an unrelated output technique. Either target your table properly or figure out how to write CSS which will do what you need. Commented Mar 27, 2017 at 14:31
  • Is there a reason why you use th and td in a tr-tag? As far as I know you need then the scope="row" attribute... But it is years ago, that i used tables-layout on a website. Commented Mar 27, 2017 at 14:33

2 Answers 2

3

You can use this but all tables will be centered

table {
    margin: 0 auto;
}

or define a class to identify the specific table

table .customClass {
    margin: 0 auto;
}

<table border='1' cellpadding='2' class='customClass footable mdl-data-table mdl-js-data-table mdl-data-table--selectable mdl-shadow--4dp full-width'>

or you can try using align="center"

<table align="center" border='1' cellpadding='2' class='footable mdl-data-table mdl-js-data-table mdl-data-table--selectable mdl-shadow--4dp full-width'>

Hope this might help you.

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed. @Daniel, you have to keep in mind that PHP is just basically creating the HTML. It does not interrupt the styling. This should work.
It's a pleasure Daniel
1

The same style mark in the front will be covered. example:

<style>
    th, td{
        text-align: center;
    }
    th, td{
        text-align: left;
    }
</style>

Then the text would be display from left.

To solve the problem simply, you can try to add these code below all of the css links:

<style>
    th, td{
        text-align: center;
    }
</style>

If it doesn't work, I suggest that you try this :-)

echo '<th style="text-align: center">' . $row['id'] . '</th>';

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.