Check if table cell has content and mark cell with symbol ◤ - php

Is there a way to check if a table cell has a specific content and to mark that cell with the symbol " ◤ " in the top left corner of that cell?
Thanks

use this find and rerplace function:
function ReplaceCellContent(find, replace)
{
$("#table tr td:contains('" + find + "')").html(replace);
}

You probably don't want to replace the content, just mark interesting cell with the "◤".
You can use CSS :before to do that
<table>
<tr>
<td>lorem</td>
<td>lorem</td>
</tr>
<tr>
<td>ipsum</td>
<td>lorem</td>
</tr>
</table>
JS:
$("td:contains('ipsum')").addClass('found');
Working example:
http://jsfiddle.net/3NWQD/1/

Example JSFiddle answer: http://jsfiddle.net/ATV4G/1/
Javascript:
function markCell(containingText, replaceWith) { //making our function
$("td:contains('"+containingText+"')").prepend("<span class='abs'>"+replaceWith+" </span>");
}
markCell("first", "◤"); //calling the function
CSS:
.abs {
position: absolute;
}
HTML:
<table>
<tr>
<td>first</td>
<td>second</td>
</tr>
</table>
These are the minimum requirements. If you want to beautify the output, you can write further CSS to achieve requirements. (Please check JSFiddle example)
Hope this helps you :)

try this
HTML
<table border="1" id="table">
<tr><th>No</th><th>Player Name</th></tr>
<tr><td>1</td><td>Sachin</td></tr>
<tr><td>2</td><td>Dhoni</td></tr>
<tr><td>3</td><td>Raina</td></tr>
<tr><td>4</td><td>Yuvi</td></tr>
<tr><td>5</td><td>Sachin</td></tr>
</table>
jQuery
function MarkCell(PlayerName)
{
$("td").each(function(){
//alert($(this).text());
if($(this).text() == PlayerName)
{
$(this).html('◤' + PlayerName);
}
});
}
MarkCell('Sachin');
live fiddle here

Here is a PHP solution.
Let's say you have a MySQL database you're connected to and the table has columns 'state_or_province', 'city', and 'country'. If you want to mark all cells with the state or province value of "GA", you would use something like this:
echo "<table>
<tr>
<th>State</th>
<th>City</th>
<th>Country</th>
</tr>";
while($row = mysql_fetch_array($myresults)){
echo "<tr>";
if($row["state_or_province"] == "GA"){
echo "<td class = 'red'>".$row["state_or_province"]."</td>";
}
else{
echo "<td>".$row["state_or_province"]."</td>";
}
echo "<td>".$row["city"]."</td>
<td>".$row["country"]."</td>
</tr>";
}
echo "</table>";
In your CSS style section, include the following
.red{
background-color: red;
}
In this example, all cells that have the value 'GA' under the 'State' column of our rendered table would appear in a red cell. Just edit the above CSS if you want to have the "◤" symbol appear on the top left hand corner instead
UPDATE
Change
if($row["state_or_province"] == "GA"){
echo "<td class = 'red'>".$row["state_or_province"]."</td>";
}
to
if($row["state_or_province"] == "GA"){
echo "<td class = 'red'>&#9700".$row["state_or_province"]."</td>";
}
and you'll see the triangles inside the text boxes

Related

PHP DOM Parser Find First Child of Div With Specific Class and Search Each Elements

