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

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 }
?>

Related

echoing multiple image urls out of the db

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";
}
?>

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!

foreach inside foreach issue

I have two directories : one has images and the other has ZIP files.
The files in both directories have the same names e.g : 1.zip , 1.png
I scanned each folder like this :
$images = 'screenshots/';
$scanned_images = array_diff(scandir($images), array('..', '.'));
$zips = 'download/';
$scanned_zips = array_diff(scandir($zips), array('..', '.'));
then :
foreach ($scanned_images as $value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'" rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
foreach ($scanned_zips as $val)
{
echo '<div class="portfolioDescription">';
echo'<h4>Download:'.$val.'</h4>';
echo'<p>Click here to download</p>';
echo'</div></div>';
}
}
This does not work. Each image in first directory will have the whole zip files of second directory in its description!
I have also tried to combine the two arrays into one array and use foreach ($result as list ($a, $b)) but as list always gives an error.
How to overcome this ?
no need to have nested foreach.
use this:
<?php
$images = 'screenshots/';
$scanned_images = array_diff(scandir($images), array('..', '.'));
$zips = 'download/';
$scanned_zips = array_diff(scandir($zips), array('..', '.'));
foreach ($scanned_images as $value)
{
$name = substr($value, 0, strrpos($value, '.'));
$pos = array_search($name.'.zip', $scanned_zips);
if($pos != null){
echo '<div class="portfolioItem">';
echo '<a href="'.$images.$value.'" class="zoom img" title="'.$value.'" rel="portfolio">';
echo '<img src="'.$images.$value.'" class="portfolio-image" alt="'.$value.'" />';
echo '</a>';
echo '<div class="portfolioDescription">';
echo '<h4>Download:'.$scanned_zips[$pos].'</h4>';
echo '<p>Click here to download</p>';
echo '</div>';
echo '</div>';
}
}
Add a break; statement at the end of your inner foreach loop and that should fix it. You have two foreach loops and hence the downloads are listed multiple times. To fix this issue,
Change your code to:
<?php
foreach ($scanned_images as $value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'" rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
foreach ($scanned_zips as $val)
{
echo '<div class="portfolioDescription">';
echo'<h4>Download:'.$val.'</h4>';
echo'<p>Click here to download</p>';
echo'</div></div>';
break; //exiting
}
}
?>
One way is to hash your files by name without extension. Then use same key to retrieve image data and zip data. Example:
$scanned_images = array('1.png', '2.png');
$scanned_zips = array('1.zip', '2.zip');
//Should be like that after hashing
$scanned_images = array('1' => '1.png', '2' => '2.png');
$scanned_zips = array('1' => '1.zip', '2' => '2.zip');
So the code could be:
function get_file_name($path) {
$name = basename($path);
$name = substr($name, 0, strrpos($name, '.'));
return $name;
}
function hash_files_by_name($items) {
$hashed = array();
foreach($items as $item) {
$name = get_file_name($item);
$hashed[$name] = $item;
}
return $hashed;
}
$scanned_images = array('1.png', '2.png'); // get images directory filesnames
$scanned_zips = array('1.zip', '2.zip'); // get zips directory filenames.
$imgs = hash_files_by_name($scanned_images);
$zips = hash_files_by_name($scanned_zips);
foreach ($imgs as $key=>$value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'" rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
if(isset($zips[$key])) {
echo '<div class="portfolioDescription">';
echo'<h4>Download:'.$zips[$key].'</h4>';
echo'<p>Click here to download</p>';
echo'</div></div>';
}
}

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;
}
?>

Echo php array value

I have the following code that reads the content of a folder
PHP Code:
<?
//PHP SCRIPT: getimages.php
header('content-type: application/x-javascript');
//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnimages($dirname="./images") {
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo 'galleryarray['.$curimage.']="'.$file .'";' . "\n";
$curimage++;
}
}
closedir($handle);
}
sort($files);
return($files);
}
echo 'var galleryarray=new Array();' . "\n"; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
?>
which spits out this the following code:
var galleryarray = new Array();
galleryarray[0] = "image1.jpg";
galleryarray[1] = "image5.jpg";
galleryarray[2] = "image2.jpg";
galleryarray[3] = "image4.jpg";
galleryarray[4] = "image8.jpg";
galleryarray[5] = "image7.jpg";
galleryarray[6] = "image0.jpg";
galleryarray[7] = "image3.jpg";
galleryarray[8] = "image6.jpg";​
Now in my html file I want to echo something like
<img src="images/image0.jpg" />
<img src="images/image1.jpg"/>
...
...
How can I do that?
foreach($galleryarray as $img) {
echo "<img src='images/$img' />";
}
maybe this help u
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
if($handle = opendir("./images")) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ ?>
<img src="/images/<?php echo $file; ?>">
<?php
}
}
}
assuming that you want to sort image name by value and then show the images. you first need to sort values by using function "asort"
asort($galleryarray);
foreach ($galleryarray as $key => $val) {
echo "<img src='images/{$val}'/>"
}
if you want to print and array
1) print_r($array); or if you want nicely formatted array then
echo '<pre>'; print_r($array); echo '<pre/>';
2) use var_dump($array) to get more information of the content in the array like datatype and length.
3) you can loop the array using php's foreach(); and get the desired output. more info on foreach in php's documentation website http://in3.php.net/manual/en/control-structures.foreach.php
Try changing the echo line in the function to this:
echo '<img src="images/' . $file . '" />' ;
Hope this will help :)
OR
Outside the function, use it like this:
$images = returnimages(); //will get the array containing the images
foreach($images as $img)
{
echo '<img src="images/' . $img . '" />';
}
Also, you have to include this line inside the if condition in the while loop:
$files[] = $file; //insert the file into the array. This array is what we are going to return from the function
Update
I have tested this code and it's working:
//PHP SCRIPT: getimages.php
header('content-type: application/x-javascript');
function returnimages($dirname="./images") {
$pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"; // http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression/
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(preg_match($pattern, $file)){ //if this file is a valid image
$files[] = $file;
}
}
closedir($handle);
}
//sort($files); // http://php.net/manual/en/function.sort.php
natcasesort($files); // case insensitive "natural order" algorithm :: http://php.net/manual/en/function.natcasesort.php
return($files);
}
$images = returnimages(); //will get the array containing the images
foreach($images as $img)
{
echo '<img src="images/' . $img . '" />';
}
In my images folder, I have put 5 files for testing. And upon running it, it would give the following:
<img src="images/a.jpg" />
<img src="images/ba.jpg" />
<img src="images/ca.jpg" />
<img src="images/Copy (3) of a.jpg" />
<img src="images/Copy (4) of a.jpg" />

Categories