I am trying to setup a table column width to only 200px or 10%. However, when I populate the data from the database, the column seems to ignore the column width and expand it as much as it can. Here is my table html.
<table>
<thead>
<tr>
<th><a href='#'>City</a></th>
<th width='200' class='table_width'><a href='#'>name</a></th>
<th><a href='#'>Agent</a></th>
<th>...
//I have tried class and width attribute. both don't work.
</tr>
</thead>
<tbody>
<tr>
//these column data were popluated from database
<td><?= $row->city; ?></td>
<td width='200' class='table_width'><?= $row->_name; ?></td>
//ignore the width and expand the table cell.
<td><?= $row->agent; ?></td>
<td>...
</tr>
You want to use word-wrap:break-word;.
http://jsfiddle.net/RF4F6/1/
HTML
<table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
<th>Col 5</th>
</tr>
</thead>
<tbody>
<tr>
<td>Normal Width</td>
<td>Normal Width</td>
<td class="forcedWidth">hiThisIsAreallyLongStringThatWillHaveToWrapAt200PixelsOrI'llBeUnhappyAndYouWon'tLikeMeWhenI'mUnhappy.</td>
<td>Normal Width</td>
<td>NormalWidth</td>
</tr>
</tbody>
</table>
CSS
table td{
padding:.5em; /* Not essential for this answer, just a little buffer for the jsfiddle */
}
.forcedWidth{
width:200px;
word-wrap:break-word;
display:inline-block;
}
This is the standard behaviour of a table cell.
One way to do this is place a div inside your cells with style="width: 200px; overflow-hidden;"
Using table-layout: fixed for table will force browser to maintain specified dimensions.
Related
Good Day, I want to display the value in my DB in one row. But what is happening now, it is being displayed horizontally
Image Sample
<table class="table table-bordered">
<thead style="font-size:20px;text-align:center;"class="thead-dark">
<tr>
<th class="tblHeader">MODEL</th>
<th class="tblHeader">PERIOD 1</th>
<th class="tblHeader">PERIOD 2</th>
<th class="tblHeader">PERIOD 3</th>
<th class="tblHeader">PERIOD 4</th>
<th class="tblHeader">PERIOD 5</th>
<th class="tblHeader">PERIOD 6</th>
<th class="tblHeader">PERIOD 7</th>
<th class="tblHeader">PERIOD 8</th>
<th class="tblHeader">PERIOD 9</th>
<th class="tblHeader">PERIOD 10</th>
<th class="tblHeader">PERIOD 11</th>
<th class="tblHeader">PERIOD 12</th>
</tr>
</thead>
<?php
$connect = mysqli_connect("localhost", "root", "", "hh_bpm");
$query = "SELECT *
FROM bpm_periods_instance
WHERE Category_Name=1
";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{ ?>
<tbody style="font-size:20px; text-align:center;">
<tr>
<td><?php echo $row["Text_Value"];?></td>
</tr>
<?php } mysqli_close($connect);?>
</tbody>
</table>
I want to display the string value in line with Period 1 - 12`
I'm still trying to learn.
First of all, move your while loop just before the <tr>, for you want to have just one table body. Then it's better to have the same number of <th>s and <td>s. I see you have 12 <th>s, so make 12 <td>s in each <tr> (leave them empty if you want, but include them)
The tags is a row containing headings for your table. For your to Match your column names you must have equal number of as th in your table.
<tr>
<th>heading1</th>
<th>heading2</th>
<th>heading3</th>
</tr>
You can now iterate this for your rows.
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
</tr>
You are showing just a singe TD for each row.
What you are missing is a row loop:
while($row = mysqli_fetch_array($result))
{ ?>
<tbody style="font-size:20px; text-align:center;">
<tr>
<?php
foreach($row as $k=>$v)
echo "<td>$v</td>";
?>
</tr>
<?php } mysqli_close($connect);?>
</tbody>
</table>
Alternatively, you should output TDs for all columns manually:
<tr>
<td><?php echo $row["Column1"];?></td>
<td><?php echo $row["Column2"];?></td>
<td><?php echo $row["Column3"];?></td>
<td><?php echo $row["Column4"];?></td>
<td><?php echo $row["Column5"];?></td>
<td><?php echo $row["Column6"];?></td>
...
</tr>
You need a crosstab query to achieve the results that you want. Since there is very little information I'll just give and outline
SELECT info,
sum( if(MONTH(dt)=1,1,0) as Period_1,
sum( if(MONTH(dt)=2,1,0) as Period_2,
//--- repeat for all 12 months
Your query will return 13 columns (info and Period_1 to Period_12). You'll need to adjust the html to cater for this output.
I have a question how to underline in the table according the column data. Below is example coding to explain what I am facing the problem:
I want to detect if column underline is 1 the first name data will draw the underline, if 0 the first name data no show the underline. Below the sample is hardcode, if real situation, I have too many row to show the data, I cannot 1 by 1 to add text-decoration: underline; in the td. So that, hope someone can guide me how to solve this problem. I am using the php code to make the variable to define the underline.
<!--Below the php code I just write the logic, because I don't know how to write to detect the column underline value-->
<?php
if ( <th>Underline</th> == 1) {
$add_underline = "text-decoration: underline;";
}
if ( <th>Underline</th> == 0) {
$add_underline = "text-decoration: underline;";
}
?>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Underline</th>
</tr>
<tr>
<td style="<?php echo $add_underline;?> ">Jill</td>
<td>Smith</td>
<td>1</td>
</tr>
<tr>
<td style="<?php echo $add_underline;?>">Eve</td>
<td>Jackson</td>
<td>0</td>
</tr>
<tr>
<td style="<?php echo $add_underline;?>">John</td>
<td>Doe</td>
<td>1</td>
</tr>
</table>
My output like below the picture:
My expected result like below the picture, Jill and John can underline:
Why not use javascript to achieve this? No matter what the server sends it will evaluate the condition if 1 is set and then underline accordingly... You would have to use classes to get the appropriate table data tags holding the values, I added class='name' to the names <td> tag and class='underline' tot he underline <td> tag.
// get the values of the elements with a class of 'name'
let names = document.getElementsByClassName('name');
// get the values of the elements with a class of 'underline'
let underline = document.getElementsByClassName('underline');
// loop over elements using for and use the keys to get and set values
// `i` will iterate until it reaches the length of the list of elements with class of underline
for(let i = 0; i < underline.length; i++){
// use the key to get the text content and check if 1 is set use Number to change string to number for strict evaluation
if(Number(underline[i].textContent) === 1){
// set values set to 1 to underline in css style
names[i].style.textDecoration = "underline";
}
}
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Underline</th>
</tr>
<tr>
<td class="name">Jill</td>
<td>Smith</td>
<td class='underline'>1</td>
</tr>
<tr>
<td class="name">Eve</td>
<td>Jackson</td>
<td class='underline'>0</td>
</tr>
<tr>
<td class="name">John</td>
<td>Doe</td>
<td class='underline'>1</td>
</tr>
</table>
Or using the td child values...
let tr = document.querySelectorAll("tr");
last = null;
for(let i = 1; i < tr.length; i++){
if(Number(tr[i].lastElementChild.innerHTML) === 1){
tr[i].firstElementChild.style.textDecoration = "underline";
}
}
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Underline</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>1</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>0</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>1</td>
</tr>
</table>
I have a page full of small html tables, each representing data from a database table. This is a one page print out report, so there are quite a few fields across these html tables, but only about 4 or 5 of them may need to be changed.
When the user chooses a record and the report is displayed, all the tables look great but I'm trying to find a way to edit and save the 4 or 5 that might need editing. I know I can create a form with inputs and turn certain table rows into inputs and then with a submit button I can attach a SQL statement to update those fields based on a primary key in the DB table.
How can I actually get these table rows to be editable inputs within a form? I've wrapped the whole page in a form and I even tried by wrapping just one table in a form as a test but I think the problem is my syntax for the table row.
Here is what I tried so far:
<form>
<table style="width:100%; border:none;
border-collapse:collapse;">
<tr style="border:none;">
<th style="border:none; text-align: left;" >Meter Test</th>
<th style="border:none;">Test 1</th>
<th style="border:none;">Test 2</th>
<th style="border:none;">Test 3</th>
<th style="border:none;">Test 4</th>
<th style="border:none;">Test 5</th>
<th style="border:none;">Test 6</th>
<th style="border:none;">Test 7</th>
<th style="border:none;">Test 8</th>
</tr>
<tr style="border: none;" >
<td style="border:none; text-align: left;">Test Rate GPM: </td>
<td><? echo $row['test1TestRateGPM'];?> </td>
<td><? echo $row['test2TestRateGPM'];?> </td>
<td><? echo $row['test3TestRateGPM'];?> </td>
<td><? echo $row['test4TestRateGPM'];?> </td>
<td><? echo $row['test5TestRateGPM'];?> </td>
<td><? echo $row['test6TestRateGPM'];?> </td>
<td><input type="text" value = "<?php $row['test7TestRateGPM'];?>">
<td><input type="text" value = "<?php $row['test8TestRateGPM'];?>"> </td>
</tr>
</table>
</form>
You can see i have the normal html table syntax but for the field I'm testing, I tried it by creating a line of php and doing it that way, but still no form on the web page.
I think you just need a simple input text field.
Do it like this:
<td><input type="text" value = "<?php echo $row['test1TestRateGPM'];?>"></td>
And make multiple of these.
I created a table and used collapsed visibility to try to hide many columns from being shown. The columns still show, just they show up blank. I want it to show as though those columns don't exist. The reason the are there is there is a search box that searches through the table data. I want it to search those items but not display them.
<table id='example1' class='table table-bordered table-striped'>
<thead>
<tr>
<th>Ticket #</th>
<th>Date</th>
<th>Subject</th>
<th>Status</th>
<th>Close Date</th>
<th>Assigned To</th>
<th>Work Order</th>
<th style='visibility:collapse;'>TID #</th>
<th style='visibility:collapse;'>Modem #</th>
<th style='visibility:collapse;'>MHL #</th>
<th style='visibility:collapse;'>Waybill #</th>
<th style='visibility:collapse;'>TID #</th>
<th style='visibility:collapse;'>ATM Brand</th>
<th style='visibility:collapse;'>ATM Model</th>
<th style='visibility:collapse;'>EPP Serial</th>
<th style='visibility:collapse;'>Router #</th>
</tr>
</thead>
<tbody>"; // output data of each row
while($row = $result->fetch_assoc()) { $href='"#"'; echo "
<tr>
<td><a href='tickets.php?id=".$row[' id ']."'>".$row['id']."</a>
</td>
<td>".$row['timecreated']."</td>
<td>".$row['subject']."</td>
<td>".$row['status']."</td>
<td>".$row['closedate']."</td>
<td>".$row['assignedtoname']."</td>
<td>".$row['workorder']."</td>
<td style='visibility:collapse;'></td>
<td style='visibility:collapse;'></td>
<td style='visibility:collapse;'></td>
<td style='visibility:collapse;'></td>
<td style='visibility:collapse;'></td>
<td style='visibility:collapse;'></td>
<td style='visibility:collapse;'></td>
<td style='visibility:collapse;'></td>
<td style='visibility:collapse;'></td>
</tr>"; } } else { echo "0 results"; } echo "</tbody>
<tfoot>
<tr>
<th>Ticket #</th>
<th>Date</th>
<th>Subject</th>
<th>Status</th>
<th>Close Date</th>
<th>Assigned To</th>
<th>Work Order</th>
<th style='visibility:collapse;'>TID #</th>
<th style='visibility:collapse;'>Modem #</th>
<th style='visibility:collapse;'>MHL #</th>
<th style='visibility:collapse;'>Waybill #</th>
<th style='visibility:collapse;'>TID #</th>
<th style='visibility:collapse;'>ATM Brand</th>
<th style='visibility:collapse;'>ATM Model</th>
<th style='visibility:collapse;'>EPP Serial</th>
<th style='visibility:collapse;'>Router #</th>
</tr>
</tfoot>
</table>
Change "visibility:collapse;" to "display: none;"
As per https://developer.mozilla.org/en-US/docs/Web/CSS/visibility, it seems as if you'll have to use display none, instead.
collapse For table rows, columns, column groups, and row groups the
row(s) or column(s) are hidden and the space they would have occupied
is removed (as if display: none were applied to the column/row of the
table). However, the size of other rows and columns is still
calculated as though the cells in the collapsed row(s) or column(s)
are present. This was designed for fast removal of a row/column from a
table without having to recalculate widths and heights for every
portion of the table. For XUL elements, the computed size of the
element is always zero, regardless of other styles that would normally
affect the size, although margins still take effect. For other
elements, collapse is treated the same as hidden.
I have a requirement to output a table using Smarty and then have the user select a row to take them to another page.
I can display the table fine:
{html_table cols="Job Title,Salary,Sector,Location" loop=$results}
which gives:
<table border="1">
<thead>
<tr>
<th>Job Title</th>
<th>Salary</th>
<th>Sector</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dog walker</td>
<td>20000</td>
<td>None</td>
<td>London</td>
</tr>
<tr>
<td>F1 Driver</td>
<td>10000000</td>
<td>Financial Services</td>
<td>Scotland</td>
</tr>
</tbody>
</table>
but I am not sure if it is possible to add a hyperlink as an additional column to the table that links to a page using a hidden id.
So I would want something link this:
<table border="1">
<thead>
<tr>
<th>Job Title</th>
<th>Salary</th>
<th>Sector</th>
<th>Location</th>
<th>Apply</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dog walker</td>
<td>20000</td>
<td>None</td>
<td>London</td>
<td>Apply</td>
</tr>
<tr>
<td>F1 Driver</td>
<td>10000000</td>
<td>Financial Services</td>
<td>Scotland</td>
<td>Apply</td>
</tr>
</tbody>
</table>
Is that possible?
Yes, but you need to modify the $results array before you pass it to Smarty so that the 4th element of each row contains the link as a string. There's no way to have {html_table} generate the link for you.