php table text align center - php

I want to align text in center under table but i dnt know how to do this
Here is my code
echo "<td>" . $row['Firstname'] ." ". $row['Lastname'] ."</td>";
echo "<td>" . $row['Marks'] . "</td>";
echo "<td>" . $row['percent'] . "</td>";
echo "<td>" . $row['Status'] . "</td>";
Kindly help me how to do this.
i tried this but not working
echo '<td tyle="text-align:center;">' . $row['enroll'] . '</td>';

i think you did typo there.replace tyle with style
echo '<td style="text-align:center;">' . $row['enroll'] . '</td>';
or you can also try
echo '<td align="center">' . $row['enroll'] . '</td>';

First, include this code to see the table borders <table border="1">
Next, include this before your </header> tag if you got everything in an HTML Doctype:
<style type="text/css">
table td {
text-align: center;
}
</style>
And your done.

In CSS
.Classname td{
text-align: center;
}
In Inline
echo '<td style="text-align:center;">' . $row['enroll'] . '</td>';
Or
echo '<td align="center">' . $row['enroll'] . '</td>';

Use CSS instead:
.tableClass td{
text-align: center;
}
Inline styles are annoying to try and maintain, especially if they're output using PHP.

You can use : align attribute for td :
echo '<td align="center">' . $row['enroll'] . '</td>';
Cheers

Use css or inside code write as:
echo '<td style='.'"'.'text-align:center'.';">' . $row['enroll'] . '</td>';

Your code didn't work because there is a typing error - missing s in style
echo '<td tyle="text-align:center;">' . $row['enroll'] . '</td>';
should be
echo '<td style="text-align:center;">' . $row['enroll'] . '</td>';

php table text align center
echo " < td align='center'>" . $row['enroll'] . "< /td>";

Related

How to send a variable value with HREF in PHP

i have one table in locallhost ,In my table there is an item id,Now I want to send the id Related row to another page by clicking on EDIT . Thankful
The part of the program I think is difficult to write below
....
....
$response["travel"]=array();
while ($row = mysql_fetch_array($sql)) {
// Print out the contents of the entry
echo '<tr>';
echo '<td class="text-center">' . $i . '</td>';
echo '<td class="text-center">' . $row['companyname'] . '</td>';
echo '<td class="text-center">' . $row['cod'] . '</td>';
echo '<td class="text-center">' . $row['bigan'] . '</td>';
echo '<td class="text-center">' . $row['stop'] . '</td>';
echo '<td class="text-center">' . $row['date'] . '</td>';
echo '<td class="text-center">' . $row['time'] . '</td>';
echo '<td class="text-center">' . $row['price'] . '</td>';
echo '<td class="text-center">' .'EDIT' .'</td>';
$i++;
.......
.....
You don't use <?php inside strings, you use that when you've gone back into HTML mode. You should just concatenate the variable, like you do everywhere else
echo '<td class="text-center">' .'ویرایش' .'</td>';

How to place database results in ScrollBox

I am trying to place my database results into a ScrollBox but im having a difficult time,
I was checking online and it was saying that i need to use the scrollbox:auto function.
But i tried add it too the CSS regarding the table for which the database results go in
but it did not work and just displayed the results all on the page.
Any guidance would be appreciated
CSS
.results
{
bottom: -10px;
position: relative;
top: 90px;
scrollbox: auto;
}
Database Results
echo "<table class='results' border='1'>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr >";
echo "<td>" . '<img style=height:45px; width:45px src="' . $row['Image'] . '"/>' . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . "£" . $row['Cost'] . "<br>" . '<a href='.$row['Link'].'>' . '</a>' . "</td>";
echo "</tr>";
}
use overflow: auto; Or overflow:scroll instead of scrollbox: auto;
make sure to give the scrolling-div a fixed height.

HTML through PHP not taking a CSS rule

Is there any reason that the following code and syle wouldn't work? The only problem is that I can't get the text to change color, it stays black, which would be fine but the background color is black. I know it's there because I can highlight the text with the cursor and it's there in the table. Here's my code fragment.
This is from the index.php page.
while($row=mysqli_fetch_array($result))
{
echo "<tr id= "Data'" class='"Data'">";
echo "<td id= " cell ">" . $row['Name'] . "</td>";
echo "<td id= " cell ">" . $row['Subject'] . "</td>";
echo "<td id= " cell ">" . $row['Date'] . "</td>";
echo "<td id= " cell ">" . $row['Source'] . "</td>";
echo "<td id= " cell ">" . $row['Comment'] . "</td>";
echo "</tr>";
}
here's the css rule that SHOULD be applied.....but isn't.
#Cell{
border-style: solid;
border-color: black;
border-width: 1px;
color: white;
<td id= " cell "> is not equivalent to <td id="cell"> because HTML does not recursively evaluate itself. So the former tag is invalid gobbledegook without an id. You also cannot give the same id to more than one element.
Why your are using " cell "
Here is correct code
while($row=mysqli_fetch_array($result))
{
echo "<tr id=\"Data\" class=\"Data\">";
echo "<td id=\"cell\">" . $row['Name'] . "</td>";
echo "<td id=\"cell\">" . $row['Subject'] . "</td>";
echo "<td id=\"cell\">" . $row['Date'] . "</td>";
echo "<td id=\"cell\">" . $row['Source'] . "</td>";
echo "<td id=\"cell\">" . $row['Comment'] . "</td>";
echo "</tr>";
}

