Retrieving the images from database and displaying in view - php

How to retrieve the images if there are multiple images in a single row. I am using this below code to get the images. if there is single image it is displaying. But, I need to display multiple images.
Here is my code:
<?php if(!empty($img['image'])) { ?>
<img src="<?php echo $img['image'];?>" width="100px" height="50px"/>
<?php }else {?>
<img src="image/data/photo/profile1.png" width="100px" height="50px"/>
<?php }?>
I used this function in view for displaying the images

<?php
if(!empty($img['image'])) {
$images = explode($img['image'],',');
foreach($images as $image)
{
print '<img src="' . $image . '" width="100px" height="50px"/>';
}
}else {
print '<img src="image/data/boutique/profile1.png" width="100px" height="50px"/>';
}
?>
or
<?php
if(!empty($img['image'])) {
$images = explode($img['image'],',');
foreach($images as $image)
{
?>
<img src="<?php print $image ?>" width="100px" height="50px"/>
<?php
}
}else {
?>
<img src="image/data/boutique/profile1.png" width="100px" height="50px"/>
<?php
}
?>

Related

How to convert a href and img to php?

I have this code
echo "<img src='" . $image[0] . "'>";
And this
<img src="images/preview-kv/1.jpg" alt="">
I need to convert this to php with dynamic image... pls
Try these. I have taken Your path and src in variable as example
$path = "/images/preview-kv/1.jpg";
$src = "images/preview-kv/1.jpg";
echo '<img src="'.$src.'" alt="">';
or
<img src="<?php echo $src ?>" alt="">
$image is an array.
Assume $image = array("/images/preview-kv/image1.png","/images/preview-kv/image2.png");
foreach($image as $image_path)
{
?>
<!--$image_value is dynamically changed-->
<img src="<?php echo $image_path; ?>" alt="">
<?php
}

How to get individual product related images in magento

I want to get each product related images which is not assign to product but want to show all related images in front-end.
<?php
foreach ($data['product'] as $product) {
$product_id = $product->getId();
$product_name = $product->getName();
$product_description = $product->getDescription();
$product_price = $product->getPrice();
$isporductImage = $product->getImage();
$product_image = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, true) . "media/catalog/product" . $product->getImage();
$isproductthumbnail = $product->getThumbnail();
$product_thumbnail = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, true) . "media/catalog/product" . $product->getThumbnail();
$collectionimage = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, true) . "media/catalog/category/thumb/" . $data['collection']['image'];
$productplaceholderImage = $this->getSkinUrl() . "whi/assets/images/whi_placeholder.png";
?>
<?php if ($isproductthumbnail != "") { ?>
<div class="image-div" style="background: url('<?php echo $product_thumbnail;?>');">
<img src="<?php echo $product_thumbnail; ?>" alt="celeb" />
</div>
<?php }elseif ($isporductImage != "") { ?>
<div class="image-div" style="background: url('<?php echo $product_image;?>');">
<img src="<?php echo $product_image; ?>" alt="celeb" />
</div>
<?php } else { ?>
<div class="image-div" style="background: url('<?php echo $productplaceholderImage;?>');">
<img src="<?php echo $productplaceholderImage ?>" alt="celeb" />
</div>
<?php } ?>
i want to show all related product images in this list.
<ul class="outfit-images-cmt">
<li><img src="<?php echo $this->getSkinUrl() ?>whi/assets/images/doll.png"></li>
<li><img src="<?php echo $this->getSkinUrl() ?>whi/assets/images/doll.png"></li>
<li><img src="<?php echo $this->getSkinUrl() ?>whi/assets/images/doll.png"></li>
<li><img src="<?php echo $this->getSkinUrl() ?>whi/assets/images/doll.png"></li>
</ul>
Try the below code for fetch all the gallery images:
$product_image_url_gallery = $product->getMediaGalleryImages();
$galleryURL = '';
echo '<ul class="outfit-images-cmt">';
foreach ($product_image_url_gallery as $image) {
$galleryImage = Mage::helper('catalog/image')->init($product, 'image', $image->getFile());
echo '<li><img src="'.$galleryURL.'"></li>'
}
echo '</ul>';