As the title says Im trying to find the first child of an element with a specific class here is what I would like to do:
Update: I added the format. Also I figured how to get to the tbody. But at the end of the day what I am trying to do is check each element within tbody if they each equal to "--" or not. How would I achieve this then? Would I convert the DOM Object to a string? Whats an efficient way to do this.
Update 2: I found an elegant solution which I updated to my code. Still the question lingers: Whats the best way to check if there are integers? Do I just convert each element to a string and then do a strpos condition?
Update 3: Nevermind I figured it out.Included below is the solution. I had to go one step further into the table body to select the table rows and loop through them instead.
foreach($html->find('div[class=compareSkillsOuter clear] section[class=playerStats] div[class=tiledBlueFrame] div[class=tableWrap] table[class=headerBgLeft] tbody tr') as $element){
if($position = strrpos($element, '--');
if($position !== false){
echo "String found";
}
else{
echo "String not found";
}
}
Format:
<div class="compareSkillsOuter clear">
<section class="playerStats">
<div class="tiledBlueFrame">
<div class="tl corner"></div>
<div class="tr corner"></div>
<div class="bl corner"></div>
<div class="br corner"></div>
<div class="tableWrap">
<table class="headerBgLeft">
<thead>
<tr>
<th class="alignleft">Rank</th>
<th class="alignleft">Total XP</th>
<th class="alignleft">Level</th>
</tr>
</thead>
<tbody>
<tr >
<td class="alignleft">1,700,012</td>
<td class="alignleft">3,290</td>
<td class="playerWinLeft alignleft"><div class="relative">55</div></td>
</tr>
<tr class=oddRow>
<td class="alignleft">--</td>
<td class="alignleft">--</td>
<td class="alignleft">--</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</div>
Best Solution:
$data = $html->find("table[class=headerBgLeft] tbody",0);
$data1 = $data->find("tr td a");
$statistics = Array();
//echo $data1[1]->plaintext;
foreach($data1 as $tr)
{
//store each
echo "$tr 1<br>";
}
If You want to find tr body than just use table and tbody element. find always return an array and your if statement is with semicolon.
$data= $html->find("table[class=headerBgLeft] tbody");
$data1=$data->find("td tr");
foreach($data1 as $ed)
{
if($ed->innertext=="--")
echo "String is found";
else
echo "String is not found";
}
If you only need to find that two dashes within tbody and their position is fixed means they always in second tr. You can use
$data1=$data->find("td tr",1);
If it is not your problem. Or it does not work please comment. I have not tested it.

how to style php echo table

I have the following code and I would like to style it. Are there any ways to do this?
Specifically I want to center the Columns headings and the text within each cell.
echo "Variable Profile";
echo "<table>";
echo "<th>"."STATE"."</th>";
echo "<th>"."$column_name"."</th>";
foreach($lotsofasians as $lotsofasian){
echo "<tr>";
echo "<td>".$lotsofasian->state."</td>";
echo "<td>".$lotsofasian->$column_selected."</td>";
echo "</tr>";
}
echo "</table>";
I have tried putting something like align= 'center' in the tag but I cant get it to work.
With regard to styling HTML, there's nothing special about the fact that PHP is outputting it. You can still give your elements classes, IDs, inline styling or whatever - it's just that if PHP is involved you'll have to reference these in the echo output statements.
Just change the echo statement to include classes as required, e.g.
echo "<table class='some_class'>";
Try using css rather than using inline styles
css
table th,table td{
text-align:center;
}
See demo here
Your php is going to output something like this:
<table>
<th>state value</th>
<th>column value</th>
<tr>
<td>value</td>
<td>value</td>
</tr>
</table>
I would sneak a <tr> in there for your <th> elements as well:
<table>
<tr>
<th></th>
</tr>
Then, create some css rules:
table th, table td { padding: 5px; text-align: center; }
Use css-classes or inline styles.
Why you quote Variables? Your HTML is not valid.
simplest way:
echo 'Variable Profile';
printf('<table>
<thead>
<tr>
<th>%s</th>
<th>%s</th>
</tr>
</thead>
<tbody>', 'State', $column_name);
foreach($lotsofasians AS $index => $lotsofasian) {
$style = array();
/* Example 1: center text */
$style[] = 'text-align: center;';
/* Example 2: Set Background each second output */
if($index % 2 == 0) {
$style[] = 'background: #DDDDDD;';
}
printf('<tr style="%">
<td>%s</td>
<td>%s</td>
</tr>', implode('', $style), $lotsofasian->state, $lotsofasian->{$column_selected});
}
echo '</tbody>
</table>';

How to remove a row from a table

I have an html table, and on each row, i would like to add a delete button.
When the user clicks on it, I would like to do two things:
1) remove the row from the table.
2) extract data from the row that was removed - a particular cell, and then make an ajax call using this value to a function that will remove the record from the database. The function returns a fresh list of items, which i will then use to redisplay the same table.
I found the following post from someone else on stackoverflow:
How to remove current row from table in jQuery?
So I guess that addresses point number one. But I don't know how to modify the code in the selected answer of the post to allow for grabbing the id value in the row, and then making an ajax call.
<table class="table table-bordered table-striped" id="databaserecords">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Status</th>
<th>Voice</th>
<th>Jumbo</th>
<th>Mode</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php foreach ($dblist as $record): ?>
<tr>
<td class ='recordId'><?php echo $record['Id'] ?></td>
<td class ='recordName'><?php echo $record['Name'] ?></td>
<td><?php echo $record['Status'] ?></td>
<td><?php echo $record['Voice'] ?></td>
<td><?php echo $record['Jumbo'] ?></td>
<td class='recordmode'><?php echo $record['Mode'] ?></td>
<td><button id="deleteRecord">Delete Record</button></td>
</tr>
<?php endforeach ?>
<script>
$('#deleteRecord').live('click', function() {
//get a count of all records. only allowed to delete if you have more than one record.
var recCount = $('#databaserecords tbody tr').length;
if (recCount > 1)
{
$thevalueIwanttoSave = $(this).closest('recordId').val();
alert($thevalueIwanttoSave);
}
});
</script>
Right now, the alert always says that my variable is undefined.
Can you point me in the right direction? I do know how to code an ajax call... I think I just need help with grabbing the value from the table.
Thanks.
You selector is not correct.
$thevalueIwanttoSave = $(this).parent().siblings('.recordId').text();
closest selects the closest parent of the element(recordId is not parent/grandparent or.. of your button element) and you have also missed a . for class selector. val is used for getting/setting values of form elements, for td elements you should use text or html method. Also note that IDs must be unique(you can use classes instead) and live method is deprecated, you can use on method.
$('#databaserecords').on('click', '.deleteRecord', function() {
var recCount = $('#databaserecords tbody tr').length;
if (recCount > 1) {
var text = $(this).parent().siblings('.recordId').text();
alert(text);
// $(this).closest('tr').remove()
}
});​
siblings()
parent()
closest()
text()
on()
$thevalueIwanttoSave = $(this).parent('tr').find('.recordId').text();
it should be .recordId, not recordId
$thevalueIwanttoSave = $(this).closest('.recordId').val();
alert($thevalueIwanttoSave);
Add the ID in an attribute on your delete button and you can just get it via jQuery.
in your php loop:
<button class="deleteRecord" recordID="<?php echo $record['Id'] ?>">Delete Record</button>
in JQuery on
$(".deleteRecord").click(function() {
var id = $(this).attr("recordID");
...
});
You need to use a . to select the element with class recordId. Also use text() to get the value:
$thevalueIwanttoSave = $(this).siblings('.recordId').text();
alert($thevalueIwanttoSave);

Align tables next to eachothers

I have a loop which generates tables according to a database content.
In other words, the content is changed all the time.
Currently all tables are aligned beneath eachother. This makes one huge vertical scroll, I would save alot of space if the tables could be aligned next to eachother.
How can I do so?
Here is the table code:
$display="<table align='left'>
<tr>
<td>Found $tot_rows records
</td>
</tr>";
foreach ($results->response->docs as $doc)
{
$display.="<tr>
<td align='center'><table align='left' class='table_bg'><tr><td>FIELD NAME</td><td>VALUE</td></tr>";
foreach ($doc as $field => $value)
{
$display.= "
<tr>
<td>".htmlspecialchars($field, ENT_NOQUOTES, 'UTF-8')."</td>
<td>".htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8')."</td>
</tr>";
}
$display.="</table></td></tr>";
}
}// end if $results
$display.="</table>";
Thanks
Put each table into a div like this:
<div class="tables"><table>...</table></div>
<div class="tables"><table>...</table></div>
<div class="tables"><table>...</table></div>
Then, in your CSS:
<style>
...
.tables {
float: left;
display: inline-block;
}
...
</style>
Put each table in cells ("td"-s inside one "tr") of one big outer table?

