Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i have a data like
<table>
<tr>
<th>title 1</th>
<td>para1</td>
</tr>
</table>
<table>
<tr>
<th>title 2</th>
<td>para1</td>
<td>para2</td>
<td>para3</td>
</tr>
</table>
<table>
<tr>
<th>title 3</th>
<td>para1</td>
<td>para2</td>
</tr>
</table>
now how can i take this data and make into an array..it will be really helpful if i can get a solution for this.
In my problem i have a table as shown above and want to store the data in a nested/ multidimensional array. All the solution above doesnot answer my question
thanks in advance
I think I know what you are looking for... It's actually pretty simple.
You want to iterate through the <table> tags first .. And subsequently iterate the children tr - tds.
I'd surround the tables with a surrounding div to make grabbing them much easier.
Then I'd use jQuery because the library makes it easy to choose children etc etc. Then for Storage into the database .. I'd "Json-ify" the array(s)
IE
$(document).ready(function() {
myHTML = $('#myDiv').html();
});
var tableNumber = $('#myDiv').children('table').length;
var items = [];
for (i = 0; i < tableNumber; i++) {
var title = $($("table tr th")[i]).html();
var paras = [];
$($("table tr")[i]).find('td').each(function() {
paras.push($(this).html());
});
items.push(title, paras);
}
var outPut = JSON.stringify(items);
$('#jsonOut').html(outPut);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv">
<table>
<tr>
<th>title 1</th>
<td>para 1-1</td>
</tr>
</table>
<table>
<tr>
<th>title 2</th>
<td>para 2-1</td>
<td>para 2-2</td>
<td>para 2-3</td>
</tr>
</table>
<table>
<tr>
<th>title 3</th>
<td>para 3-1</td>
<td>para 3-2</td>
</tr>
</table>
</div>
<br>
<pre>
<div id="jsonOut">
</div>
</pre>
You can also view the FIDDLE
Hope this helps.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So this is my code that I am trying to add the CSS Styling to its contained within a HereDoc
<div class = "row>
<div class="column">
<div style="WebAppS15/css/TeamManagment.css;">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Squad Role</th>
</tr>
<tr>
<td>Jurgen</td>
<td>Klopp</td>
<td>Manager</td>
</tr>
<tr>
<td>Zelko</td>
<td>Buljvak</td>
<td>Assistant Manager</td>
</tr>
<tr>
<td>Gary</td>
<td>McAllister</td>
<td>Head Coach</td>
</tr>
<tr>
<td>Peter</td>
<td>Krawietz</td>
<td>Assistant Coach</td>
</tr>
<tr>
<td>Tom</td>
<td>Werner</td>
<td>Chairman</td>
</tr>
</table>
</div>
</div>
I do not think I have the location wrong but I am not seeing any change whenever I adjust the pixel width in the TeamManagment css file on this file that I am attempting to link it to.
That is not how it works. You need to include the css file with:
<link rel="stylesheet" href="WebAppS15/css/TeamManagment.css">
And set a class to the div like this:
<div class="class-name">
So, in your WebAppS15/css/TeamManagment.css file, you set the custom css for that class with:
.class-name {
// Properties
}
Another option is to configure the CSS inside the html file like this:
<style>
.class-name {
// Properties
}
</style>
<div class="row">
<div class="column">
<div class="class-name">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Squad Role</th>
</tr>
<tr>
<td>Jurgen</td>
<td>Klopp</td>
<td>Manager</td>
</tr>
<tr>
<td>Zelko</td>
<td>Buljvak</td>
<td>Assistant Manager</td>
</tr>
<tr>
<td>Gary</td>
<td>McAllister</td>
<td>Head Coach</td>
</tr>
<tr>
<td>Peter</td>
<td>Krawietz</td>
<td>Assistant Coach</td>
</tr>
<tr>
<td>Tom</td>
<td>Werner</td>
<td>Chairman</td>
</tr>
</table>
</div>
</div>
</div>
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>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i have a data like
<table>
<tr>
<th>title 1</th>
<td>para1</td>
</tr>
</table>
<table>
<tr>
<th>title 2</th>
<td>para1</td>
<td>para2</td>
<td>para3</td>
</tr>
</table>
<table>
<tr>
<th>title 3</th>
<td>para1</td>
<td>para2</td>
</tr>
</table>
now how can i take this data and make into an array..it will be really helpful if i can get a solution for this.
In my problem i have a table as shown above and want to store the data in a nested/ multidimensional array. All the solution above doesnot answer my question
thanks in advance
I think I know what you are looking for... It's actually pretty simple.
You want to iterate through the <table> tags first .. And subsequently iterate the children tr - tds.
I'd surround the tables with a surrounding div to make grabbing them much easier.
Then I'd use jQuery because the library makes it easy to choose children etc etc. Then for Storage into the database .. I'd "Json-ify" the array(s)
IE
$(document).ready(function() {
myHTML = $('#myDiv').html();
});
var tableNumber = $('#myDiv').children('table').length;
var items = [];
for (i = 0; i < tableNumber; i++) {
var title = $($("table tr th")[i]).html();
var paras = [];
$($("table tr")[i]).find('td').each(function() {
paras.push($(this).html());
});
items.push(title, paras);
}
var outPut = JSON.stringify(items);
$('#jsonOut').html(outPut);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv">
<table>
<tr>
<th>title 1</th>
<td>para 1-1</td>
</tr>
</table>
<table>
<tr>
<th>title 2</th>
<td>para 2-1</td>
<td>para 2-2</td>
<td>para 2-3</td>
</tr>
</table>
<table>
<tr>
<th>title 3</th>
<td>para 3-1</td>
<td>para 3-2</td>
</tr>
</table>
</div>
<br>
<pre>
<div id="jsonOut">
</div>
</pre>
You can also view the FIDDLE
Hope this helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can anyone help me how to display table inside PHP if statement.
Here's my code:
if( empty($errors))
{
ACTIVITIES \n
echo ' <table>
<thead>
<tr>
<th>Activity</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chicken Feeding</td>
<td>$price_chicken</td>
<td>$num_chicken</td>
<td>$total_chicken</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Fish Feeding</td>
<td>$price_fish</td>
<td>$num_fish</td>
<td>$total_fish</td>
</tr>
</tbody>
.................
</table> ';
}
try this
<?php if( empty($errors)): ?>
ACTIVITIES \n
<table>
<thead>
<tr>
<th>Activity</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chicken Feeding</td>
<td><?=$price_chicken;?></td>
<td><?=$num_chicken;?></td>
<td><?=$total_chicken;?></td>
</tr>
</tbody>
<tbody>
<tr>
<td>Fish Feeding</td>
<td><?=$price_fish;?></td>
<td><?=$num_fish;?></td>
<td><?=$total_fish;?></td>
</tr>
</tbody>
.................
</table>
<?php endif; ?>
You have the table code wrapped in single quotes, but there are PHP variables inside. PHP will not translate those variables unless you wrap the table in double quotes.
If this doesn't help, give us an idea of what the output looks like.
Try using a 2-dimensional array and looping through it to create a proper HTML table. This way, there's no hard-coding, so you can make as many rows/columns as you want. Here's the code, just change the array:
<table>
<tbody>
<?php
$tableArray = [["Chicken Feeding", $price_chicken, $num_chicken],
["Fish Feeding", $price_fish, $num_fish]];
foreach ($tableArray as $tableRow) {
echo "<tr>";
foreach ($tableRow as $tableCell) {
echo "<td>$tableCell</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
I have a HTML thus like:
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
<tr>
<td>Joe Bloggs</td>
<td>joe#bloggs.com</td>
<td>40</td>
</tr>
<tr>
<td>John Doe</td>
<td>john#doe.com</td>
<td>40</td>
</tr>
</table>
Is there a way using xPath to get the first 2 columns, i.e the Name and Email fields?
I can get the table data using $data = $xpath->query( '//table'); just unsure how to get only the first 2 columns.
Many thanks
Get the first two td's:
//table/tr/td[position() <= 2]