I am working on a project were I need to turn text in a table cell into a dynamic link and I am getting an error when escaping to HTML.
Here are the line of code I am trying to turn into a link. It works perfect before I try to make it a link.
echo "<td><a>" . (isset($resultSetArray[$x]["assessment"]["owner1"]) ?
ucwords(strtolower($resultSetArray[$x]["assessment"]["owner1"])) : '')
. "</a></td>";
It returns a name from the database. I need this name to be a link to another page, with the name in the URL.
Here is what I tried without success. Where am I going wrong?
echo "<td>" . "<a href=\"http://Company.com/secure/IndSearch?owner="
(isset($resultSetArray[$x]["assessment"]["owner1"]) ?
ucwords(strtolower($resultSetArray[$x]开发者_如何学Python["assessment"]["owner1"])) : '')"/"
(isset($resultSetArray[$x]["assessment"]["owner1"]) ?
ucwords(strtolower($resultSetArray[$x]["assessment"]["owner1"])) : '') . "</a></td>";
I've identified two with your code:
- You are double-quoting HTML you're trying to echo
- You're using ternary operator which is hard to debug at first glance
There's a way to write that code in a much nicer way so you can immediately see where you made the mistake (or add additional HTML markup / variables etc).
The string quoting syntax is called HereDoc
$owner = '';
if(isset($resultSetArray[$x]["assessment"]["owner1"]))
{
$owner = $resultSetArray[$x]["assessment"]["owner1"];
}
$str = <<<EOF
<td><a href="http://Company.com/secure/IndSearch?owner=$owner">$owner</a></td>
EOF;
echo $str;
As you can see, you don't have to check the array twice to confirm entry exists, nor do you have to escape any quotes so you can safely paste your HTML without any worries AND you can use PHP variables too.
I think you are missing some concatenation dots .
in your echo statement. Esp. before first isset
function call.
btw your code needs serious refactoring. There are a lot of redundant calls that you can avoid by saving those return values in local variables.
You missed a dot when concat strings:
echo "<td>" . "<a href=\"http://Company.com/secure/IndSearch?owner="
(isset($resultSetArray[$x]["assessment"]["owner1"]) ?
ucwords(strtolower($resultSetArray[$x]["assessment"]["owner1"])) : '') . "/"
// ^ missing one
....
ucwords(strtolower($resultSetArray[$x]["assessment"]["owner1"])) : '')"/"
to
ucwords(strtolower($resultSetArray[$x]["assessment"]["owner1"])) : '')."/"
You were missing a period.
You are missing some concatenation dots, for example between the first line and the first isset
.
you are missing concatinating periods are the end of line 1, and two on line 3
精彩评论