html code inside php if statement - php

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.

Related

php echo image with single and double quotes doesn't not work

How do I get this example echo image to work single and double?
data-mean="<?php
echo
'<div>' . $k["auto_ans"][$i] . '</div>' .
'<div><img src=' . $k["auto_image"][$i] . '></div>' .
'<div><img src=' . $k["auto_image2"][$i] . '></div>'; ?>"
I want it to output:
data-mean="<div>AAAA</div><div><img src='aaa.jpg'></div><div><img src='bbb.jpg></div>"
You should put quotes around SRC prop like this :
data-mean="<?php
echo
'<div>' . $k["auto_ans"][$i] . '</div>' .
'<div><img src=\'' . $k["auto_image"][$i] . '\'></div>' .
'<div><img src=\'' . $k["auto_image2"][$i] . '\'></div>'; ?>"

List files in iframe

I need to show files that I have in a listing files and directories in mi < iframe >
This is my listinf files code:
$directorioInicial = "./";
$rep = opendir($directorioInicial);
echo "<ul>";
while ($todosArchivos = readdir($rep)) {
if ($todosArchivos != '..' && $todosArchivos != '.' && $todosArchivos != '') {
echo "<li>";
echo "<a href=" . $directorioInicial . "/" . $todosArchivos . " target='_blank'>" . $todosArchivos . "</a><br />";
echo "</li>";
}
}
closedir($rep);
clearstatcache();
echo "< /ul>";
I need to do click in the file that I show in my list and the file will be show in my frame, but I dont know how...At the momento I show the file in another page... But it's not what I need... Thank you...
This is my frame:
<iframe id="probando" src="<?php echo $url; ?>" scrolling="auto" height="700" width="800" marginheight="0" marginwidth="0" name="probando"></iframe>
Name a iframe and set that name in target attribute of the hyperlink.
Try this :
$directorioInicial = "./";
$rep = opendir($directorioInicial);
echo "<ul>";
while ($todosArchivos = readdir($rep)) {
if ($todosArchivos != '..' && $todosArchivos != '.' && $todosArchivos != '') {
echo "<li>";
echo "<a href=" . $directorioInicial . "/" . $todosArchivos . " target='probando'>" . $todosArchivos . "</a><br />";
echo "</li>";
}
}
closedir($rep);
clearstatcache();
echo "< /ul>";
<iframe id="probando" src="<?php echo $url; ?>" scrolling="auto" height="700" width="800" marginheight="0" marginwidth="0" name="probando"></iframe>
More : http://www.w3schools.com/html/html_iframe.asp

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]"; ?>');">

php pass parameter issue

This my code:
<?php
$lijstDoelmannen = mysql_query("SELECT * FROM Speler WHERE positie = 'Doelman' ORDER BY familienaam, voornaam");
$teller = 1;
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
The problem is that in the hyperlink the parameter spelerId = spaces (not filled in). If I echo $rij["id"], it gives me the right value.
You have a ' in the wrong spot in your href.
"...<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">..."
This should be:
"...<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>..."
<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">
You need to move the apostrophe:
<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>
It's currently ending the link, before the variable is added.
You can also do:
echo "<td><a href='spelerDetail.php?spelerId={$rij['id']}'
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
I prefer writing the above code this way to avid these types of issues:
while($rij = mysql_fetch_array($lijstDoelmannen)){
if($teller < 5){ ?>
<td><a href="spelerDetail.php?spelerId=<?php echo $rij['id'] ?>">
<img src="images/spelers/unknown.png" alt="" width="50" />
<br /><br /><?php echo $rij['id'] . " " . $rij['familienaam'] . " " . $rij['voornaam'] ?></a></td>
<?php }} ?>

Concatenating In 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'].'>';

Categories