Php variable in a html table - php

I'm trying to display an html table in which I'd like to show some php variables.
My code is the following:
<?php
$title = the_title();
?>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $title?></td>
</tr>
</tbody>
</table>
In the output I can see the word "Name" as expected but it doesn't print the $title variable (there is a empty space instead).
I've already read some of the questions on the site but they doesn't help me because they suggest to write the same code that I wrote.

Related

display plain text php mysql

after having found the data in a table the function displays the information without missing, with a td tag only the first word which displays the others after the space are not displayed.
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Service </th>
<th>Prix</th>
<th >Quantite</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$reqq = "SELECT * FROM service WHERE client = '$clientId' AND dossier = '$dossier' ";
$results = $connect->query($reqq);
?>
<?php if($results->num_rows > 0){ ?>
<?php
foreach($results as $data){ ?>
<?php echo $data['nom']; ?>
<tr>
<td> <?php print $data['nom']; ?>
<td><?= $data['prix']?></td>
<td><?= $data['quantite']?></td>
<td>Supprimer</td>
</tr>
</tbody>
</table>
the result is
Inside the table the last field have Franchise Procedure the table only display the first word which is Franchise
Help me find the solution please.
Try with:
<td> <?php echo htmlspecialchars($data['nom']); ?> </td>
Often, text stored in databases can contains reserved characters in HTML like "<" or "&" which should be replaced by html entities
That's what the PHP function htmlspecialchars was invented for.

why does simple_html_dom find('tr')[0] get table row 2 instead of table row 1?

why does find('tr')[0]; get table row 2 instead of table row 1 ?
This is my html all tables have the same class and layout.
<table class="tablemenu">
<tbody>
<tr>
<td><b>hello</b></td>
<td><b>hi</b></td>
</tr>
<tr>
<td>hey</td>
<td>Alright</td>
<td>Good</td>
<td>Good</td>
<td><a>Date</a></td>
</tr>
</tbody>
</table>
<table class="tablemenu">
<tbody>
<tr>
<td><b>hello</b></td>
<td><b>hi</b></td>
</tr>
<tr>
<td>hey</td>
<td>Alright</td>
<td>Good</td>
<td>Good</td>
<td><a>Date</a></td>
</tr>
</tbody>
</table>
<table class="tablemenu">
<tbody>
<tr>
<td><b>hello</b></td>
<td><a>hi</a></td>
</tr>
<tr>
<td>hey</td>
<td>Alright</td>
<td>Good</td>
<td>Good</td>
<td><a>LINK</a></td>
</tr>
</tbody>
</table>
This is my php
<?php
include("simpleHtmlDom/simple_html_dom.php");
$html = new simple_html_dom();
// Load a file
$html->load_file('http://mySite.net/');
foreach($html->find('table[class=tablemenu]') as $element){
$Link = $element->find('tr')[0]->find('td')[4]->find('a')[0];
echo($Link->text());
echo '<br />';
}
?>
At first to get the word 'Date' i tried
$Link = $element->find('tr')[1]->find('td')[4]->find('a')[0];
But that didn't work, it said undefined index.
Then i tried this just messing around and it works
$Link = $element->find('tr')[0]->find('td')[4]->find('a')[0];
This gets the word Date for some reason. I don't understand why, i do need that but
although it works - i now can't access table row 1. to grab the word say "hi".
I see two one issues:
Your first <tr> only has 2 <td>s, so $element->find('tr')[0]->find('td')[4] should throw an exception.
Edit OP fixed pasted code.
Fix your markup. You're not properly closing your <tr> elements:
<table class="tablemenu">
<tbody>
<tr>
<td><b>hello</b></td>
<td><b>hi</b></td>
</tr> <!-- close this! --->
<tr>
<td>hey</td>
<td>Alright</td>
<td>Good</td>
<td><a>Date</a></td>
</tr> <!-- close this! --->
</tbody>
</table>
There is wrong indexing because you are not closing the tr tags properly
the link should be on first index instead of zeroth index
$Link = $element->find('tr')[1]->find('td')[4]->find('a')[0];
To print hi try
echo $element->find('tr')[0]->find('td')[1]->find('b')[0]->text();
Full code
foreach($html->find('table[class=tablemenu]') as $element){
$Link = $element->find('tr')[1]->find('td')[4]->find('a')[0];
echo($Link->text());
echo '<br />';
echo $element->find('tr')[0]->find('td')[1]->find('b')[0]->text();
}
If the above not works then find tr in tbody like
$Link = $element->find('tbody')->find('tr')[1]->find('td')[4]->find('a')[0];
Also for debugging, try this
foreach($html->find('table[class=tablemenu]') as $element){
echo '<pre>';
var_dump($element);// find the object here
echo '</pre>';
}

display empty/blank table if query returns false

what would be the proper implementation of displaying an empty/blank table or just the table header if query result is empty?
**note/conditions
no page redirection
no creation of two tables, one for query with empty result and one for query with results
or is there a much, much better way to do this?
here is a sample code:
<?php if(isset($result)){ ?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?php foreach($result as $key => $data){?>
<tr>
<td><?php echo $data['name'];?></td>
<td><?php echo $data['email_add'];?></td>
</tr>
<?php }?>
</table>
<?php } ?>
the problem here is that it still throws an error on foreach loop.
When the query result returns false return an empty array instead
I hope this help , and ready for more help if needed
About the exception:
You check if $results is set, but then you try to loop over $result (without a trailing s). I believe you have a typo, which might be a part of the problem. Fixing the typo and making sure that the result is not empty (it might be set, but still empty) will probably fix the exception being thrown.
About always showing the header:
To display the header no matter what, move the if-statement to just before the the loop.
FYI - About well formatted HTML-tables:
To declare a header on a table, you usually make use of the <thead> element - to separate it from the content of the table. An example of a well-formatted HTML-table:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>john#email.com</td>
</tr>
</tbody>
</table>
More on formatting tables
In your case, I would put the loop around the <tr> element within the <tbody>.
<?php if(isset($result) && !empty($result)): ?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?php foreach($result as $data): ?>
<tr>
<td><?php echo $data['name'];?></td>
<td><?php echo $data['email_add'];?></td>
</tr>
<?php endforeach ?>
</table>
<?php else: ?>
No results found.
<?php endif ?>
Note that I prefer to use the long-form of statements for PHP templates, as it greatly improves readability.

What is the best way to display a table if an array is not null in PHP?

If the array is not null (and has values in it), then I want to display the table.
But if it is null, then I don't want to display any table code at all.
Using an MVC framework which appends a footer to the page.
What is the best way to avoid a statement like:
<?php
if ($users) {
echo '<table id="tha_table" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>';
} ?>
And, don't want to do another test to add the table footer.
I think I see what you are after...
I would place all of the HTML in a separate file, and conditionally include it.
if(!empty($users)) {
include "users_table.template";
}
Note that the template file can include php if you want it to.
I always use empty() to check whether an array is empty. Empty will also check whether the variable is null. Note that empty() does not throw a warning if the array variable is not set, which may or may not be desirable.
<?php
$displayUserTable = !empty($users);
?>
<?php if($displayUserTable): ?>
<table id="tha_table" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $user): ?>
<tr>
<td><?php echo htmlspecialchars($user['firstName']); ?></td>
<td><?php echo htmlspecialchars($user['lastName']); ?></td>
<td><?php echo htmlspecialchars($user['emailAddress']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php if($displayUserTable): ?>
<!-- show footer here... -->
<?php endif; ?>
I recommend you use either a templating system or any other vehicle to separate your PHP code from the HTML rendering.
All template systems I know of allow for a block to be skipped depending on a boolean, so you would just include the (template for the) table in your page template and surround it with whatever your chosen framework uses as an if or repeat n times construct.

Called hyperlink stopped showing when CSS table implemented

EDIT: Solved - was not flutter's tag stripping, should work as advertised.
I'm using Flutter (which creates custom fields) in Wordpress to display profile information entered as a Post. Before I implemented the CSS tables the link showed up and was clickable. Now I get nothing returned, even when I try to call the link outside the table.
If you know anything about this, here's my code in the index.php file and I remain available for any questions.
<?php if (in_category('Profile')) { ?>
<table id="mytable" cellspacing="0">
-snip-
<tr>
<th class="row1" valign="top">Website </td>
<td>Link: <?php echo get_post_meta($post->ID, 'FrWebsite', $single=true) ?></td>
</tr>
-snip-
</table>
Edit: #Josh - there is a foreach looping construct in the table and it is reading and displaying the code correctly, I see what you're getting at now:
<tr>
<th class="row2" valign="top">Specialities </td>
<td class="alt" valign="top"><?php $my_array = get('Expertise');
$output = "";
foreach($my_array as $check)
{
$output .= "<span>$check</span><br/> ";
}
echo $output; ?></td>
</tr>
Edit - #Josh - here's the old code as far as I can remember it, there was no major difference just a <td> tag where there now stands a <th>, there wasn't the class="" and there was no "Link:" and FrWebsite was called Website, but it still didn't work when called Website so I changed to see if that was the error.
<tr>
<td width="200" valign="top">Website </td>
<td><?php echo get_post_meta($post->ID, 'Website', $single=true) ?></td>
</tr>
Where is $post set? What does the full table look like? Could be that when you changed the table structure you accidentally removed something like (line 2 below):
<table id="mytable" cellspacing="0">
<?php foreach($posts as $post) { ?>
<tr>
<th class="row1" valign="top">Website </td>
<td>Link: <?php echo get_post_meta($post->ID, 'FrWebsite', $single=true) ?></td>
</tr>
<?php } ?>
(Just guessing here...)

Categories