sika
sika

Reputation: 147

Escaping quotes in PHP with variable

There are numerous question on Stackoverflow concerning this but I have not been able to solve this still.

I'm trying to put multiple variables inside global attribute data-*. The output should be:

data-info="23;thisWeek"

The 23 comes from a database: $row["id"], and thisWeek from a variable: $categori1.

I've tried:

echo "<tr data-info=" .fixSlashes($row["id"], $categori1); " class=\"tableclass\"><td>"

Then

<?php
function fixSlashes($idP, $categoriP){
$str = addslashes($idP . ";" .$categoriP);
return $str;
}
?>

But something goes wrong. No error message, but the behaviour is wrong. It works if I just write:

echo "<tr data-info="data-info="23;thisWeek" class=\"tableclass\"><td>"

Upvotes: 0

Views: 68

Answers (4)

Peter Bowers
Peter Bowers

Reputation: 3093

I hate to piggy-back, but half the answers are getting the one problem and half the answers are getting the other problem.

You need to fix your semi-colon to a period (concat) as @MingShun said. You need to get quotes (escaped) around your data as @JimmyScray said.

echo "<tr data-info=\"" .fixSlashes($row["id"]. $categori1). "\" class=\"tableclass\"><td>";

Upvotes: 1

Jimmy Scray
Jimmy Scray

Reputation: 154

It looks like you are not outputting the html correctly. Note the escaped quotes around the fix Slashes function. Also note the "." instead of the semicolon.

"<tr data-info=\"" .fixSlashes($row["id"], $categori1) . "\" class=\"tableclass\"><td>"

If you want to see all errors include the following code. This would have caught the semicolon error.

error_reporting(E_ALL);
ini_set('display_errors', '1');

Upvotes: 2

Piyush
Piyush

Reputation: 23

Use escape sequence for displaying double quotations and use single quotations for id (i.e $row['id'])

 echo "<tr data-info=\" $row['id'];$categori1\" class=\"tableclass\"><td>";

Upvotes: 0

MingShun
MingShun

Reputation: 287

echo "<tr data-info=" .fixSlashes($row["id"], $categori1); " class=\"tableclass\"><td>"

You have a semicolon (;) where the concat operator (.) should be. Umm, after the fixSlashes function. Before the class suffix string.

You're ending the echo statement prematurely, before the "class=\"tableclass\">" can be appended.

Upvotes: 1

Related Questions