-1

I have this variable in php with html, but it doesn't work. Can you guys find any mistake...I tried and didn't find anything. The code is:

$html .= '<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
            <div class="box-produtos">                
                <a href="' . $url_imagem . '" class="magnifier" rel="shadowbox">
                    <img src="' . $url_imagem . '" class="img-responsive img-produtos center-block" onerror="imgError(this);">
                </a>

            <div class="facebook-btn" onclick="window.open("http://www.facebook.com/sharer/sharer.php?u=' . $base_url . '"/facebookPublish.php?idproduto="' . $idproduto . '", "sharer", "toolbar=0,status=0,width=548,height=325");"><i class="fa fa-facebook fc-facebook"></i>' . $lang["FACEBOOK_SHARE"] . '</div>

            <div class="clearfix"></div>             
            <a href="' . $base_url . '/' . $langi . '/produtosDesc/' . $idproduto . '"><h5 class="prod-size">' . $produto . '<br>' . $refproduto . '</h5></a>                
            <div class="clearfix"></div>
            <h5 class="text-center font-color">' . $lang['DEFAULT_PRICE'] . ' ' . $preco . ' &euro;</h5>
            <a href="' . $base_url . '/' . $langi . '/produtosDesc/' . $idproduto . '" class="btn btn-skin center-block btn-prod">' . $lang['GERAL_COMPRAR'] . '</a>';     
Thamilhan
  • 13,040
  • 5
  • 37
  • 59

4 Answers4

3

You can use Heredocs to mix PHP with HTML in pretty way.

$html .= <<< HTML
    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
        <div class="box-produtos">                
            <a href="{$url_imagem}" class="magnifier" rel="shadowbox">
                <img src="{$url_imagem}" class="img-responsive img-produtos center-block" onerror="imgError(this);">
            </a>

            <div class="facebook-btn" onclick="window.open("http://www.facebook.com/sharer/sharer.php?u={$base_url}/facebookPublish.php?idproduto={$idproduto}", "sharer", "toolbar=0,status=0,width=548,height=325");">
                <i class="fa fa-facebook fc-facebook"></i>{$lang["FACEBOOK_SHARE"]}
            </div>

            <div class="clearfix"></div>             
            <a href="{$base_url}/{$langi}/produtosDesc/{$idproduto}">
                <h5 class="prod-size">{$produto}<br/>{$refproduto}</h5>
            </a>                
            <div class="clearfix"></div>
            <h5 class="text-center font-color">
                {$lang['DEFAULT_PRICE']} {$preco} &euro;
            </h5>
            <a href="{$base_url}/{$langi}/produtosDesc/{$idproduto}" class="btn btn-skin center-block btn-prod">
                {$lang['GERAL_COMPRAR']}
            </a>
HTML;// no indentation, must be single in line, so remove this comment

Also you were messing with facebook link in onclick attribute.

Justinas
  • 41,402
  • 5
  • 66
  • 96
2

If you want to avoid the mistakes of quotes you can try something like this :

$var="variables";
$html =<<<HTML
<h1>All your html code</h1>
<p>with all your $var or {$var}</p>
HTML;

echo $html;

it's safer.

Alexis
  • 5,681
  • 1
  • 27
  • 44
0

Use <?php ?> whenever specifying the php variable.

MACMAN
  • 1,883
  • 1
  • 21
  • 35
-2

You cannot spread a string over multiple lines. (Justinas sais you can. See comment below)

I think this question is related to your one: Best Practices: working with long, multiline strings in PHP?

Edit 1:

I think an other solution could also be that you have your HTML in a separate file an then read its content to your variable.

$html .= file_get_contents("your-file.html");

Edit 2: Removed wrong statement. As mentioned in Justinas comment.

Community
  • 1
  • 1
hiddenAlpha
  • 390
  • 4
  • 10
  • Why do you think you can't? It's not JS. In JS you can only have single string per line and it must be concatenated. *About edit* how you will put PHP var's in this `$html` than? – Justinas May 27 '16 at 08:39
  • Sure. You cannot simply use php variables in there. I had no need to do that in my case. I though solution may'll be helpful in case someone encounters same problem as i did. – hiddenAlpha Oct 19 '17 at 10:13