What I am trying to do is something like below:
The buttons at the side can be ignored. So the idea is that the big square is the main video playing and the little ones underneath will be the thumbnails of the remaining videos. The main video playing is randomly picked when the site is loaded and then the ones that are not playing, the thumbnails will be shown underneath. I am doing this like so:
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
echo $video;
array_push($items, $video);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $items[0];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
foreach($restVideos as $videoThumbnail) {
try {
$stmt = $dbconn->query("SELECT thumbnail, videoTitle FROM videos WHERE video = '$videoThumbnail'");
while($row = $stmt->fetch()){
$thumbnail = $row['thumbnail'];
array_push($thumbnails, $thumbnail);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
}
foreach($thumbnails as $nonPlaying) {
?>
<img src=<?php echo $nonPlaying ?> id="thumbnails" width="200" height="100">
<?php } ?>
what is happening at the moment is below:
Each image is just stacking up against each other how can I change it so this is not the case?
Also is there a more efficient way of doing this than what I am currently doing?
Edit:
$thumbnails = array();
$videoTitles = array();
$test = 700;
echo "<div>";
foreach($restVideos as $rVideo) {
$test = 200 + $test;
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100" style= "left: <?php echo $test?>px">
<?php
echo "</div>";
}
You not required to two query two times to db .
Use key value and store the entire video. While printing thumbnail based on the key (unique id ex. videoid). You can retrieve the content when you print the thumbnail list and can avoid querying again in a loop.
Ref to code snipp
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoMap[$row['videoid']] = $row;
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
if (count($items) == 0) echo $video;
array_push($items, $row['videoid']);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $videoMap[$items[0]]['video'];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
echo "<div>";
foreach($restVideos as $rVideo) {
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100">
<?php
}
echo "</div>";
}
?>
Use a div tag and print the image thumbnails.
Related
I'm trying to display images from database but unable to show .
Please help me.
My code
$id = $_GET['reg_id'];
$sql13 = "select * from contacts where reg_id=" . $id;
$result13 = mysqli_query($conn, $sql13);
if (mysqli_num_rows($result13) > 0) {
while($documents = mysqli_fetch_array($result13))
{ ?>
<li class="make_text1" style="font-size:16px">
<span class="definition"><b>
<?php if(!empty($documents["name"])){ echo
$documents["name"]; }?><br><?php
if(!empty($documents["image"])){
$upload_dir = 'uploads/';
// echo "<img src='uploads/".$documents["image"]."' width='800' height='500'> ";
echo "<img src='uploads/".$documents["reg_id"]."/".$documents["image"]."' width='800' height='500'> ";
}
You need to loop over the values in $documents["image"]. Try this:
$images = explode(',', $documents["image"]);
foreach ($images as $image) {
echo "<img src='uploads/".$documents["reg_id"]."/$image' width='800' height='500'> ";
}
or if you only want do display one of them e.g. the first, something like this:
$images = explode(',', $documents["image"]);
echo "<img src='uploads/".$documents["reg_id"]."/".$images[0]."' width='800' height='500'> ";
I am having problem with displaying default image. I do not know where exactly to place it.
$result = $conn->query("SELECT * FROM adoption;");
if($result->num_rows !=NULL){
while($rows = $result->fetch_assoc()) {
$AAnimalName = $rows['AAnimalName'];
$Abreed = $rows['Abreed'];
$Asex = $rows['Asex'];
$Acolor = $rows['Acolor'];
$image = $rows ['image'];
?>
<div class="container-custom1">
<?php echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?>
<?php echo "<i><h1 class='junction'><a style='cursor:pointer' class='junction'>".$AAnimalName."</a></h1></i>"."<br>".$Asex." /".$Abreed."<br>".$Acolor."<br>"?></div>
<?php
}
}
try this inside while loop :
$image_location = "admin/function/upload/".$image;
if(file_exists($image_location )) {
echo '<img src = "'.$image_location .'" width = "248" height="190" class="age1" title>';
}
else {
echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>';
}
Change the line like this:
<?php echo '<img onerror="this.src=\'img/logo.png\'" src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?>
img/logo.png would be the default image
Try this:
If(file_exist('your_file_path'))
{
echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>';
}
else
{
echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>';
}
This will check if the file exist or not. If exist, it will display given file. Otherwise a default file.
$image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ;
Use ternary operator and set image
<?php
$image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ;
$image_location = "admin/function/upload/".$image;
echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>';
?>
This is a bit confusing and hard to explain but I will try my best to explain it.
Basically, I have a directory of images (test) which holds all the images for my products.
The images look like this:
999999999Image1.jpg
999999999Image2.jpg
999999999Image3.jpg
999999999Image4.jpg
999999999Image5.jpg
555555555Image6.jpg
555555555Image7.jpg
555555555Image8.jpg
555555555Image9.jpg
555555555Image10.jpg
etc etc...
The numbers before Image is a field in MYSQL database called STOCK.
Now, I'm trying to use glob() to display the images for each item in mysql database like so:
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2 .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2 = '';
}
}
/////rest of my code..../////
}
However, when I run my code, I get a very strange output...
The issue that I have is that with each output of MYSQL result, the images that being found by glob() is being added to the next result too! So, its like the image of the first result are being added to the next result and then the images of first result and the second results are being added to the third result and so on so forth...
I hope this makes sense and someone could point me in the right direction.
Any help would be appreciated.
I suggest:
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
$pic_list2 = ''; // you need this line also here !! only change
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2 .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2 = '';
}
}
/////rest of my code..../////
}
What about this? This creates for each mysql output a new array key.
<?php
$i = 0;
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2[$i] .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2[$i] = '';
}
}
$i++;
/////rest of my code..../////
}
?>
I am trying to build a web page that has 6 rows of images. Each row has 4 images and each image has a caption right below it. So it goes something like this:
IMAGE IMAGE IMAGE IMAGE
TEXT TEXT TEXT TEXT
IMAGE IMAGE IMAGE IMAGE
TEXT TEXT TEXT TEXT
and so on.
This is my code:
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
while ($rows = $resultSet->fetch_assoc()) {
$image = $rows["image"];
$text = $rows["text"];
echo "<img class=images src=$image> <p class=texts>$text</p>";
}
}
?>
Both classes are set to display: inline-block. The code prints out each image next to the text. The text should be below the image. I am trying to think of ways of displaying the images and the texts in the proper format, but I can't seem to think of any solutions at the moment. Anyone can give me some insight?
Set only the wrapper class with the display: inline-block rule
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
while ($rows = $resultSet->fetch_assoc()) {
$image = $rows["image"];
$text = $rows["text"];
echo "<div class='wrapper'>";
echo "<img class=images src=$image> </br> <p class=texts>$text</p>";
echo "</div>";
}
}
?>
You should be able to achieve this using divs.
The following code will add in a row ever
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
$count = 0;
while ($rows = $resultSet->fetch_assoc()) {
$num_in_row = 4; // Number of items you want in each row
if($count % $num_in_row == 0){
echo '<div class="row">'; // if the row already has 4 items, add a new row.
}
$image = $rows["image"];
$text = $rows["text"];
echo "<span class='wrapper'>";
echo '<img class="images" src='.$image.'> <div class="texts">'.$text.'</p>';
echo "</span>";
if($count % $num_in_row == ($num_in_row-1)){
echo '</tr>';
}
$count++;
}
}
?>
That should give you a result that looks like the jsfiddle
I have working code, which pulls all images from a dynamically created folder. But I want to display only 1 image from a particular folder in my display page. Any suggestions?
My code:
<?php
$search_dir = "$directory/{$row['name']}{$row['hotel_address']}";
$images = glob("$search_dir/*.jpg");
sort($images);
//display images
foreach ($images as $img) {
echo "<img src='$img' height='150' width='150' /> ";
}
?>
You can display a single image with:
<?php
$search_dir = "$directory/{$row['name']}{$row['hotel_address']}";
$images = glob("$search_dir/*.jpg");
sort($images);
// Image selection and display:
//display first image
if (count($images) > 0) { // make sure at least one image exists
$img = $images[0]; // first image
echo "<img src='$img' height='150' width='150' /> ";
} else {
// possibly display a placeholder image?
}
?>
If you want a random image, do this:
// Image selection and display:
//display random image
if (count($images) > 0) { // make sure at least one image exists
// Get a random index in the array with rand(min, max) which is inclusive
$randomImageIndex = rand(0, count($images)-1);
$img = $images[$randomImageIndex]; // random image
echo "<img src='$img' height='150' width='150' /> ";
} else {
// possibly display a placeholder image
}
You can use current to get the first image from the array.
<?php
$search_dir = "$directory/{$row['name']}{$row['hotel_address']}";
$images = glob("$search_dir/*.jpg");
sort($images);
//display one image:
echo "<img src='current($images)' height='150' width='150' /> ";
?>