Reputation: 1011
I am not looking for alternating rows... Rather, I have two tables A and B and Field1 is present in both. I want to present TableA and everywhere that the field1 matches the field1 in TableB, I would like to highlight the row. How can I do this?
I know how to do this in SQL, but the question is how to present this in the browser. I think I must use javascript. Any advice?
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif ORDER BY aif_id DESC");
$result->setFetchMode(PDO::FETCH_ASSOC);
echo "<table id=\"all_aifs\">";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";
foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$dbh = NULL;
?>
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining LIMIT 0,50");
$result->setFetchMode(PDO::FETCH_ASSOC);
echo "<table>";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
echo "</tr>";
foreach($result as $index => $row) {
echo "<tr>";
echo "<td>$row[fee_source_id]</td>";
echo "<td>$row[company_name_per_sedar]</td>";
echo "<td>$row[document_filing_date]</td>";
echo "<td>Placeholder</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$dbh = NULL;
?>
<?php
require_once 'config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining");
$result->setFetchMode(PDO::FETCH_ASSOC);
$match = $dbh->query("SELECT a_aif.aif_id FROM a_aif INNER JOIN a_aif_remaining WHERE a_aif_remaining.aif_id = a_aif.aif_id");?>
<?php if ( !empty($result) ) : ?>
<table id="all_aifs">
<tr>
<th><b>Document ID</b></th>
<th><b>Pubco Name</b></th>
<th><b>Filing Date</b></th>
<th><b>PDF</b></th>
</tr>
<?php foreach($result as $index => $row) : ?>
<tr>
<?= $row['aif_id'] == true ? ' class="match"' : '' ?>
<td><?php echo $row[fee_source_id]; ?></td>
<td><?php echo $row[company_name_per_sedar]; ?></td>
<td><?php echo $row[document_filing_date]; ?></td>
<td>Placeholder</td>
</tr>
<?php endforeach; ?>
</table>
<br>
<?php endif;
$dbh = NULL;
?>
Upvotes: 0
Views: 318
Reputation: 8701
First of all, if you are going to keep your code maintainable and clean as well, then you should avoid printing HTML TAGS.
The way not to go:
echo "<table>";
echo "<tr>";
echo "<th><b>Document ID</b></th>";
echo "<th><b>Pubco Name</b></th>";
echo "<th><b>Filing Date</b></th>";
echo "<th><b>PDF</b></th>";
The way to go:
<?php
require_once 'config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$result = $dbh->query("SELECT aif_id, fee_source_id, company_name_per_sedar, document_filing_date FROM a_aif_remaining LIMIT 0,50");
$result->setFetchMode(PDO::FETCH_ASSOC);
?>
<?php if ( !empty($result) ) : ?>
<table>
<tr>
<th><b>Document ID</b></th>
<th><b>Pubco Name</b></th>
<th><b>Filing Date</b></th>
<th><b>PDF</b></th>
</tr>
<?php foreach($result as $index => $row) : ?>
<tr>
<td><?php echo $row[fee_source_id]; ?></td>
<td><?php echo $row[company_name_per_sedar]; ?></td>
<td><?php echo $row[document_filing_date]; ?></td>
<td>Placeholder</td>
</tr>
<?php endforeach; ?>
</table>
<br />
<?php endif; ?>
Conclusion:
1) The code now looks more readable and clean as well
2) You separated PHP from HTML
3) You can easily determine a problem once it occurs (like, parse error at ..
, unexcepted ';' ...
Back to your question
So how can you highlight a row you need? Well it's easy, just use if/else in a foreach
loop, like
<?php foreach($result as $k => $v): ?>
<?php if ( $k == 'some_value_you_expect_to_be_highlighted' ) : ?>
<tr>... should be highlighted ...</tr>
<?php else : ?>
<tr>.... a regular row</tr>
<?php endif; ?>
<?php endforeach; ?>
Upvotes: 2
Reputation: 37905
Perform your check in SQL (since you know how to do so).
In PHP use the result of your check to add an (additional) class-attribute to the HTML element that needs the highlight (just like 'alternating rows' do):
<?php
$query = ... // perform your SQL query
// TODO Open HTML table
// TODO Loop though results:
$row = ... // fetch you row here
?>
<tr <?= $row['my_check'] == true ? ' class="match"' : '' ?>>
<td><?= $row['field'] ?></td>
</tr>
<?php
// TODO close HTML table
This (incomplete) example uses the SQL check (named my_check
) and adds the match
class when required. In CSS you can use this match
class to apply highlighting:
.match {
background-color: red;
}
Upvotes: 3
Reputation: 4291
No, you don't 'have' to use JavaScript. You can use PHP.
When generating your rows, check whether the condition is met and put your 'highlighted' class into the row definition.
Pseudocode:
<?php
if(Condition) echo "<tr class='highlighted'>";
else echo "<tr>";
echo "<td>CellStuff</td>";
echo "</tr>";
?>
Upvotes: 2
Reputation: 2524
You should simply add a class to the rows where the data matches, and set a background color in the associated CSS.
<table> <tr class="matching"> ... </tr>
How to write the appropriate CSS is beyond the scope of this answer. ;-)
Whether that class is added by your web server's PHP code, or by some JavaScript in the client, depends on the design of your Web application.
Upvotes: 2
Reputation: 1205
I'm not sure if I could understand your problem correct but I dont think you need JS. You can calculate if a cell/row should be highlighted in PHP and set a css class, also in PHP.
Upvotes: 0