Concatenating In PHP - php

What is the proper way to write the following code?
echo '<img src="'images/.$row['picture']. '"/>';
I want to display an image from the database.

If I understand the question correctly,
<?php
echo '<img src="/images/' . $row['picture'] . '"/>';
?>
or
<?php
echo "<img src='/images/" . $row['picture'] . "'/>";
?>

You can use vprintf function: http://www.php.net/manual/ru/function.vprintf.php
vprintf('<img src="images/%s"/>', $row['picture']);
Or this:
echo "<img src=\"{$row['picture']}\" />";
Don't forget to escape html characters: http://php.net/manual/en/function.htmlspecialchars.php
$row['picture'] = htmlspecialchars($row['picture'], ENT_QUOTES);

echo '<img src="images/'.$row['picture'].'" />';

Try
<?php
echo "<img src=images/".$row['picture']."/>";
?>

echo '<img src=images/'.$row['picture'].'>';

Related

Image and words with classes won't display (PHP CSS HTML)

Got problem displaying the below picture and words, what am I doing wrong?
echo "<img src="images/<?php echo $row["image"]; ?>" class="img-responsive" /><br />";
echo "<h4 class="text-info"><?php echo $row["name"]; ?></h4>";
AS you are working in php so you have no need to echo <?php echo $row["image"]; ?> in side an echo and you have to consider about single quotes and double quotes
Do like this
echo "<img src='images/".$row["image"]."' class='img-responsive' /><br />";
echo "<h4 class='text-info'>".$row["name"]."</h4>";
At first you need to use php tag for translating echo tag after do like this..
<?php
echo '<img src="images/echo $row["image"];" class="img-responsive" /><br/>';
echo '<h4 class="text-info"> echo $row["name"]; </h4>';
?>
try this.
echo "<img src='images/'". $row['image']." class='img-responsive' /><br />";
echo "<h4 class='text-info'>".$row['name']."</h4>";

html code inside php if statement

I am trying to add a video tag if the PHP code receives it from a $_GET method.
The problem is in the 2nd echo
<?php
if($_GET['video1'] == NULL) {
}
} else {
$vid = $_GET["video1"];
echo '<video width="320" height="240" controls>';
echo '<source src=' . <?php echo $_GET["video1"]; ?> . ' type="video/mp4">';
echo '</video>';
}
?>
PHP is not recursively embeddable:
echo '<source src=' . <?php echo $_GET["video1"]; ?> . ' type="video/mp4">';
You are ALREADY in "php mode" with your echo statement.Therefore you cannot "go deeper" into php mode.
Why do you need such a hideously ugly convoluted statement anyways? Why can't you simply have
echo '<source src=' . $_GET["video1"] . ' type="video/mp4">';
?
Replace:
echo '<source src=' . <?php echo $_GET["video1"]; ?> . ' type="video/mp4">';
With:
echo '<source src=' . $_GET["video1"] . ' type="video/mp4">';
There is no need to nest PHP code clocks.

Display image from database as background of a div element in php

Currently I'm learning about PHP and database, I wrote the script below to display the image as background for my div element, but the output is actually nothing! the error is from the line:
echo "<div class=\"post\" style='background-image: url('\"<?php echo $data[Image];\"?>')'";?>
The quotes mess up! Can someone tell me how to correct this? I tried to change double quotes to single quotes, but still doesn't work at all.
This is my full script:
<div class="dashboardA">
<?php
$con = mysqli_connect("localhost", "Dave", "password");
if (!$con){
die ("Could not connect to database: " . mysqli_connect_error());
}
mysqli_select_db($con, "my_blog");
$sql = mysqli_query($con, "select * from article");
while ($data=mysqli_fetch_array($sql)){
echo "<div class=\"post\" style='background-image: url('\"<?php echo $data[Image];\"?>')'";?>
<?php echo "<p>" . $data["Title"] . "</p>";
echo "<p>" . $data["Category"] . "</p>";
echo "<p>" . $data["Published"] . "</p>";
echo "</div>";
}
?>
</div>
You are getting a little mixed up. You already have PHP opening tags, you don't need them again. Just concatenate your variable:
echo "<div class='post' style='background-image: url(\"$data[Image]\")'>";
Note: You also need to close your opening <div> tag.
There is a mistake in the line
echo "<div class=\"post\" style='background-image: url('\"<?php echo $data[Image];\"?>')'";?>
You are already in php, so the opening tags are wrong there.
Try:
echo "<div class=\"post\" style='background-image: url(\"" . $data[Image] . "\")'";?>
Try this
while ($data=mysqli_fetch_array($sql)){
echo "<div class=\"post\" style='background-image: url('\"<?php echo $data[Image];\"?>')'";?>
<?php echo "<p>" . $data["Title"] . "</p>";
echo "<p>" . $data["Category"] . "</p>";
echo "<p>" . $data["Published"] . "</p>";
echo "</div>";
}
Replace this
with this
while ($data=mysqli_fetch_array($sql)){
$img = $data["Image"];?>
<div class="post" style="background-image: url('<?php echo $img;?>')" >
<?php echo "<p>" . $data["Title"] . "</p>";
echo "<p>" . $data["Category"] . "</p>";
echo "<p>" . $data["Published"] . "</p>";
echo "</div>";
}
You actually did it right in the following echo commands. To combine the value of your variable and a string you use the "." that basically substitutes for "+" that is used in other languages for this purpose. Opening a new php tag is therefore not needed here.
The correct code is
echo '<div class="post" style="background-image: url(\'' . $data[Image] . '\')">';
Try this, outside php tags.
<div class="post" style="background-image: url('<?php echo "$data[Image]"; ?>');">