Apply Table Style in Dynamic Table Creation with PHP

I am having trouble applying CSS styles to my dynamic table. I am connecting to a database and I know the data is populated correctly and displaying. My issue is that there are 900 records and I want to limit the table size, utilizing a scrollbar. I have read elseware that the proper CSS style nodes to accomplish this are: How to specify table's height such that a vertical scroll bar appears?
overflow: auto;
height: 700px;
display: block;
overflow-y: scroll;
At first I attempted this with inline styling (a no-no.. I realize), but it didn't work. I have read about adding a 'class' to the table and/or individual rows, which would then be reflected in my CSS style sheet, but I can't seem to figure out how to accomplish this. I get syntax errors when I add 'span' or 'class' tag designators to the PHP (I imagine from utilizing 'ECHO' - which both require double quotes).
Good example of what I'm trying to accomplish: http://www.timrivera.com/tests/csstables.html#markupIE
The PHP code snippet below has good syntax, but I don't know where to add the class or span designators appropriately. One thing to note - I need to have different styles for different tables, so changing the global 'table' CSS isn't going to work.
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
I think the best way would be to wrap a div around the table and give the div a height.
http://phpfiddle.org/main/code/i7h-9b1
<style type="text/css">
#scroll {
height: 100px; /* Max height of table */
overflow-y: scroll;
width: 340px;
}
</style>
<div id="scroll">
table
</div>
Using your code:
echo '
<style type="text/css">
#scroll {
height: 100px; /* Max height of table */
overflow-y: scroll;
width: 340px;
}
</style>';
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<div id='scroll'><table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table></div>";
Edit your PHP code to...
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<div class=\"table-container\">";
echo "<table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
Then just style using
div.table-container table {}
Using Calum's style format you could do this:
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<style>#size{height:100px;width:340px;overflow-y:scroll;}</style>";
echo "<table id='size' border='1'>
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
I tested it and works fine. And for the different table styles you could:
<style>
#table1
{
style code here
}
#table2
{
style code here
}
</style>
and so on... then you could apply them to the tables:
<table id="table1">
...
</table>
<table id="table2">
...
</table>

PHP code with alternating row color