WideImage output as strange characters instead of image

I am trying to showing cropped images as thubmnails with WideImage.
I made a function with an array of filenames as parameter ($images) where I produce my html
foreach ($images as $img) {
$pathToThumb = str_replace("/upload/images", "/upload/images/thumbs", $img);
$thumb = WideImage::loadFromFile($pathToThumb)->crop('center', 'center', 150, 100)->output('jpg', 100); ?>
<div class="lingerie">
<a class="fancybox" rel="group" href="<?php print $img; ?>">
<img src="<?php echo $thumb; ?>" alt=""/>
</a>
</div>
<?php } ? ?>
For now, all I get is
����JFIF``���u i$�J�$��]5�����j4���%�o�WEʴ���^[7��|��6�d��P$g�bx�e��wh����N��c��rp=Oz��h�F�Z��"��u8q�oN��la_28�g�����1��2G��1^Uy�EJ��'��|��9F[����Vn��2m�j�i��ַߣ,4���? ����2zp3�P[|���>���1���8$u��+cQ��:x���-�II^r1��{U�+O��T�x��*b��3����zv�m �)b�U��j�+'B�`m������a����T��r���Tq�1�*�����{D_s�!AbG;v��������/i5��u�ݶ]�����M�]�]���#$Z���74"+ar]�-�',�B���J��W�}KY�/7H#��7�3-n�=���v�Ps�3�C~��N��h��j�q��6�����- and so on
What is the reason for these weird digits?

if image exists show image else show different image

I have this code in my php file.
<div class="the-avatar">
<div class="flash"></div>
<div class="avatar">
<img src="avatar/default-avatar.jpg" alt="">
</div>
I want to do this:
if file upload/user_avatar.jpg exist
show upload/user_avatar.jpg
else
show avatar/default-avatar.jpg
Save it to a dB if you ask me, checking everytime seems awkward.
Anyways,
$filename = 'upload/user_avatar.jpg';
if (file_exists($filename)) {
echo '<img src="'. $filename .'" alt="" />';
} else {
echo '<img src="avatar/default-avatar.jpg" alt="" />';
}
Straight from http://nl3.php.net/file_exists
You can use
<?php
if (file_exists('upload/user_avatar.jpg')) {
echo "<img src='upload/user_avatar.jpg'>";
} else {
echo "<img src='avatar/default-avatar.jpg'>";
}
?>
Your question shows no effort of research, so it is off-topic, though, here's the solution.
<div class="the-avatar">
<div class="flash"></div>
<div class="avatar">
<img src="<?php
If (file_exists('upload/user_avatar.jpg')) {
echo 'upload/user_avatar.jpg';
} else {
echo 'upload/default_avatar.jpg';
}
?>" alt="">
</div>

Loading array from database in HREF

I want to load images from my database that if someone clicks on it brings them to another page but instead it just shows $r[n]
Here is the code:
<?php
$cn=mysql_connect('localhost', 'root', '');
mysql_select_db('db', $cn);
$query="SELECT * FROM tblg";
$res=mysql_query($query,$cn);
$r = mysql_fetch_array($res);
?>
<?php
if ((isset($_GET['guide'])))
{
$guide=$_GET['guide'];
}
else
{
$guide="";
}
?>
<IMG SRC='$r[a]' WIDTH="120" HEIGHT="120" ALT="$r[n]">
<?php
if ($guide == "t")
{
include('t.php');
}
elseif ($guide == "a")
{
include("a.php");
}
?>
Change
<IMG SRC='$r[a]' WIDTH="120" HEIGHT="120" ALT="$r[n]">
to
<IMG SRC='<?php echo $r[a]; ?>' WIDTH="120" HEIGHT="120" ALT="<?php echo $r[n]; ?>">
change
<IMG SRC='$r[a]' WIDTH="120" HEIGHT="120" ALT="$r[n]">
into
<img src='<?php echo $r[a]; ?>' width="120" height="120" alt="<?php echo $r[n]; ?>">
Note that using caps in your code is not really a good way of coding

Categories