Is there a better or more efficient way to do this?

This code is working for what I need it to do, but (in my opinion) it looks bad so I am hoping someone knows of a cleaner or more efficient way to do the same thing. I have several entries being pulled from the database and I want them to be styled identically. Only the logo and the the link name will change eventually I will add a description. Here is the code:
<div class="content">
<?PHP
while($row = $stmt->fetch())
{
$name = $row['name'];
$id = $row['id'];
$logo = $row['logo'];
$username = $row['username'];
echo "<div class=" . "Links" . ">";
echo "<div class=" . "linkImages" . ">";
echo "<br>" . "" . "<img src=" . "users/" . $username . "/images/" . $logo . " " . "width=" . "200" . " " . "height=" . "auto" . " " . "border=" . "0" . "/>" . "";
echo "</div>";
echo "<div class=" . "linkName" . ">";
echo "" . $name ."";
echo "</div>";
echo "</div>";
}
?>
</div>
You can trivially remove most of the echoes and string concatenation by switching to a HEREDOC:
while($row = $stmt->fetch()) {
echo <<<EOL
<div class="links">
yadayada
<br><a href="Profile.php?id={$row['id']}"><img src="users/{$row['username']}" etc....
yada yada yada
EOL;
Note that the lack of escapes in there, allowing for proper quotes around the tag attributes, and the {} notation on the embedded variables.
Don't use extra variable names. Instead, use the original.
Also, don't output every row with PHP. Use plain HTML and add the variable in it later:
<div class="Links">
<?=$row['name']?>
Or just echo as 1 line, no need for concatenation
echo "<div class=\"linkImages\">";
or
echo '<div class="linkImages">';
echo '<div class="content">';
while($row = $stmt->fetch()){
$name = $row['name'];
$id = $row['id'];
$logo = $row['logo'];
$username = $row['username'];
echo '<div class="Links">
<div class="linkImages">
<br><img src="users/'.$username.'/images/'. $logo .'" width="200" height="auto" border="0">
</div>
<div class="linkName">
'.$name.'
</div>
</div>';
}
echo '</div>';
Here's how I would write it:
<div class="content">
<?php
while ($row = $stmt->fetch()){
echo '<div class="Links">';
echo '<div class="linkImages">';
echo '<br /><img src="users/'. $row['username'] .'/images/'. $row['logo'] .'" width="200" />';
echo '</div>';
echo '<div class="linkName">';
echo ''. $row['name'] .'';
echo '</div>';
}
?>
</div>
Note that I removed the border="0" for the img tag - that should be done with CSS.
The short answer is yes. There is almost always a cleaner or more efficient way to do it.
How about something like this?
<div class="content">
<?PHP while($row = $stmt->fetch()) { ?>
<div class="Links">
<div class="linkImages">
<br><img src="users/<?=$row['username'] ?>/images/<?=$row['logo'] ?> width="200" height="auto" border="0" />
</div>
<div class="linkName">
<a href="Profile.php?id="<?=$row['id'] ?>><?=$row['name'] ?></a>
</div>
</div>
<?PHP } ?>
</div>
There are a lot of erroneous symbols etc in this, it comes with practice, but something like this might be worth trying
<?php
while($row = $stmt->fetch()) {
$string = "";
$string .= "<div class=\"Links\">\n";
$string .= "<div class=\"linkImages\">\n";
$string .= "<br />\n";
$string .= "<img src=\"users/". $row['name'] ."/images/" . $row['logo'] . "\" width=\"200\" height=\"auto\" border=\"0\" />\n";
$string .= "</div>\n";
$string .= "<div class=\"linkName\">\n";
$string .= "". $row['name'] ."\n";
$string .= "</div>\n";
$string .= "</div>\n";
echo $string;
}
?>
I'd recommend learning how to use the printf() family of functions.
$frame = '<img src="users/%s/images/%s" width="200" height="auto" border="0"/>';
printf($frame, $id, $username, $logo);

Issue with photo echo in php, simple task

i have this code in php and it's supposed to print me the image but it isn't.
Is there any problem with this code?
Thanks!
echo "<td> <img src=foto/photo1/".$row['photo'] . "
></td>";
echo "<td> <img src=foto/photo2/".$row['photo2'] . "></td>";
You have missed the ' marks in the echo. Try this:
echo '<td> <img src="foto/photo1/'.$row['photo'].'"></td>';
echo '<td> <img src="foto/photo2/'.$row['photo2'].'"></td>';
Use single quotes ' for image src attribute.
Change your code like this
<img src='foto/photo1/".$row['photo'] ."'></td>

Categories