Variable inside <th></th>
Hi all!
Qick question, I have this variable below and I'm wondering if it's possible to Exchange the text in <th>Totalt</th> with a varible instead i.e. <th>$Totalt</th>? I couldn't figure it out? :-/
$table1 = "<div id=\"container\"><table id=\"table-3\" cellspacing=\"0\"><thead><tr><th>Totalt</th><th>Totalt</th><th>Benchmark</th><th>Differans</th></tr></thead><tbody>";
Should be simple enough, right...
Peace
Adam
10 Months Ago
Last Updated
adishardis
Junior Poster in Training
91 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
$table1 =
"<div id=\"container\"><table id=\"table-3\" cellspacing=\"0\"><thead><tr><th>Totalt</th><th>" .
$Totalt .
"</th><th>Benchmark</th><th>Differans</th></tr></thead><tbody>";
deceptikon
Challenge Accepted
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
Or seeing as PHP will already parse the entire string because you're using double quotes, you could avoid string contatenation and just include the parameter directly in the string:
$Totalt = 'abc123';
$table1 = "<div id=\"container\"><table id=\"table-3\" cellspacing=\"0\"><thead><tr><th>{$Totalt}</th><th>{$Totalt}</th><th>Benchmark</th><th>Differans</th></tr></thead><tbody>";
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12
Or seeing as PHP will already parse the entire string because you're using double quotes, you could avoid string contatenation and just include the parameter directly in the string
Indeed. Though I chose concatenation because interpolation doesn't stand out as much for this particular string (ie. it's relatively long and noisy). Barring an editor that highlights interpolated variables, I'd prefer the more verbose concatenation for purposes of transparency.
deceptikon
Challenge Accepted
3,454 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
Indubitably my dear @deceptikon :)
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12
Thanks guys, it does not work though, it returns an empty space. Could it be because it's inside a function?
$result12466 = mysql_query("SELECT * FROM ABC WHERE Id ='$first'");
$rows = mysql_fetch_assoc( $result12466 );
$Totalt = $rows['totalt'];
function createHTML($arr){
$first = $arr[0];
$second = $arr[1];
$fieldnames = array_keys($arr[0]);
$nycknamnDIV1[38]="Top";
$nycknamnDIV1[39]="In";
$nycknamnDIV1[40]="Pr";
$nycknamnDIV1[41]="Int";
$nycknamnDIV1[42]="K";
$nycknamnDIV1[43]="Mål";
$nycknamnDIV1[44]="Ut";
$nycknamnDIV1[45]="Ta";
$nycknamnDIV1[46]="E";
$table1 = "<div id=\"container\"><table id=\"table-3\" cellspacing=\"0\"><thead><tr><th>förutsättningar</th><th>".$Totalt."</th><th>Benchmark</th><th>Differans</th></tr></thead><tbody>";
for($x=38;$x<=41;$x++){
if(abs($first[$x] - $second[$x]) <= 5){
$img = "no";
}elseif($first[$x] - $second[$x] > 5){
$img = "up";
}else{
$img = "down";
}
$table1 .= "<tr><td>$nycknamnDIV1[$x]</td><td>{$first[$x]}%</td><td>{$second[$x]}%</td><td><img src=\"images/$img.png\" /></td></tr>";
}
$table1 .= "</tbody></table></div>";
adishardis
Junior Poster in Training
91 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Yes, the variable is out of scope. Pass it as a parameter to the function.
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12
Ok, I'm not entirely sure what "pass it as a parameter to the function" means. I tried moving
$result12466 = mysql_query("SELECT * FROM ABC WHERE Id ='$first'");
$rows = mysql_fetch_assoc( $result12466 );
$Totalt = $rows['totalt'];
Into the function but without success...
So I'm guessing that's not what you meant?!
/Adam
adishardis
Junior Poster in Training
91 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
No, that's not what I meant. Taking your previous code, see line 5:
$result12466 = mysql_query("SELECT * FROM ABC WHERE Id ='$first'");
$rows = mysql_fetch_assoc( $result12466 );
$Totalt = $rows['totalt'];
function createHTML($arr, $Totalt){
$first = $arr[0];
$second = $arr[1];
$fieldnames = array_keys($arr[0]);
$nycknamnDIV1[38]="Top";
$nycknamnDIV1[39]="In";
$nycknamnDIV1[40]="Pr";
$nycknamnDIV1[41]="Int";
$nycknamnDIV1[42]="K";
$nycknamnDIV1[43]="Mål";
$nycknamnDIV1[44]="Ut";
$nycknamnDIV1[45]="Ta";
$nycknamnDIV1[46]="E";
$table1 = "<div id=\"container\"><table id=\"table-3\" cellspacing=\"0\"><thead><tr><th>förutsättningar</th><th>".$Totalt."</th><th>Benchmark</th><th>Differans</th></tr></thead><tbody>";
for($x=38;$x<=41;$x++){
if(abs($first[$x] - $second[$x]) <= 5){
$img = "no";
}elseif($first[$x] - $second[$x] > 5){
$img = "up";
}else{
$img = "down";
}
$table1 .= "<tr><td>$nycknamnDIV1[$x]</td><td>{$first[$x]}%</td><td>{$second[$x]}%</td><td><img src=\"images/$img.png\" /></td></tr>";
}
$table1 .= "</tbody></table></div>";
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12