Use html in a php variable - php

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>
<h5 class="prod-size">' . $produto . '<br>' . $refproduto . '</h5>
<div class="clearfix"></div>
<h5 class="text-center font-color">' . $lang['DEFAULT_PRICE'] . ' ' . $preco . ' €</h5>
' . $lang['GERAL_COMPRAR'] . '';

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} €
</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.

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.

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

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.

Related

Image doesn't display using PHP

I'm trying to display the contents from database. Following query is working fine, but the problem is image does not show anymore.
<?php
session_start();
// Include config file
require_once "../auth/dbconnection.php";
$output='';
$sql='select `image`,`title`,`sub_title`,`username`,`blog_id`,`body`,`published` from `blog` WHERE user_id=? ';
$stmt=$conn->prepare( $sql );
$stmt->bind_param( 's',$_SESSION['user_id'] );
$res=$stmt->execute();
if( $res ){
$stmt->store_result();
$stmt->bind_result($image,$title,$sub_title, $username, $blog_id,$body,$published);
while( $stmt->fetch() ){
$filepath="../assets/img/blog_images/";
$title= substr($title,0,30);
$body= substr($body,0,500);
$date= date('dS F Y', strtotime($published));
$output .= '
<div class="col-sm-6 col-md-6 col-lg-4">
<div class="blog grid-blog">
<div class="blog-image">
<a href="#">';
$output .= ' <img style="height:190px; width:330px;" class="img-fluid" src="data:image/png;base64, %s" alt="" />'; base64_encode(file_get_contents($filepath.$image) ) ;
$output .= ' </a>
</div>
<div class="blog-content">
<h3 class="blog-title"> '.$title.' </h3>
<p> <code> '.$body.' </code> </p> <br>
<i class="fa fa-long-arrow-right"></i> Read More
<div class="blog-info clearfix">
<div class="post-left">
<ul>
<li><i class="fa fa-calendar"></i> <span>'.$date.'</span></li>
</ul>
</div>
<div class="post-right"><i class="fa fa-heart-o"></i>21 <i class="fa fa-eye"></i>8 <i class="fa fa-comment-o"></i>17</div>
</div>
</div>
</div>
</div>';
}
echo $output;
}else{
echo 'No any post found';
}
?>
Can anyone guide me how can fix the issue, i would like to appreciate if someone guide me regarding this. Thank You.
I assume that you omit sprintf function.
This should work.
sprintf('<img src="data:image/png;base64, %s" alt="" />', base64_encode(file_get_contents($filepath.$image));
You could put the image binary in separate variable to be easy to use
$binary = "data:image/png;base64," . base64_encode(file_get_contents($filepath . $image));
and use it like this
$output .= '<img style="height:190px; width:330px;" class="img-fluid" src="' . $binary . '" alt="" />';

HTML is processed first in the footer

I've got a problem
In my Footer, there is am Part Html the PHP and then HTML again. The Problem is that on the website the whole HTML is shown before the PHP. You can see it in the photo below.
Thanks, J.S.
<footer class="row align-items-center block bg-light border-t-o">
<!-- Footer Titel -->
<div class="col-lg text-left footer-content">
<h1 class="sponsor">Sponsoren:</h1>
</div>
<!-- Sponsorenbilder -->
<?php
$random1 = rand(1,4);
$random2 = rand(1,4);
$random3 = rand(1,4);
$random4 = rand(1,4);
echo "
<div class='col-md text-center footer-content' style='order: " . $random1 . ";'>
<img class='img-fluid img-hover' src='images/footer/bsp.jpg' alt='Sponsor 1'></img>
</div>
<div class='col-md text-center footer-content' style='order: " . $random2 . ";'>
<img class='img-fluid img-hover' src='images/footer/bsp.jpg' alt='Sponsor 2'></img>
</div>
<div class='col-md text-center footer-content' style='order: " . $random3 . ";'>
<img class='img-fluid img-hover' src='images/footer/bsp.jpg' alt='Sponsor 3'></img>
</div>
<div class='col-md text-center footer-content' style='order: " . $random4 . ";'>
<img class='img-fluid img-hover' src='images/footer/bsp.jpg' alt='Sponsor 4'></img>
</div>
";
?>
<!-- Impressum Button -->
<div class="col-lg text-right footer-content">
<h1 class="impressum impressum-style"><a class="impressum-style" href="impressum.html">Impressum</a></h1>
</div>
</footer>
The problem, if I understand correctly, is not that the PHP is actually loading second, but rather that the actual block of code generated by the PHP is positioned differently to the flow of code.
This is because columns that are given the order style are actually placed after columns that do not have the order style.
To fix this, you can use more PHP to ensure that the 3rd block of code is given an order which is higher than all the others.
<?php
function getHighest($list_of_numbers){
$highest = 0;
foreach($list_of_numbers as $list_item){
if ($list_item > $highest){
$highest = $list_item;
}
}
return $highest;
}
$highest = getHighest([$random1, $random2, $random3, $random4]) + 1;
echo "<div class='col-lg text-right footer-content' style='order: ${highest}'>"
?>

Html link <a> not wrapping <div> element properly

I am currently writing a php website where I encountered a problem where the html link will not wrap div element properly. I have not found an answer to my problem yet, so I appreciate if someone can help me.
This is the php code I had written:
ECHO '<a target="_blank" href="' . $row["link"] . '">';
ECHO '<div class="header-box">';
ECHO '<div class="detail">';
ECHO '<p class="title">' . $row["title"] . '</p>';
$user_data = get_user_data($row["owner_id"]);
ECHO '<p class="author">This article is shared by <a class="user" href="./member.php?action=profile&id='.$user_data["id"].'">' . $user_data["firstname"] . " " . $user_data["lastname"] . '</a> on '.date("d F Y", strtotime($row["posted"])).'.</p>';
ECHO '</div>';
ECHO '<div class="good">';
ECHO '<p id="number">'.$row["good"].'</p>';
ECHO '<p id="icon"><i class="fa fa-thumbs-o-up"></i>Useful</p>';
ECHO '</div>';
ECHO '<div class="bad">';
ECHO '<p id="number">'.$row["bad"].'</p>';
ECHO '<p id="icon"><i class="fa fa-thumbs-o-down"></i>Not Useful</p>';
ECHO '</div>';
ECHO '</div>';
ECHO '</a>';
However, the outcome of the php code in browser are as followed:
<div class="header-box">
<a target="_blank" href="http://designinstruct.com/tools-basics/5-easy-steps-optimize-your-images-photoshop/"></a>
<div class="detail">
<a target="_blank" href="http://designinstruct.com/tools-basics/5-easy-steps-optimize-your-images-photoshop/">
<p class="title">5 Easy Steps to Optimize Your Images in Photoshop</p>
</a>
<p class="author"><a target="_blank" href="http://designinstruct.com/tools-basics/5-easy-steps-optimize-your-images-photoshop/">This article is shared by </a><a class="user" href="./member.php?action=profile&id=2">demoname demo</a> on 01 January 1970.</p>
</div>
<div class="good"><p id="number">1</p><p id="icon"><i class="fa fa-thumbs-o-up"></i>Useful</p></div>
<div class="bad"><p id="number">2333</p><p id="icon"><i class="fa fa-thumbs-o-down"></i>Not Useful</p>
</div></div>
I am getting a little bit frustrated trying to fix the problem because I can't figure out what is going wrong? The other part of code written with html link wrapping the div are working fine. Can anyone help me out? Thanks.
So the question was solved by Rick Hitchcock for pointing out my nesting issue.
So the answer is not nesting an tag within another tag.
After seeing the refactored code, I realized you have a link inside another link. The browser doesn't know what to do with this, so it closes them both down as small as they can.
Try using html inside your .php file, and just using tags where you have to use php. For Example:
close the php with a ?>, then open it each time you need it by wrapping your code in
<?php //php code goes here ?>
Here's your code refactored:
<a target="_blank" href="<?php echo $row["link"] ?>">
<div class="header-box">
<div class="detail">
<p class="title"><?php echo $row["title"] ?></p>
<?php $user_data = get_user_data($row["owner_id"]); ?>
<p class="author">This article is shared by <a class="user" href="./member.php?action=profile&id=<?php echo $user_data["id"] ?>"><?php echo $user_data["firstname"] ?> <?php echo $user_data["lastname"] ?></a> on <?php echo date("d F Y", strtotime($row["posted"])) ?></p>
</div>
<div class="good">
<p id="number"><?php echo $row["good"] ?></p>
<p id="icon"><i class="fa fa-thumbs-o-up"></i>Useful</p>
</div>
<div class="bad">
<p id="number"><?php echo $row["bad"] ?></p>
<p id="icon"><i class="fa fa-thumbs-o-down"></i>Not Useful</p>
</div>
</div>
</a>
Why are you using pure PHP for this? Its not necessary?

Combine site content with external site rss

I'm currently running a classified ads site and planning to display my own content combined with and external site rss.
So here is what i got right now after the db query for the jobs ads,
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
echo '<div class="media margin-none">
<a class="pull-left bg-inverse innerAll text-center" href="#"><img src="'.$foto.'" share_alt="" width="100" height="100"></a>
<div class="media-body innerAll">
<h4 class="media-heading innerT">
'. $remuneracion .' ' . substr(ucfirst(strtolower($row['title'])), 0, 53) . ' <small class="pull-right label label-default"><i class="fa fa-fw fa-calendar-o"></i> ' . $row['date_created'] . '</small></h4>
<p>' . substr(ucfirst(strtolower($row['description'])), 0, 80) . ' ...</p>';
echo '</div>
</div>
<div class="col-separator-h"></div>';
}
echo pagination($statement,$per_page,$page, $url_filtros, $filtros);
?>
it is the while loop that i use to display ads from my database, what could be the best way to display (in this same loop?) other site's rss feed?
Thanks

Functions in multi-line variables

I'm trying to create af multi-line variable and echo out its contents. I have a couple of functions concatenated to the string and for some reason these get output before all of the other html.
/***SNIP***/
$reference[0] = '
<div class="slide">
<div class="reference-image">
</div>
<div class="reference-text">
<h2 class="reference-title">' . the_title() . '</h2>
<div class="reference-content">
' . the_content() . '
</div>
</div>
</div>
---
';
echo $reference[0];
/***SNIP***/
The html this outputs would be:
the_title
the_content
<div class="slide">
<div class="reference-image">
</div>
<div class="reference-text">
<h2 class="reference-title"></h2>
<div class="reference-content">
</div>
</div>
</div>
---
Wordpress (somewhat annoyingly, in my opinion) doesn't return values from the_title() and the_content().
Instead, it echos (writes to the output stream) the value directly, which is why you can't concatinate it to your string. If you want to return the value instead, use the WP functions:
get_the_content()
and
get_the_title()

Categories