echoing multiple image urls out of the db - php

I am trying to echo out multiple image urls from one column from the database. My echo script which echo's out the numbers of images that is coming from the db
however it can't get the main image for each image out from the folder only showing boxes that are equivalent to the db image paths this is my code.Thanks in Advance
Note:commas where used to seperate the images while inserting image path to the db
<?php
$q="praise"; //Folder path
$get_pro="SELECT * FROM `a`";
$run_pro = mysqli_query($con, $get_pro);
while($row_pro=mysqli_fetch_array($run_pro)){
$a=$row_pro['q'];
$str= $row_pro["image"];
$array = explode(',', $str);
foreach ($array as $item) {
echo "<img src=\"".$q."/".$item . "\" height=\"200\" width=\"200\"/>"; //only shows image boxes expecting images but calling from the folder is the problem
}
}
?>

try this If your image has absolute path
<?php
$q="praise"; //Folder path
$get_pro="SELECT * FROM `a`";
$run_pro = mysqli_query($con, $get_pro);
while($row_pro=mysqli_fetch_array($run_pro)){
$a=$row_pro['q'];
$str= $row_pro["image"];
$array = explode(',', $str);
foreach ($array as $item) {
// absolute path of the image
echo "<img src='".$q.DIRECTORY_SEPARATOR.$item."' height='200' width='200'/>";
}
}
?>

Do you know you have praise use two times in source as $q and in $item ? Try to compare src of image with path to image on server.

would you please try that
<?php
$q="praise"; //Folder path
$get_pro="SELECT * FROM `a`";
if($run_pro = mysqli_query($con, $get_pro)) {
while($row_pro=mysqli_fetch_assoc($run_pro)){
$a = $row_pro['q'];
$str = $row_pro["image"];
$array = explode(',', $str);
}
foreach ($array as $item) {
?>
<img src="<?php echo $q."/".$item; ?>" height="200" width="200" />
<?php
}
} else {
echo "error failed to run mysql query";
}
?>

Related

Get all img and pdf with php

I'm trying to get all images from folder with php and link them with another folder where I have pdf files. The names of both are the same.
My code looks like this:
$dir_jpg = "../wp-content/uploads/newspaper/jpg/";
$dir_pdf = "../wp-content/uploads/newspaper/pdf/";
$images = glob($dir_jpg."*.*");
$pdfs = glob($dir_pdf."*.*");
$array = array_combine($images, $pdfs);
foreach($array as $image => $pdf){
echo '<ul>';
echo '<li>';
echo '<img src="'.$image.'" height="100" width="100" />';
echo '</li>';
echo '</ul>';
}
I did var_dump to check the array, but it was empty. What could be the reason my code is not working?
Update
I store my files in a wordpress folder system, but I didn't upload them through
wordpress media, so there is no records about them in database. I want to avoid it, and upload those files through ftp account and list them with a php code.
Update 2 On chat with #Gabor Klement we have got to this
$wp_root = wp_upload_dir()['baseurl'];
$dir_jpg = $wp_root."/newspaper/jpg/";
$dir_pdf = $wp_root."/newspaper/pdf/";
$images = glob($dir_jpg."*.*");
$pdfs = array();
$imgNum = count($images);
$list = '<ul>';
for ($i = 0; $i < $imgNum; $i++) {
$filename = substr(basename($images[$i]), 0, -4);
$pdfs[$i] = $dir_pdf.$filename.".pdf";
if (file_exists($dir_pdf.$filename.".pdf")) {
$list .= '<li><img src="'.$images[$i].'" height="100" width="100" /></li>';
}
}
$list .= '</ul>';
echo $list;
but for some reason wordpress is not showing those files. Funy part is that <img src="<?php echo $dir_jpg ?>/july2012.jpg" /> placed before the $list works
Update 3
the only thing that is passing paths to $images is wp_upload_dir()['basedir']; but then the wordpress creates a path like this domain.com/home/user/domains/domain/public_html/wp-content/uploads/newspaper/jpg/december2012.jpg and the image is not found.
I found a solution for my problem. I had to use wp_upload_dir()['basedir'] to pass the path to glob, and then wp_upload_dir()['baseurl']to pass the link to src.
Maybe it not the best solution, but it works.
$base_dir = trailingslashit(wp_upload_dir()['basedir']);
$base_url = wp_upload_dir()['baseurl'];
$dir_jpg = '/newspaper/jpg/';
$dir_pdf = '/newspaper/pdf/';
$images = glob($base_dir.$dir_jpg.'*.*');
foreach($images as $image) {
$url = $base_url.$dir_jpg.basename($image);
$filename = substr(basename($image), 0, -4);
$pdfs = $base_url.$dir_pdf.$filename.".pdf";
printf('<img src="%s" alt="'.$filename.'.pdf"><div class="newspaper-hover"></div>', esc_url($url));
}

