How can I add different maximum width to my table columns? I'd also like to have an auto line break when I write something into the columns. Here's my code:
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Kategorie</th> <th>Titel</th> <th>Betroffene Seiten</th> <th>Beschreibung</th><th>prioritaet</th><th>Status</th><th>Eingereicht von</th><th>Umsetzung mit Release</th><th>Dev. Kommentar</th><th>Entwickler</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 '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['kategorie'] . '</td>';
echo '<td>' . $row['titel'] . '</td>';
echo '<td>' . $row['betroffen'] . '</td>';
echo '<td>' . $row['beschreibung'] . '</td>';
echo '<td>' . $row['prioritaet'] . '</td>';
echo '<td>' . $row['status'] . '</td>';
echo '<td>' . $row['eingereicht'] . '</td>';
echo '<td>' . $row['umsetzung'] . '</td>';
echo '<td>' . $row['kommentar'] . '</td>';
echo '<td>' . $row['entwickler'] . '</td>';
echo '<td>bearbeiten</td>';
echo '<td>löschen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
you can set a width to your td in pixels or %. setting in one td will set it for the column.
echo '<td style="width:40px">' . $row['id'] . '</td>';
I didn't understood you intenstion by:
"when I write something into the columns."
you can use the \n to get new lines in your php code. just add it to your code where appropriate.
you can see here
you can also use <br /> in your HTML to get the same line break.
You can do as following to break line in column and keep max width,
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Kategorie</th> <th>Titel</th> <th>Betroffene Seiten</th> <th>Beschreibung</th><th>prioritaet</th><th>Status</th><th>Eingereicht von</th><th>Umsetzung mit Release</th><th>Dev. Kommentar</th><th>Entwickler</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 '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['id'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['kategorie'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['titel'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['betroffen'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['beschreibung'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['prioritaet'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['status'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['eingereicht'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['umsetzung'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['kommentar'], 20, "<br />\n") . '</td>';
echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['entwickler'], 20, "<br />\n") . '</td>';
echo '<td>bearbeiten</td>';
echo '<td>löschen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
using strlen function set max width to td and using wordwrap break column value to new line
Related
I have a code where I echo a table:
<?php
// connect to the database
include('core/base.php');
// get results from database
$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM uitslag ORDER BY ID ASC")
or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Nummer</th><th>Naam</th><th>Telefoon</th><th>Binnen</th> <th>Adres</th> <th>Postcode</th> <th>Wijk</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo '<td>' . $row['Telefoon'] . '</td>';
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
echo '<td>Aanpassen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
The row 'Telefoon' either echos 1 or 0. How can I echo a checkbox which is checked when 1 instead of echoing the actual number?
Define input element of type checkbox.Like this.
$telefoon = ($row['Telefoon']==1)?'checked':'';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo "<td><input type='checkbox' $telefoon></td>";
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
<?php
if($row['Telefoon']==1){
echo "<td><input type='checkbox' checked></td>";
}
else {
echo "<td><input type='checkbox' ></td>";
}
?>
<!doctype html>
<?php
?>
<html>
<head>
<title>Midterm Review</title>
</head>
<body>
<h3>Tools Not Currently in Stock</h3>
<?php
$conn = mysqli_connect(This part works );
mysqli_select_db(this part works);
$query = "SELECT * FROM `midterm` WHERE stock='0'";
$result = mysqli_query($conn, $query);
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach ($result as $row) {
$row = mysqli_fetch_array($result);
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
echo '</table>';
};
mysqli_close($conn);
?>
<!--<form method="post" action="midterm_confirmation.php">
<label>Part Number: </label><input type="text" name="partNumber" /><br />
<label>Description: </label><input type="text" name="description" /></br />
<label>Stock: </label><input type="number" name="stock" /><br />
<label>Price: </label><input type="text" name ="price" /></br />
<label>Received Date: </label><input type="text" name="receivedDate" /></br />
<input type="Submit" value="Add to Stock">
</form> -->
</body>
</html>
Basically my end result is that I am getting one table row, instead the two I am getting. Any suggestions? The table I have populates everything but is only running once, instead of posting for each line I have where stock is equal to zero.
See the snippet below. P.S. Don't mix templates and database layer, it's a bad smell...
<?php
// establish connection to $conn variable
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>';
mysqli_close($conn);
?>
The problem is in closing of </table> It should be kept outside the loop.
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
// echo '</table>';// this is wrong.
};
echo '</table>';// this is correct. closing table inside loop is wrong, do it outside the loop.
You can simply use a while loop to do it like below:
<?php
// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>
Or if you want to use a foreach then write an extra function for it:
<?php
function mysql_fetch_all($result) {
$rows = array();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
return $rows;
}
// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach (mysql_fetch_all($result) as $row){
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>
I have a render of row of database
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</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 '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
And in the tex1 is a date, my problem is for a class for tr, when the tex2 is empty and the date of tex1 is older than 30 days, tr have class red, the date of tex1 is inserted manually,how can I do to change the class on the basis of this?
I'm not sure if I understood what you wanted properly but I'll give it a shot. You want to add a class ("red") to the , whenever the condition is met that either the date is older than 30 days old (tex1) or the First Name is empty (tex2).
echo "<table border='1' cellpadding='10'>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</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
// I assume since you're just printing it out, the date is a String
// So first convert it to a timestamp
$date = strtotime($row['tex1']);
// Determine the difference, I'm doing it this way to make it easy for you to understand
$days = 60*60*24*30; // 30 days
if (empty($row['tex2']) || time()-$date > $days) {
echo '<tr>';
} else {
echo '<tr class="red">';
}
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
You should check for the conditions what you have mentioned in your question. And this can be done like this.
if(strtotime($row['tex1']) + 30 * 24 * 60 * 60 < time() && $row['tex2'] =="") {
$class="class='red'";
} else {
$class= "";
}
echo "<tr $class>";// id condition satisfies, class='red' will get echo, else echo will be null.
Try a conditional when writing the element:
echo '<tr'.
( ((strtotime($row['tex1']) < strtotime('30 days ago'))
&& (empty($row['tex2']))) ? ' class="red"' : '').
'>';
how can i do if this row is empty then it will showing another row
suppose name row is blank
echo '<td>' . $row['name'] . '</td>';
so if name row is blank then it will show echo '<td>' . $row['companyname'] . '</td>'; row
please help me how can i fix this
thanks
echo "<tr>";
echo '<td>' . $row['buildingname'] . '</td>';
echo '<td>' . $row['area'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['flatno'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['mobileno'] . '</td>';
echo '<td>' . $row['agreementdates'] . '</td>';
echo '<td>' . $row['agreementdatee'] . '</td>';
echo "</tr>";
Try
if(empty( $row['name'])){
$tbl = '<td>' . $row['companyname'] . '</td>';
}else{
$tbl = '<td>' . $row['name'] . '</td>';
}
echo $tbl;
try this i think this is working.
echo '<td>' . empty($row['name']) ? $row['companyname'] : $row['name'] . '</td>';
try this:
echo "<tr>";
if(!empty($row['buildingname'])) {
echo '<td>' . $row['buildingname'] . '</td>';
}
if(!empty($row['area'])) {
echo '<td>' . $row['area'] . '</td>';
}
if(!empty($row['city'])) {
echo '<td>' . $row['city'] . '</td>';
}
if(!empty($row['flatno'])) {
echo '<td>' . $row['flatno'] . '</td>';
}
if(!empty($row['name'])) {
echo '<td>' . $row['name'] . '</td>';
}
if(!empty($row['mobileno'])) {
echo '<td>' . $row['mobileno'] . '</td>';
}
if(!empty($row['agreementdates'])) {
echo '<td>' . $row['agreementdates'] . '</td>';
}
echo "</tr>";
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>';