remove row on click on specific td of that row by jquery

i have an table
<table class="oldService">
<thead>
<th>name</th>
<th>age</th>
<th>action</th>
</thead>
<tbody>
<?php foreach($array as $k=>$v){ ?>
<tr>
<td><?php echo $k[name] ?></td>
<td><?php echo $k[age]?></td>
<td id="<?php $k[id]" class="delme">X</td>
</tr>
<?php } ?>
</tbody>
<table>
now i want to delete any row by clicking on X of each row except first and last row,
and also need to confirm before deletion.
i used below jquery
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('table.oldService>tbody tr').not(':first').not(':last').click(function(){
if(confirm('want to delete!')){
jQuery(jQuery(this).addClass('del').fadeTo(400, 0, function() {
jQuery(this).remove()}));
jQuery.get('deleteService.php', {id:jQuery(this).attr('id')});
}
else return false;});
});
</script>
this is working perfect,but it execute by click on that row( means any td), i want that this event only occour when user click on X(third td) .
please suggest me how to modify this jquery so that the event occur on click of X.
UPDATE:
i want that minimum one row should be in tbody,
means if there is only one row then no function to delete that row, and if there is many rows then any row he can delete but not the all rows.
You can do this with a bit less code like this:
jQuery('table.oldService').delegate('td.delme', 'click', function() {
if(jQuery(this).parent().siblings(":not(.del)").length === 0) return;
if(!confirm('want to delete!')) return;
jQuery.get('deleteService.php', { id:this.id });
jQuery(this).parent().addClass('del').fadeTo(400, 0, function() {
jQuery(this).remove();
});
});​
This attaches one event handler for the table instead of 1 per <td>. First we check if there are any siblings left that aren't deleted (prevent last deletion). Then check if they confirm, then delete the row.
You also need a few html fixes based on the posted question. The last tag needs to be </table> instead of <table>, and those <td> elements in the <thead> need to be wrapped in <tr></tr>. Let me know if you have any trouble, you can see a working demo of this here.
I didn't implement all your animation and AJAX logic, but here is how you could do this in general:
$(function() {
$('table.oldService td.delme:not(:first):not(:last)').click(function() {
$(this).closest('tr').remove();
});
});
Also, I highly recommend you run your code through JSLint. The code sample you posted has a massive number of errors in it.
Try this :
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('table.oldService>tbody tr img.delete').not(':first').not(':last').click(function () {
if(confirm('want to delete!'))
{
jQuery(jQuery(this).addClass('del').fadeTo(400, 0, function() {
jQuery(this).parent().parent().remove()}));
jQuery.get('deleteService.php', {id:jQuery(this).attr('id')});
}
else return false;});
});
</script>
html :
<table class="oldService">
<thead>
<th>name</th>
<th>age</th>
<th>action</th>
</thead>
<tbody>
<?php foreach($array as $k=>$v){ ?>
<tr>
<td><?php echo $v['name'] ?></td>
<td><?php echo $v['age']?></td>
<td><img id="<?php echo $v['id']?>" class="delete" src="del.gif" /></td>
</tr>
<?php } ?>
</tbody>
<table>

Categories