Hello Im new in programing. I want to create a table using the alternate row color. But dont know how to do it. Here is my code. Please help me!
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['a.ServiceID'] . '</td>';
echo '<td>' . $row['a.Title'] . '</td>';
echo '<td>' . $row['a.Description'] . '</td>';
echo '<td>' . $row['a.Notes'] . '</td>';
echo '<td>' . $row['a.SubmitBy'] . '</td>';
echo '<td>' . $row['a.AssignedEmp'] . '</td>';
echo '<td>' . $row['c.GroupName'] . '</td>';
echo '<td>' . $row['d.NameCategory'] . '</td>';
echo '<td>' . $row['e.TipoStatus'] . '</td>';
echo '<td>' . $row['f.TiposUrgencia'] . '</td>';
echo '<td>' . $row['g.CustomerName'] . '</td>';
echo '<td>' . $row['a.DayCreation'] . '</td>';
echo '<td>' . $row['a.ModifyBy'] . '</td>';
echo '<td>' . $row['a.ModifyTime'] . '</td>';
echo '</tr>';
}
mysqli_free_result($result);
echo '</table>';
$rowColors = Array('#FF0000','#00FF00'); $nRow = 0;
while ($row = mysqli_fetch_assoc($result)){
echo '<tr style="background-color:'.$rowColors[$nRow++ % count($rowColors)].';">';
// ....
echo '</tr>';
}
Or this could be edited to apply classes. Just place the class names in $rowColors, and change the echo to <tr class="'.$rowColors[...].'"> instead.
Working example can be found here.
$c = false;
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr style="background:',(($c=!$c)? '#eee' : '#ddd' ),'">';
// ...
}
Or with CSS 3:
tr:nth-child(odd){ background:#eee; }
tr:nth-child(even){ background:#ddd; }
$rowColors = Array('#FFFFFF','#FF0000'); $i= 0;
while ($row = mysqli_fetch_assoc($result))
{
echo '<tr style="background-color:'.$rowColors[$i++ % count($rowColors)].';">';
echo '<td>' . $row['a.ServiceID'] . '</td>';
echo '<td>' . $row['a.Title'] . '</td>';
echo '<td>' . $row['a.Description'] . '</td>';
echo '<td>' . $row['a.Notes'] . '</td>';
echo '<td>' . $row['a.SubmitBy'] . '</td>';
echo '<td>' . $row['a.AssignedEmp'] . '</td>';
echo '<td>' . $row['c.GroupName'] . '</td>';
echo '<td>' . $row['d.NameCategory'] . '</td>';
echo '<td>' . $row['e.TipoStatus'] . '</td>';
echo '<td>' . $row['f.TiposUrgencia'] . '</td>';
echo '<td>' . $row['g.CustomerName'] . '</td>';
echo '<td>' . $row['a.DayCreation'] . '</td>';
echo '<td>' . $row['a.ModifyBy'] . '</td>';
echo '<td>' . $row['a.ModifyTime'] . '</td>';
echo '</tr>';
}
mysqli_free_result($result);
echo '</table>';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/view.css">
<title>ShowList</title>
</head>
<body>
<table width="100%" height="830" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="3" align="left">
<?php include 'common/header.html'; ?>
</td>
</tr>
<tr>
<td colspan="3">
<?php include 'sepperatemunu.php'; ?>
</tr>
<tr height="720" width="1240" align="center" valign="middle">
<td>
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('Could not Connect'.mysql_error());
}
mysql_select_db("USER", $con);
$color="1";
$row_count = 0;
$sql=mysql_query("select * from registration");
echo "<table border='1' width='100%' height='400' cellpadding='0' cellspacing='0' >
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>DOB</th>
<th>Gender</th>
<th>MobileNo</th>
<th>Address</th>
<th>country</th>
<th>Modify</th>
</tr>";
?>
<?php
while($row=mysql_fetch_array($sql) )
{
if($color==1)
{
echo "<tr bgcolor='#FFFFFF'>";
echo "<td align='center'>" . $row['ID'] . "</td>";
echo "<td align='left'>" . $row['Name'] . "</td>";
echo "<td align='center'>" . $row['Age'] . "</td>";
echo "<td align='center'>" . $row['DOB'] . "</td>";
echo "<td align='center'>" . $row['Gender'] . "</td>";
echo "<td align='center'>" . $row['MobileNo'] . "</td>";
echo "<td align='left'>" . $row['Address'] . "</td>";
echo "<td align='center'>" . $row['Country'] . "</td>";
echo "<td align='center'>" ?>edit <?php "</td>";
echo "</tr>";
$color="2";
}
else
{
echo "<tr bgcolor='#808080'>";
echo "<td align='center'>" . $row['ID'] . "</td>";
echo "<td align='left'>" . $row['Name'] . "</td>";
echo "<td align='center'>" . $row['Age'] . "</td>";
echo "<td align='center'>" . $row['DOB'] . "</td>";
echo "<td align='center'>" . $row['Gender'] . "</td>";
echo "<td align='center'>" . $row['MobileNo'] . "</td>";
echo "<td align='left'>" . $row['Address'] . "</td>";
echo "<td align='center'>" . $row['Country'] . "</td>";
echo "<td align='center'>" ?>edit <?php "</td>";
echo "</tr>";
$color="1";
}
}
echo "</table>";
mysql_close($con);
?>
</td>
</tr>
<tr>
<td colspan="3" align="left">
<?php include 'common/footer.html'; ?>
</td>
</tr>
</table>
</body>
</html>
I think you want something like
$num = 0;
while ($row = mysqli_fetch_assoc($result)) {
$color= ($num % 2 == 0) ? 'color1' : 'color2';
$num++;
echo '<tr style="background-color:'.$color.';">';
echo '<td>' . $row['a.ServiceID'] . '</td>';
echo '<td>' . $row['a.Title'] . '</td>';
echo '<td>' . $row['a.Description'] . '</td>';
echo '<td>' . $row['a.Notes'] . '</td>';
echo '<td>' . $row['a.SubmitBy'] . '</td>';
echo '<td>' . $row['a.AssignedEmp'] . '</td>';
echo '<td>' . $row['c.GroupName'] . '</td>';
echo '<td>' . $row['d.NameCategory'] . '</td>';
echo '<td>' . $row['e.TipoStatus'] . '</td>';
echo '<td>' . $row['f.TiposUrgencia'] . '</td>';
echo '<td>' . $row['g.CustomerName'] . '</td>';
echo '<td>' . $row['a.DayCreation'] . '</td>';
echo '<td>' . $row['a.ModifyBy'] . '</td>';
echo '<td>' . $row['a.ModifyTime'] . '</td>';
echo '</tr>';
}
mysqli_free_result($result);
echo '</table>';
Use some binary variable to store the row color state in:
$colored = false;
while($row = mysqli_fetch_assoc($result)) {
// depending on the state of colored, choose color:
if($colored)
echo '<tr style=\"background-color:lightgray;\">';
else
echo '<tr style=\"background-color:white;\">'; // or '<tr>';
// change the state of $colored:
$colored = !$colored;
echo '<td>' . ...
...
echo '</tr>';
}
You can place the style information in your css (e.g. in two classes "backgroundbright" and "backgroundlow") and add these definitions to your trs:
// depending on the state of colored, choose color:
if($colored)
echo '<tr class=\"backgroundlow\">';
else
echo '<tr class=\"backgroundbright\">'; // or '<tr>';

Categories