Error while using materialize css table - php

I have a php file designed to retrieve information from database. The problem is that if i use a normal table tags like the following it would work and show the page for me.
<?php /*Template Name: contactread */
get_header();
$servername = "localhost";
$username = "***** (for security purpose only)";
$password = "*****";
$dbname = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * FROM contact";
$result = mysqli_query($conn,$query);
echo "<table class='table'>
<tr>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>نام</th>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>ایمیل</th>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>پیام</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['name'] . "</td>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['email'] . "</td>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
And When i use it like the following which is calling everything from the framework, it gives me the http 500 error. (I included the entire page for showing the first type of table so you have the idea of the page, however i dont see a reason to re write it all again)
echo "<table>
<thead>
<tr>
<th>نام</th>
<th>ایمیل</th>
<th>پیام</th>
</tr>
</thead>
<tbody>";
while($row = mysqli_fetch_array($result))
{
echo <tr>
echo <td>. $row['name'] .</td>
echo <td>. $row['email'] .</td>
echo <td>. $row['message'] .</td>
echo </tr>
}
echo "</tbody>
</table>";
Can someone help me why is this making the error? I couldnt find anything helpful in the internet.
EDIT: I have added materialize-css css files with the link in the header and i have included the header as well.

Simple mistake. You are not wrapping the data you want to echo in quotes, so try doing it like this.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[name]</td>";
echo "<td>$row[email]</td>";
echo "<td>$row[message]</td>";
echo "</tr>";
}

Related

Php Dynamic Table Showing Server Date/Time Format

I have Created the Program where I am Fetching the Content from a text file from a cpanel server,
In my Table Fields It shows the Date Format in Server format i.e (YYYY/MM/DD) I want it to Display as
(DD/MM/YYYY).
This is My Code
<?php
$data = file_get_contents("file-path.txt");
$arr = explode(";", $data);
echo "<thead>";
echo "<tr style=\"border: 1px solid black;\">";
echo "<th style=\"border: 1px solid black;\">Doctor Name</th>";
echo "<th style=\"border: 1px solid black;\">Leave From (YYYY/MM/DD)</th>";
echo "<th style=\"border: 1px solid black;\">Leave To (YYYY/MM/DD)</th>";
echo '<th style="border: 1px solid black;">Remarks</th>';
echo "</tr>";
echo "</thead>";
echo "<tbody>";
for($value = 0; $value < count($arr)-1; $value=$value+4) {
echo "<tr style=\"border: 1px solid black;\">";
echo "<td style=\"border: 1px solid black;\">".$arr[$value]."</td>";
echo "<td style=\"border: 1px solid black;\">".$arr[$value+1]."</td>";
echo "<td style=\"border: 1px solid black;\">".$arr[$value+2]."</td>";
echo '<td style="border: 1px solid black;">'.$arr[$value+3].'</td>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
You should use date_format() function bro
date_format($arr[$value+2],"d/m/Y");
date('d/m/Y', strtotime($arr[$value+2]));
Have a try, you can do it.

PHP controlling where a table is placed on a web page

I'm new to PHP and I'm working on a very simple SELECT query pulling information from a MySQL database. I have an echo statement that prints out that my connection to the database was successful which is the very first line on the webpage. After "Connected Successfully" is display the SELECT statement pulls the query information into a table.
Right now everything is pulling and displaying correctly but there is a large gap between my echo connection statement and the actual table results. I've tried to directly align my table to 'TOP' but that is still producing the large gap.
<!DOCTYPE html>
<html>
<style>
#trip th {
padding-top: 5px;
padding-bottom: 5px;
text-align: left;
background-color: #4CAF50;
color: white;
}
#trip {
border-collapse: collapse;
width: 75%;
}
#trip td, #trip th {
border: 1px solid #ddd;
padding: 8px;
}
#trip tr:nth-child(even){
background-color: #f2f2f2;
}
#trip tr: hover {
background-color: #ddd;
}
</style>
<body>
<?php
require "Login.php";
?>
<?php
//Create Connection
$conn = mysqli_connect($serverName, $userName, $password, $dbName);
//Check connection
if ($conn === false) {
die("ERROR: Could not Connect. " . mysqli_connect_error());
}
echo "Connected successfully";
//SQL SELECT query execution
$sql = "SELECT * FROM Trip";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table id='trip' align='top'>";
echo "<tr>";
echo "<th>TID </th>";
echo "<th>NAME </th>";
echo "<th>DATE </th>";
echo "<th>COST </th><br>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['TID'] . " </td>";
echo "<td>" . $row['NAME'] . " </td>";
echo "<td>" . $row['DATE'] . " </td>";
echo "<td>" . $row['COST'] . " </td>";
echo "</tr>";
}
echo "</table>";
//Free result set
mysqli_free_result($result);
}
else {
echo "No records matching your query were found";
}
}
else {
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
//Close connection
mysqli_close($conn);
?>
</body>
</html>
I'd like the table to display right underneath my "Connected Successfully" message but something seems to be putting the table further down the page.
Edit Fixed the '\' that was added when I tried to paste into Stackoverflow. Not sure why that was added. It's not in my actual PHP file.
The problem is the <br> you have at the end of this line:
echo "<td>" . $row['COST'] . " </td><br>";
Since <br> isn't allowed inside <tr> elements, the browser is rendering them before the table. So the number of blank lines before the table is the same as the number of table rows.
Get rid of that, it serves no purpose.
#trip th {
padding-top: 5px;
padding-bottom: 5px;
text-align: left;
background-color: #4CAF50;
color: white;
}
#trip {
border-collapse: collapse;
width: 75%;
}
#trip td,
#trip th {
border: 1px solid #ddd;
padding: 8px;
}
#trip tr:nth-child(even) {
background-color: #f2f2f2;
}
#trip tr: hover {
background-color: #ddd;
}
<div>Connected successfully</div>
<table id='trip' align='top'>
<tr>
<th>TID </th>
<th>NAME </th>
<th>DATE </th>
<th>COST </th>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
</table>

