I am trying to write a script that displays images and/or mp4 files. The image and mp4 file names are stroed in a data table.
When the script runs it needs to loop through the array returned by the mySQL statement. Each returned record has a column called "FileType" which contains either a 1 for an image file or 2 for an mp4 file.
My thinking is that as the do while loop is executed it looks at the content of "FileType" it is trapped by the if statement and uses the appropriate code to display the file. The issue I have is the script is only
displaing rows where the column "FileType" contains 1.
So if the first row contains an image file, it displays, but if the row contains an mp4 file it is not displayed.
Can anyone see where I have gone wrong and how I can fix it so it loops through the array of rows and displays each one.
My code:
do {
$FileType = $row_signage['FileType'];
if($row['DisplayType'] == "P" ) {
$DisplayWdith = "1080";
$DisplayHeight = "1920";
} else {
$DisplayWdith = "1920";
$DisplayHeight = "1080";
}
?>
<div>
<?php if($FileType == 1) { ?>
<img src="/<?=$ImagePath . $row_signage['SignageImageName']?>" class="responsive"/>
<?php } elseif($FileType == 2) { ?>
<div id="video_player">
<video width="<?php echo $DisplayWdith;?>" height="<?php echo $DisplayHeight;?>" autoplay muted playsinline>
<source src="/<?php echo $ImagePath.''.$row['SignageImageName'];?>"/>
</video>
<?php } ?>
</div>
} while ($row_signage = mysqli_fetch_array($fb)); ?>
Code rewrite and working:
<?php while($row = $fb->fetch_array()):
$FileType = $row['FileType'];
if($row['DisplayType'] == "P" ) {
$DisplayWdith = "1080";
$DisplayHeight = "1920";
} else {
$DisplayWdith = "1920";
$DisplayHeight = "1080";
}
?>
<div class="banner-container" data-delay-time="<?=$row['TimeLapse']?>">
<div>
<a>
<?php if($FileType == 1) { ?>
<img src="/<?=$ImagePath . $row['SignageImageName']?>" class="responsive"/>
<?php } else { ?>
<video id="video_player" width="<?php echo $DisplayWdith;?>" height="<?php echo $DisplayHeight;?>" autoplay muted playsinline>
<source src="/<?php echo $ImagePath.''.$row['SignageImageName'];?>"/>
</video>
<?php } ?>
</a>
</div>
</div>
<?php endwhile ?>
Related
How do I properly echo images combining HTML and PHP? $offer['picture'] has link saved as example.com/picture.png. I've tried many different options, but nothing works. Can anyone help me out?
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
?>
<img src="<?php echo $image ?>">
<?php
}
If there is only example.com/picture.png in $offer['picture'], problem is that images linked incorrectly. You should add http:// before image link to make browser sure you are loading image by absolute path.
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
?>
<img src="http://<?php echo $image ?>">
<?php
}
Well it depends on your directory structure.
suppose your images are in example.com/assets/images/photo-1.jpg
your code should be
<?php foreach($json['offers'] as $offer) {
$image = $offer['picture']; ?>
<img src="http://www.example.com/assets/images/<?php echo $image; ?>">
<?php } ?>
In fact, you need to concatenate or hard code the path and image name will be appended dynamically.
Try this syntax,
<?php
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
echo "<img src='$image' />";
}
?>
My script uploads the selected pictures to a folder but in my other page I have to see it. But I can't see my uploaded picture.
But if I manually drag and drop the picture from the desktop to the folder then I can see the picture on the page.
This is my code where I want show the uploaded picture:
$image = "../images/accommodatie/".$row2['acco_id']."/";
$images = glob($image."*.jpg");
sort($images);
if (count($images) > 0) {
$img = $images[0];
$img = str_replace("../","", $img);
echo "<a href='acco.php'>
<div>
<div>
<img src='$img'>
</div>";
}
I checked a few times, The folder location is correct, so that can not be the problem.
You're missing and closing double quote and a semi colon after your echo :
if (count($images) > 0) {
$img = $images[0];
$img = str_replace("../","", $img);
echo "<a href='acco.php'>
<div>
<div>
<img src='$img'>
</div>"; //<----- SEMI COLON HERE
}
I'm trying to show an image (or rather a link to an image) stored in a database and I'd like to get the image to show only if the link is set in the database.
Currently, if the link is not set (value is null), it shows a broken link.
Is there a way to for example use an if-statement and echo a HTML-code?
Something like this:
(The value have been fecthed to array $current in this example:)
<?php
if(isset($current['image']) {
echo "<img src='<?php echo $current['image'];
?>' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'})">
You used <?php twice, you have problem with quotes, brackets, etc.
<?php
if (!empty($current['image'])) {
echo "<img src='" . $current['image'] . "' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'>";
} else {
// here you can write for example default no-image image or whatever yo want, if you want
}
Nevermind, got it.
-Solution:
<?php if(isset($current['image'])): ?><img src="<?php echo $current['image']; ?>" class="left" style="max-height:20em; max-width:15em; margin-right:1em; margin-top:0;})">
<?php endif; ?>
<?php
if(isset($current['image'])) {
?>
<img src='<?php echo $current['image'];?>' class='left' style='max-height:20em; max-width:15em;
margin-right:1em; margin-top:0;'>
<?php
}
?>
I am pretty new to PHP, but I understand the basics. My problem is I have I have an index page that needs to have a list of the names of videos stored in a different folder. Since there will be over 100 videos in the folder by the time the site is live, I want the video names to be clickable, and when clicked the video plays.
<?php
$videopath = 'videos';
$videoExts = array('webm'=> 'video/webm','mp4'=>'video/mp4','mpeg'=>'video/mp4','ogv'=>'video/ogv');
$directory = "/videos";
$phpfiles = glob($directory . "*.html");
if ($handle = opendir($videopath)) {
while (false !== ($file = readdir($handle))) {
$info =pathinfo($file);
$ext = strtolower($info['extension']);
echo $file . " \n " . "\n <br> \n";
if (array_key_exists($ext, $videoExts)) {
?>
<div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
<video>
<source type="<?php echo $videoExts[$ext]; ?>" src="<?php echo "$videopath/$file"; ?>">
</video>
</div>
<?php
}
}
closedir($handle);
} else {
echo "no dir";
}
?>
This code will list the video name (in text), and play each video. What can I do to modify this so that the video names become clickable and once clicked open the appropriate video?
Replace:
<div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
<video>
<source type="<?php echo $videoExts[$ext]; ?>" src="<?php echo "$videopath/$file"; ?>">
</video>
</div>
By:
<?php echo $file; ?>
And in page2.php
<?php
$type = $_GET['type'];
$src = $_GET['src'];
if (file_exists($src)) { ?>
<div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
<video>
<source type="<?php echo $type; ?>" src="<?php echo $src; ?>">
</video>
</div>
<?php } ?>
I would do the following (it involves a bit of jQuery). The idea is to have a list of videos and one single video player. Once you click one of them, the video player will be updated.
Steps:
List a link of videos, using the HTML5 data attribute to store the source and extension information of each video. So when clicked, the only video player will be updated with the new settings.
$videopath = 'videos';
$videoExts = array('webm'=> 'video/webm','mp4'=>'video/mp4','mpeg'=>'video/mp4','ogv'=>'video/ogv');
$directory = "/videos";
$phpfiles = glob($directory . "*.html");
// Stores first video of the list
$first_video = array();
if ($handle = opendir($videopath)) {
$counter = 0;
while (false !== ($file = readdir($handle))) {
$info =pathinfo($file);
$ext = strtolower($info['extension']);
echo $file . " \n " . "\n <br> \n";
if (array_key_exists($ext, $videoExts)) {
if ($counter == 1) {
// Save settings of first video
$first_vid['extension'] = $videoExts[$ext];
$first_vid['path'] = $videopath .'/'. $file;
// Save it in array
$first_video[] = $first_vid;
}
?>
<a class="video-item" href="#" data-type="<?php echo $videoExts[$ext]; ?>" data-src="<?php echo "$videopath/$file"; ?>"><?php echo "$videopath/$file"; ?></a><br/>
<?php
}
}
closedir($handle);
} else {
echo "no dir";
}
?>
<div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
<video>
<source type="<?php echo $first_video[0]['extension']; ?>" src="<?php echo $first_video[0]['path']; ?>">
</video>
</div>
<script>
// Don't forget to add jQuery in your project for this to work :)
$(function () {
$('.video-item').click(function (e) {
// do nothing
e.preventDefault();
// get info of video being clicked
var extension = $(this).attr("data-type");
var src = $(this).attr("data-src");
// update video player with clicked video
$('.flowplayer video').attr('type', extension);
$('.flowplayer video').attr('src', src);
});
});
</script>
This is a untested code. Hope it helps though.
I'm using jQueryZoom that need two images to be displayed like this (one image for the 'a' tag and another for the 'img'):
<a href="images/BIGIMAGE.JPG">
<img src="images/SMALLIMAGE.JPG">
</a>
But I need bring those images from two diferents directories from MySQL (the default directory: 'zoom' and the directory: 'normal'. So I'm trying this on my HTML body:
<?php if ( $imagePath = $results02['adaptador']->getImagePathFrontend() && $imagePathNormal = $results02['adaptador']->getImagePathFrontend( IMG_TYPE_NORMAL ) ) { ?>
<a href="<?php echo $imagePath ?>" rel='gal1' id="demo1" >
<img src="<?php echo $imagePathNormal ?>" />
</a>
<?php } ?>
It's bringing me just the second one. In the first one it's returns a true value (1). This is the code after load the page on the browser:
<a href="1" rel='gal1' id="demo1" >
<img src="../../images/produtos/adaptadores/normal/Adaptador 2P.png" />
</a>
Why the 'if' is returning true on the first option instead of the image path like in the second one?
You have to group the assignments separately. Right now you're assigning to $imagePath the result of a boolean operation.
if ( ($imagePath = $results02['adaptador']->getImagePathFrontend()) &&
($imagePathNormal = $results02['adaptador']->getImagePathFrontend( IMG_TYPE_NORMAL )) )
{
// ...
}
Either set image path outside or put parenthesis around it. your order of operations is probably confusing you
<?php
$imagePath = $results02['adaptador']->getImagePathFrontend();
if ( $imagePath && $imagePathNormal = $results02['adaptador']->getImagePathFrontend( IMG_TYPE_NORMAL ) ) { ?>
<a href="<?php echo $imagePath ?>" rel='gal1' id="demo1" ><img src="<?php echo $imagePathNormal ?>" /></a>
<?php } ?>