Display a multi image(array) name from database in codeigniter

My problem is how to display the array image from database. I called that array image because when I uploaded that image I'm using an array to do upload to database.
I'm using this code to insert the file name to database
public function multipost()
{
$filepath = $this->upload->get_multi_upload_data('file_name');
$filenames = '';
foreach($filepath as $a) {
$filenames .= $a['file_name'].",";
}
$filenames = rtrim($filenames,',');
$db_data = array(
'pic_id'=> NULL,
'ad_pic'=> $filenames,
);
$this->db->insert('technical_slide_img', $db_data);
}
That result to
As you can see the ad_pic column has the value of 1.jpg,2.jpg.
if I'm using like this
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode("/", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
to display that image. Is there any way to display that images? Or do I need to separate that 2 images into row?
replace
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode("/", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
with
<?php
foreach ($this->header_model->getalldata() as $row)
{
$image_arr = explode(",", $row->ad_pic);
foreach($image_arr as $image_name)
{
echo base_url().'images/'.$image_name .'<br />';
}
}
?>
As i see in your database table image is saved with comma , and you are exploding it with slash / . So i think you have to replace / with , in your explode function.
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode(",", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
<?php
$image_arr = explode(",", $data_user->image);
foreach($image_arr as $image_name)
{?>
<img style="width:5em;"src="<?php echo base_url().'uploads/'.$image_name?>" class="img-reponsive thumbnail">
<!-- echo base_url().'images/'.$image_name .'<br />'; -->
<?php }
?>

Foreach loop for fotorama

im trying to use a foreach loop with the plugin fotorama.
What im trying to do is load one half sized image for the main gallery image. Which i have working in a foreach, but i want to use a full image for the data-full tag but i cant get it to work.
This is the working code.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
</div>
this is what im trying to do.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$fullImgs = "<img data-full=".$image2." src=".$image." /><br />";
foreach($fullImgs as $fullImg) {
echo $fullImg;
}
?>
</div>
thanks in advanced guys
Try this:
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$array = array_merge($images, $images2);
// Supossing both array have same length
$length = count($images);
for($i = 0; $j = $length; $i < $length; $i++, $j++) {
echo '<img data-full=".$images2[$j]." src=".$images[$i]." /><br />';
}
I think what you want is this:
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
//assuming $images and $images2 are the same length...
foreach ($images as $k => $v) {
echo "<img data-full=".$images2[$k]." src=".$v." /><br />";
}
?>
Haven't tested, but should give the idea...
Your code won't work like this. If I understood you correctly you have the same image in big and small format in two different directories. To show them as pair you need to make a connection between those two files - because how should your script know which images belong together? You could use a database, but in your case I think it is easier to make a connection in the filename. For example, name the pictures in the directory for the small images
Image_7263.png
Image_0172.png
And so on. For the big images you simply append _BIG to the end of the name.
Then you use your foreach loop to loop through the directory for the small images. For each image in there, you append _BIG to the end of the filename and include it from the directory for the big ones.
$smallImages = glob ("/path/to/small/images/*.*");
foreach ($smallImages as $img)
{
$name = substr ($img, 0, strlen ($img)-4); //Remove the .png or .jpg
$bigImg = "/path/to/big/images/".name."_BIG.jpg"; // or whatever image type you are using
echo "<img data-full=\"".$bigImg."\" src=\"".$img."\" />
}

PHP & Lightbox (href in for loop links to last file of lightbox set)

EDITED: Debugged some of the code but issue persists:
The issue i'm having with the following code is that the link always takes me to the last image in the set. I've tried reversing the array of photo's but it had no effect. Any help would be appreciated.
Thanks.
<?php
$dir = 'pic';
$max_albums=9;
$albums = array_diff(scandir($dir),array('..', '.', 'thumbs'));
foreach ($albums as $album) {
$albumdir = $dir.'/'.$album;
$coverdir = $albumdir.'/thumbs';
$thumbs = array_diff(scandir($coverdir),array('..', '.'));
//re-index $thumbs
$thumbs = array_values($thumbs);
//1 random cover image from each album
$rnd_min = 0;
$rnd_max = count($thumbs)-1;
$rnd_i = mt_rand($rnd_min, $rnd_max);
$covers = $thumbs[$rnd_i];
//re-index $covers
echo $rnd_i.'<br>';
//populate hrefs
$photos = array_diff(scandir($albumdir),array('..', '.', 'thumbs'));
//re-index $photos
$photos = array_values($photos);
foreach ($photos as $photo) {
echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'">';
}
//display cover images
echo '<img src="'.$coverdir.'/'.$covers.'" class="img-responsive"></a>';
}
?>
hmmm try reversing scandir
$photos = array_diff(scandir($albumdir,1),array('..', '.', 'thumbs'));
Not a very elegant solution but it seems to work:
<?php
$dir = 'pic';
$max_albums=9;
$albums = array_diff(scandir($dir),array('..', '.', 'thumbs'));
foreach ($albums as $album) {
$albumdir = $dir.'/'.$album;
$coverdir = $albumdir.'/thumbs';
$thumbs = array_diff(scandir($coverdir),array('..', '.'));
//re-index $thumbs
$thumbs = array_values($thumbs);
//1 random cover image from each album
$rnd_min = 0;
$rnd_max = count($thumbs)-1;
$rnd_i = mt_rand($rnd_min, $rnd_max);
$covers = $thumbs[$rnd_i];
//populate hrefs
$photos = array_diff(scandir($albumdir),array('..', '.', 'thumbs'));
//re-index $photos
$photos = array_values($photos);
$countphoto = 0;
foreach ($photos as $photo) {
if ($countphoto==0) {
echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'">'."\n";
//display cover images
echo '<img src="'.$coverdir.'/'.$covers.'" class="img-responsive"></a>';
}
else {
echo ''."\n";
}
$countphoto++;
}
}
?>
So i solved this issue with a simple if $countphotos==0 statement within the foreach ($photos as $photo) loop, if 0, it would display the thumbnail, else it would just output anchors to the other imgs.
I've also done away with the random thumbnail as cover, and just pulled the first thumbnail of the set.
I now another issue, but I will make a separate thread for that.
Thanks!

Ordering and then setting variables

I have the following code which orders my data in mysql perfectly:
<?php
$con = mysql_connect('localhost','root','password');
mysql_select_db("users");
$pop = mysql_query("
SELECT * FROM images ORDER BY pop DESC LIMIT 9
");
while($row = mysql_fetch_array($pop))
{
echo $row['imagefile'];
echo "<br />";
}
mysql_close($con);
?>
However i would like for each "result" to then automatically be assigned as a variable. So for example "uploads/logo.png" comes out as the first "result" of the above code. I would then want this to be assigned $image_1 - so in code it would read $image_1 = "uploads/logo.png". I then want all the other 8 outputs to be assigned to variables so that $image_2 corresponds to the second output and so on. This is only a prototype. I wish for it to eventually output 100 results. Is this at all possible? Thanks a lot for your time.
Use an array.
$images = array();
while($row = mysql_fetch_array($pop))
$images[] = $row['imagefile'];
Or keep the associative array:
$imageData = array();
while($row = mysql_fetch_array($pop))
$imageData[] = $row;
// $imageData[0]['imageFile'] will be "uploads/logo.png"
Edit for comment below:
First method from above:
<?php
foreach ($images as $image) {
echo <<<EOT
<div class="box"><img src= "$image" width="200" height="150"></a></div>
EOT;
}
?>
Second method could be more involved, depending on your data:
<?php
foreach ($imageData as $image) {
$imagePath = $image['imageFile'];
$imageWidth = $image['width'];
$imageHeight = $image['height'];
echo <<<HTML
<div class="box"><img src= "$imagePath" width=$imageWidth height=$imageHeight></a></div>
HTML;
}
?>

Categories