Need to put color to a html table through css

<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$co_name = mysqli_real_escape_string($con, $_POST['co_name']);
$co_address = mysqli_real_escape_string($con, $_POST['co_address']);
$co_website = mysqli_real_escape_string($con, $_POST['co_website']);
$co_phoneno = mysqli_real_escape_string($con, $_POST['co_phoneno']);
$co_contactperson = mysqli_real_escape_string($con, $_POST['co_contactperson']);
$therapist_id = mysqli_real_escape_string($con, $_POST['therapist_id']);
$result = mysqli_query($con,"SELECT * FROM therapist_office WHERE therapist_id='".$user_id."'");
echo"<table id='miyazaki' style='border-collapse: collapse; border: 1px solid; margin-left:180px; width:1000px; margin-top:30px;'>";
echo"<thead style='border:1px solid; background-color:#ffe3ab;' >";
echo"<tr>";
echo"<th style='border: 1px solid; padding: .65em;' >Office Name</th>";
echo"<th style='border: 1px solid; padding: .65em;' >Address</th>";
echo"<th style='border: 1px solid; padding: .65em;' >Website</th>";
echo"<th style='border: 1px solid; padding: .65em;' >PhoneNo</th>";
echo"<th style='border: 1px solid; padding: .65em;'>Contact Person</th>";
echo"</tr>";
echo"</thead>";
while($row = mysqli_fetch_array($result))
{
echo"<tbody>";
echo"<tr>";
echo "<td style=' padding: .65em;'>" . $row['co_name'] . "</td>";
echo "<td style=' padding: .65em;'>" . $row['co_address'] . "</td>";
echo "<td style=' padding: .65em;'>" . $row['co_website'] . "</td>";
echo "<td style=' padding: .65em;'>" . $row['co_phoneno'] . "</td>";
echo "<td style=' padding: .65em;'>" . $row['co_contactperson'] . "</td>";
echo"</tr>";
echo"</tbody>";
}
echo"</table>";
mysqli_close($con);
?>
I have this table that is simple but i am not able to add colors through css to it, at present i have used inline css will convert it to external css later. i wish to show the table head with different color and the rows of the table that contain data should be of 2 colors that run alternatively. Would appreciate if someone could help me
P.S some people wanted to see my other sheet, here it is
<style>
table { border-collapse: collapse; font-family: Futura, Arial, sans-serif; border: 1px solid ; margin-left:180px; width:1000px; margin-top:30px;}
caption { font-size: larger; margin: 1em auto; }
th, td { padding: .65em; }
th, thead { background-color: ffecc4; border: 1px solid ; }
tr:nth-child(odd) { background: #ccc; }
tr:hover { background: #aaa; }
td { border-right: 1px solid #777; }
</style>
In html TABLE you cannot give background-color or color to <thead>. <thead> doesn't not take any colors. You can do..
#miyazaki thead th{
background-color:red;
color:#000;}
And for alternate colors you can apply
#miyazaki tr:nth-child(odd){
background: #b8d1f3;
}
#miyazaki tr:nth-child(even){
background: #dae5f4;
}
Do it with jquery:
<script>
$(document).ready(function() {
$("#miyazaki tr:even").css("background-color", "#CCC");
$("#miyazaki tr:odd").css("background-color", "#FFF");
});
</script>
You can do it with CSS:
#miyazaki tr:nth-child(even) {background: #CCC}
#miyazaki tr:nth-child(odd) {background: #FFF}
Reference
Note: No support in IE 8 for this.
Or, if you have jQuery:
<script>
$(document).ready(function() {
$("#miyazaki tr:even").css("background-color", "#CCC");
$("#miyazaki tr:odd").css("background-color", "#FFF");
});
</script>
if you add classes to you code e.g
`echo"<th style='border: 1px solid; padding: .65em;' class='tblHead'>Office Name</th>";`
and
`echo "<td style=' padding: .65em;'>" . $row['co_name'] . " class='tblRow' </td>";`
Then you can write some CSS to set styles for those classes.
like:
.tblHead {
background-color: yellow;
}
.tblRow{
background-color: green;
}

Dashed line not showing HTML, PHP, MySql

I have a database that is out putting a specific row. My code does that but I want a dashed border around it for my table. The code below should make a dashed line around this row but I don't know why it not working. Can someone help?
$count = 1
echo "<table>";
echo "<tr> <th>Pos</th> <th>Team</th> <th>PLD</th> <th>W</th> <th>D</th>
<th>L</th> <th>F</th> <th>A</th> <th>GD</th> <th>PTS</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if($count == 5) {
echo "<tr style='border-style:dashed'><td>";
echo $row['Pos'];
echo "</td><td>";
echo $row['Team'];
echo "</td><td>";
echo $row['PLD'];
echo "</td><td>";
echo $row['W'];
echo "</td><td>";
echo $row['D'];
echo "</td><td>";
echo $row['L'];
echo "</td><td>";
echo $row['F'];
echo "</td><td>";
echo $row['A'];
echo "</td><td>";
echo $row['GD'];
echo "</td><td>";
echo $row['PTS'];
echo "</td></tr>";
}
else {
echo "<tr><td>";
}
Direct styling of is not allowed.
You should put the border on the elements and use border-right and border-left to remove the border in between columns.
<tr>
<td style="border: 1px dashed black; border-right: none">Left</td>
<td style="border: 1px dashed black; border-left: none; border-right: none">Middle</td>
<td style="border: 1px dashed black; border-left: none; border-right: none">Middle</td>
<td style="border: 1px dashed black; border-left: none">Right</td>
</tr>
You really should use html classes and a CSS file though.

PHP alternating colors

$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
I wanted to add an alternating color in the loop. what code should I add?
$rowCount = 0;
$colorOne = '#ffffff';
$colorTwo = '#f3f3f3';
while($row = mysql_fetch_array($result)){
$rowColor = ($rowCount % 2) ? $colorOne : $colorTwo;
echo "<element bgcolor='$rowColor'></element>";
$rowCount++;
}
Edited after #Jayrox comment.
Use CSS classes instead of inline styles. They're easier to manipulate. Then style the .myclass_row_0 and .myclass_row_1 in your CSS for the alternate row color as well as .col1 and .col2 for the column styles.
$c=1; // or 0 (added after deceze's comment)
while($row = mysql_fetch_array($result)) {
$c=1-$c; // magic!
echo "
<tr class=\"myclass_row_$c\">
<td class=\"col1\">".$row['first_name']." ".$row['last_name']."</td>
<td class=\"col2\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
Or using inline styles:
$colors = array('#ffffff','#f3f3f3');
$c=1;
while($row = mysql_fetch_array($result)) {
$c=1-$c;
echo "
<tr style=\"background-color:{$colors[$c]};\">
<!-- tds here -->
</tr>
";
}
Or using preset classes:
$styles = array('odd','even');
$c=1;
while($row = mysql_fetch_array($result)) {
$c=1-$c;
echo "
<tr class=\"{$styles[$c]}\">
<!-- ... -->
</tr>
";
}
There are already a lot of solutions, but this one use only two lines of css.
You can set specific css on even and odd rows:
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
Usually, I will declare an index(int) value and increment for each time through the loop. Then do a check to see if index mod 2 = 1. If so, then output a tr with the style that you want to apply to show an alternating row.
$color = 0;
while($row = mysql_fetch_array($result)) {
if($color % 2 == 1){
echo "<tr>";
}else{
echo "<tr class=\"altRow\">";
}
echo "
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$color++;
}
As it's bumped already, here is my 5 cents.
7 answers and not a single one using templates.
We can write a thousand articles of the templates necessity, but such an examples will always win.
So, based on the OP's code and stagas' answer:
business logic part:
$c = 1;
$DATA = array();
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($result)) {
$row['c'] = $c=1-$c;
$DATA[] = $row;
}
and template part:
<tr class="myclass_row_<?=$row['$c']?>">
<td class="col1"><?=$row['first_name']?> <?=$row['last_name']?></td>
<td class="col2">
<input type="button" name="edit" value="Edit" class="selbtn"> <input type="button" name="delete" value="Delete" class="selbtn">
</td>
</tr>
$color = 1;
while($row = mysql_fetch_array($result)) {
if($color == 1){
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$color = 2;
}
else
{
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #555; border-right: 1px solid #555;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #555; border-right: 1px solid #555; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
$color = 1;
}
// here it is in less code
<?php
$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql);
$colors = array('FFF', '000000'); // valid hex colors
$numOfColors = sizeOf($colors); $i = 0
while($row = mysql_fetch_array($result)) {
$i++;if($i>$numOfColors){$i=0;}
?>
<tr>
<td style="padding-left: 20px; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>;"> <?=$row['first_name']?> <?=$row['last_name']?> </td>
<td style="text-align: center; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>; padding-top: 2px ; padding-bottom: 3px ;"><input type="button" name="edit" value="Edit" class="selbtn"> <input type="button" name="delete" value="Delete" class="selbtn"></td>
</tr>
<?php
}
?>
Just keep track whether it is an alternating row with a Boolean. Initialize it to false before your loop, not it for each iteration, then you can set the row style based on its value. Something like:
...
$isAlternatingRow = false;
while($row = mysql_fetch_array($result)) {
echo "
<tr class=\"" . $isAlternatingRow ? "defaultRow" : "alternatingRow" . "\">
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$isAlternatingRow = !($isAlternatingRow);
}
Then just define styles for tr.defaultRow td and tr.alternatingRow td.
$class="even"
while($row = mysql_fetch_array($result))
{
if($class == "even")
{
echo "<tr class='$class'>";
$class="odd"
}
else
{
echo "<tr class='$class'>";
$class="even";
}
...
}
I did it like this:
Remember to add a CSS class called "even", with styling of course.
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i % 2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>
Using predefined color outside the loop.
$rowCount = 0;
$color = array('#ffffff','#f3f3f3');
while($row = mysql_fetch_array($result)){
$i = ($rowCount % 2);
echo "<element bgcolor='".$color["$i"]."'></element>";
$rowCount++;
}

Categories