PHP - syntax to escape a string for a link

I can’t get the syntax for this quite right.

I have a line in a repeating table which works as it should, i.e. repeats the field ‘Activity’ from my query.

echo "<td width=\"18%\" align=\"left\">".$keyword['Activity']."</a></td>\n";

I am trying to make the Activity name a link to an update page:

<a href="../lodge_keyword_update/index.php?ActivityID=<?php echo(rawurlencode($row_WADAactivities['ActivityID'])); ?>">

The closest I seem to get is something like this:

echo "<td width=\"18%\" align=\"left\"><a href=\"../lodge_keyword_update/index.php?ActivityID=<?php echo(rawurlencode($row_WADAactivities['ActivityID'])); ?>\">".$keyword['Activity']."</a></td>\n";

But its not quite right - if someone could help out with the correct syntax that would be much appreciated. Thanks.

echo is already “in” PHP then the code has <?php in it though it never “left” PHP.

Mixing mark-up with PHP can get messy, that’s for sure.

Thanks - although I still can’t quite get it.

Get to this, which at least seems correct:

echo "<td width=\"18%\" align=\"left\"><a href=\"../lodge_keyword_update/index.php?ActivityID=\">".$keyword['Activity']."</a></td>\n";

Just not sure how to add in the part after ActivityID=

That looks like it should be working OK. (but I’m tired and might be missing something)

The String portions are enclosed inside double quotes and other double quotes inside of them are escaped so as to be part of the String. And the String-PHP-String is concatenated with periods.

EDIT
Now I see it. You have $var as the link text instead of the query var value. Try moving the ">" over with the "</a>"

First thing I’d do is move any function calls out of the string you’re building/outputting, as it’ll make things easier to read:

$activityId = rawurlencode($row_WADAactivities['ActivityID']);

As you’re using double quotes, you can include variables directly inside the string (within curly braces, to avoid problems with array values) and PHP will replace them with their values. This also simplifies the code a little:

echo "<td width=\"18%\" align=\"left\"><a href=\"../lodge_keyword_update/index.php?ActivityID={$activityId}\">{$keyword['Activity']}</a></td>\n";

Thanks for the replies, much appreciated.

It looks like its working with this:

echo '<td width="18%" align="left"><a href="../lodge_keyword_update/index.php?ActivityID=' . rawurlencode($row_WADAactivities['ActivityID']) . '">' . $keyword['Activity'] . '</a></td>' . "\n";
1 Like

That is how I like to write using ’ instead of ", in my opinon it’s easier to read .

I have never liked using the backslash and far prefer this method of formatting:

Version #1

$keyword = array();
$keyword['Activity'] = 'Keyword Activity';
$b = '<td width="18%" align="left">' 
   .  '<a href="' 
	 .'.../lodge_keyword_update/index.php?ActivityID='
   .    $keyword['Activity']
   .  '">'
   .  $keyword['Activity']
   .  '</a>' 
   .'</td>';
  
echo '<table><tr>';
	echo $b;   
echo '</tr></table>';

Version #2

	$keyword = array();
	$keyword['Activity'] = 'Keyword Activity';
	$url = '.../lodge_keyword_update/index.php?ActivityID=' .$keyword['Activity'];

	$b = '<td width="18%" align="left">' 
	   .  '<a href="' .$url .'">'
	   .     $keyword['Activity']
	   .  '</a>' 
	   . '</td>';

  
	echo '<table><tr>';
		#echo $a;   
		echo $b;   
	echo '</tr></table>';	
2 Likes

Thanks again for the help with this. I have one more similar, but I think more complicated.

It looks like this in HTML:

<input type="button" class="formButtonDeleteButton" value="" onclick="document.getElementById('WADADeleteRecordID').value=<?php echo($row_WADAactivities2['ActivityID']); ?>;document.getElementById('WADADeleteRecordName').innerHTML='<?php echo($row_WADAactivities2['Activity']); ?>';document.getElementById('deleteBox').style.display = 'block';document.getElementById('deleteMessage').style.display = 'table';" />

I get so far with it, but just get a bit lost, e.g.:

echo '<input type="button" class="formButtonDeleteButton" value="" onclick="document.getElementById('WADADeleteRecordID').value=' . rawurlencode($row_WADAactivities2['ActivityID']) . ;document.getElementById('WADADeleteRecordName').innerHTML=' . rawurlencode($row_WADAactivities2['Activity']);';document.getElementById('deleteBox').style.display = 'block';document.getElementById('deleteMessage').style.display = 'table';" \"/>";

This is pretty much the last bit of something I’ve been looking at that needs tidying up.

Thanks again.

Try this:

$c = '
  <input 
   type  = "button" 
   class = "formButtonDeleteButton" 
   value="" 
   onclick="document.getElementById
   (
   	"WADADeleteRecordID").value=' 
	. $row_WADAactivities2["ActivityID"]
   .')  document.getElementById("WADADeleteRecordName")'
  .' innerHTML="(' 
      .$row_WADAactivities2["Activity"]
 .') 
     document.getElementById("deleteBox")    .style.display = "block";
     document.getElementById("deleteMessage").style.display = "table"; 	
/>';	

Basically the idea is:

  1. set PHP error_reporting(-1); ini_set(‘display_errors’,1);
  2. start and end with a single quotation
  3. each element to be on a separate line
  4. join PHP variables with .
  5. get a good editor :smile:

FYI: To format this PHP script:

  1. start block with a new line with three backticks followed by PHP
  2. paste PHP script
  3. finish block with three backticks

Just as an HTML aside, align="left" is way way way out of date. Consider putting a class in there and using CSS instead.

Also try to avoid inline JS (e.g. onclick="...").

It’s generally not a good idea to include JS inline like this. It would be better to attach your click handler from a script block or external JS file - this would simplify the HTML above considerably.

Also, as I mentioned in my previous post, where strings become complicated because of combining output from functions and things like that, you can always assign these values to temporary variables before combining them into the final string. Verbose, readable code is always preferable to terse, complicated code.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.