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..../////
}
?>
Related
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.
I've used the below code to store and get the images from the backend. The images are getting stored but it is not getting converted to base64 format...
$fruit_img = addslashes($_FILES['fruit_img']['tmp_name']);
$name = addslashes($_FILES['fruit_img']['name']);
$fruit_img = file_get_contents($fruit_img);
$fruit_img = base64_encode($image);
while($row = mysqli_fetch_array($res))
{
echo '<img height ="300" width="300" src="data:image/jpg;base64,'.$row[2].' "> ';
echo $row[0];
}
I presume your code represents storing the data here:
$fruit_img = addslashes($_FILES['fruit_img']['tmp_name']);
$name = addslashes($_FILES['fruit_img']['name']);
$fruit_img = file_get_contents($fruit_img);
$fruit_img = base64_encode($image);
And retrieving the data here:
while($row = mysqli_fetch_array($res))
{
echo '<img height ="300" width="300" src="data:image/jpg;base64,'.$row[2].' "> ';
echo $row[0];
}
If that is the case, you have simply mislabeled a variable in this line:
$fruit_img = base64_encode($image);
$image has not been assigned any value previously.
$fruit_img = base64_encode($fruit_img);
If that is not the case, please post additional (complete insert and read) 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 would like to display multiple images on my index page. Doesn’t matter if they are displayed in any order.
Each of the images have their image path and their unique ID. I have the following code but it is not displaying anything:
<?php
$result = mysql_query("SELECT * FROM my_image", $connection);
while($row = mysql_fetch_array($result))
{
$ID = $row['name'];
$file = $row['file_path'] ;
echo '<img style="margin: -32.5px 0 0 0;" alt="" src="'.$file.'">' ;
}
?>
Anyone know what could be wrong?
you should start to debug your code, as example :
while($row = mysql_fetch_array($result))
{
$file = $row['file_path'] ;
echo $file; continue;
//...
}
and see if you get expected result.
EDIT:
what about var_dump($result) ?
I am trying to .load a script called 'refreshImages.php'. Inside that script is a while loop pulling from the database. I have got it to load a single echo function but it wont load anything inside the while loop I have on the script... this is what the php file has...
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid='$pid'");
//Cant Post Images So Leaving The Echo Content Out//
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo "<li>$img</li>";
}?>
I am using .load('refreshImages.php') on the page I need it to show up on.
Any explanation I am not seeing?
Your $img is an array, not a string. You will get output like <li>Array</li>, if you have stuff coming from the database. Is that what you mean? Or are you getting an empty result?
If empty - what does your mysql_num_rows tell you when ran against the result resource?
try changing this:
echo "<li>$img</li>";
to
echo "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
You may not be getting any results from the database. Try using this code which will display a message if there is something wrong with your sql query.
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid=" . $pid );
if (mysql_num_rows($pimages) > 0) { // checks to see if you are getting results from db
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo '<li><a class="thumb" href="{$image}"><img src="{$image}" width="50px" height="50px" alt="{$image_alt}"></a></li>';
}
} else {
echo "no results returned from database";
} // end of mysql_num_rows check
?>
You might be better off concatenating all the images and then echo-ing it out rather than echo-ing each one e.g
$htmlOutput = '';
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
$htmlOutput .= "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
}
echo $htmlOutput ;