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

$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>";

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>";

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.

Indubitably my dear @deceptikon :)

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>";

Yes, the variable is out of scope. Pass it as a parameter to the function.

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

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>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.