CakePHP: Showing images in view.ctp - php

Goal:
I am trying to show an image in the view.ctp. I am trying to grab the image that was saved with the rest of the information. I am pretty sure the code is 100% correct in the ProductsController, if not that'll be posted next.
Here is the code in the view.ctp for products:
<h1>Viewing Product</h1>
<div><b>Name:</b> <?php echo $Product['Product']['name']; ?></div>
<div>
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
</div>
<div><b>Description:</b> <?php echo $Product['Product']['description']; ?></div>
<div><b>Price:</b> <?php echo $Product['Product']['price']; ?></div>

Read down a bit to see the actual solution.
Shoudln't
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
be:
<img src="<?php echo $path .'/'. $Product['Product']['filename']; ?> " width="300px" />
?
Since the original poster actually answered their own question I thought I'd elaborate.
The Solution:
Replacing:
<img src="<?php echo $path .'/'. $row['Product']['filename']; ?> " width="300px" />
with:
<?php echo $this->Html->image('$Product['Product']['filename']') ?>

Related

image slider when empty image

i use this template for my image slider "https://www.jssor.com/demos/image-gallery.slider"
i try to change a image if image1 is empty(NULL) to image2, there is my sample code i have try but doesnt work.
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:980px;height:550px;overflow:hidden;">
<?php
if (!empty ($upload_dir.$data['image1'])){
?>
<div>
<img data-u="image" src="<?php echo $upload_dir.$data['image1']; ?>" />
<img data-u="thumb" src="<?php echo $upload_dir.$data['image1']; ?>" />
</div>
<?php
}else{
?>
<div>
<img data-u="image" src="<?php echo $upload_dir.$data['image2']; ?>" />
<img data-u="thumb" src="<?php echo $upload_dir.$data['image2']; ?>" />
</div>
<?php
}
?>
this code is work when image1 is able on databases, but when it null the image is not change as image2
It's because your $upload_dir is not empty so $upload_dir.$data['image1'] would not be empty.
Try changing the condition to:
if (!empty($data['image1'])) {
...
}

unable to fetch image from the database

I am trying to display images on my web page, the content is getting fetched from my database, but the issue I'm facing is in displaying the image. please, can anyone guide me how should I display the image?
I mean to say the path what I should give
here 'image' is my column name and this is my view
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/');$image?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
Hope this will help you :
Use this : <img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
The whole code should be like this :
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
If $image is the column name(which contains image name) then Replace
<img src="<?php echo base_url('uploads/');$image?>" alt="">
with
<img src="<?php echo base_url();?>uploads/<?php echo $row->image;?>" alt="">
If $image has image path then remove ';' and add '.' on echo base_url('uploads/');$image?> line
You issue here is the following code;
<?php echo base_url('uploads/');$image?>
This is because you are not concatenating the string, rather, just saying it's variable name, so, use of the following will provide the output;
<?php echo base_url('uploads/') . $image' ?>
This replaces the semi-colon part way through for a period (.) which is the PHP concatenation operator
Obviously, there is the issue you are not setting $image, which would require (before usage) of;
$image = $row['image_column_name'];
// Or
$image = $row->img_column_name

PHP concatenation for wordpress homepage img src

I've tried my best to looking for other questions similar to mine, but couldn't find any.
I have a php code to show for my homepage only but am having a hard time concatenating this correctly so that the img src php can work properly inside the php tags. I'm still new to php and this is confusing me a bit. Could anyone show me the correct way?
<?php
if ( is_front_page() ) {
echo '
<div id="demo">
<img alt="background" src="<?php echo get_template_directory_uri(); ?>/images/img1.png"/>
<div id="block" ></div>
<img alt="logo" src="<?php echo get_template_directory_uri(); ?>/images/img2.png"/>
</div>'; }
?>
You don't need PHP tags inside anymore since its already inside your PHP script. Just concatenate that function you're calling.
if ( is_front_page() ) {
echo '
<div id="demo">
<img alt="background" src="' . get_template_directory_uri() .'/images/img1.png"/>
<div id="block" ></div>
<img alt="logo" src="' .get_template_directory_uri() . '/images/img2.png"/>
</div>
';
}
If you want to use separate tags than try like this.
<?php if ( is_front_page() ) { ?>
<div id="demo">
<img alt="background" src="<?php echo get_template_directory_uri(); ?>/images/img1.png"/>
<div id="block" ></div>
<img alt="logo" src="<?php echo get_template_directory_uri(); ?>/images/img2.png"/>
</div>
<?php } ?>
You don't need a php tag anymore.
<?php
if (is_front_page()){
echo "<div id='demo'> <img alt='background' src='".get_template_directory_uri()."/images/img1.png'/> <div id='block' ></div> <img alt='logo' src='".get_template_directory_uri()."/images/img2.png'/> </div>";
}
?>

Joomla 3.1 the full article image to be clickable (to self image url)

I find this com_content/views/article/tmpl/default.php I created a template override and here is the code:
<?php if ($params->get('access-view')):?>
<?php if (isset($images->image_fulltext) && !empty($images->image_fulltext)) : ?>
<?php $imgfloat = (empty($images->float_fulltext)) ? $params->get('float_fulltext') : $images->float_fulltext; ?>
<div class="pull-<?php echo htmlspecialchars($imgfloat); ?> item-image">
<img
<?php if ($images->image_fulltext_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_fulltext_caption) . '"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_fulltext); ?>" alt="<?php echo htmlspecialchars($images->image_fulltext_alt); ?>"/> </div>
<?php endif; ?>
Before img tag how to form, modify this php code to be clickable the image to self url:
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>"> <img
Here is the resolution:
<a href="<?php echo JURI::root().$images->image_fulltext; ?>">
Now the image is "clickalble" to self url
JURI::root() > your webpage url (like: something.com)
$images->image_fulltext > in joomla 3.1 you can choose an image what is displayed when you click to readmore, ie when you read the full article, that image have an url, like: /images/stories/freshsite/something.jpg
This php code combines these two code, to be: something.com/images/stories/freshsite/something.jpg > when you click on the image, only this image will shows in you browser :)

Output image src with SimpleXML

I'm trying to output an image with SimpleXML, but the image tag doesn't appear in the source code.
Can anyone help me outpout this image:
Here's my XML and code:
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->location; ?></p>
</a>
</li>
<?php } ?>
Your issue is here:
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
You're trying to access url as though it were an attribute, you need to access it as a child element by using ->url instead.
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
EDIT: By the way, == !null works as you expect, but != null is a bit friendlier and less confusing
Your if statement is incorrect. It should be:
if ($event->Media['url'] != null)

Categories