I'm new to PHP and I know this is an easy one but still can't find a solution.
I have an array that holds 4 different images and I want to echo the results
so my HTML looks like this:
<img src="<?php echo $results['image1']?>" >
<img src="<?php echo $results['image2']?>" >
<img src="<?php echo $results['image3']?>" >
<img src="<?php echo $results['image4']?>" >
But what if for example image4 is null? - i don't want to echo the whole
<img src> tag line
How can i do it?
Thanks?
Here is your work around example.
<?php
if(!empty($results['image1'])) echo '<img src="'.$results['image1'].'">';
if(!empty($results['image2'])) echo '<img src="'.$results['image2'].'">';
if(!empty($results['image3'])) echo '<img src="'.$results['image3'].'">';
if(!empty($results['image4'])) echo '<img src="'.$results['image4'].'">';
?>
try this code may it helps you. here i have check first if result not empty.
<?php if(isset($results['image1']) && !empty($results['image1'])){ ?><img src="<?php echo htmlspecialchars($results['image1']}; ?>" ><?php } ?>
<?php if(isset($results['image2']) && !empty($results['image2'])){ ?><img src="<?php echo htmlspecialchars($results['image2']}; ?>" ><?php } ?>
<?php if(isset($results['image3']) && !empty($results['image3'])){ ?><img src="<?php echo htmlspecialchars($results['image3']}; ?>" ><?php } ?>
<?php if(isset($results['image4']) && !empty($results['image4'])){ ?><img src="<?php echo htmlspecialchars($results['image4']}; ?>" ><?php } ?>
<?if(isset($results['image1']) && $results['image1'] != ""){?><img src="<?=$results['image1']?>" ><?}?>
With this you check it the image is set. Do that for each of your images and you should be fine.
The isset is not necessary, but some setups of php throw a warning without it. Thus it's better practice to use it.
If you don't want all the short-tags, do it like this:
if(isset($results['image1']) && $results['image1'] != ""){echo "<img src='{$results['image1']}' >"; }
That should be about the fanciest you can do. the "x{$variable}y" works like "x".$variable."y" but is easier to write/read, if you like it.
Related
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
I have this following code
<td>
<?php if($rec->telegram){ ?><img class="social" src="<?php echo base_url(); ?>social/telegram.jpg" /><?php } ?>
<?php if($rec->google_pluse){ ?><img class="social" src="<?php echo base_url(); ?>social/g.jpg" /><?php } ?>
<?php if($rec->instagram){ ?><img class="social" src="<?php echo base_url(); ?>social/insta.jpg" /><?php } ?>
<?php if($rec->facebook){ ?><img class="social" src="<?php echo base_url(); ?>social/f.jpg" /><?php } ?>
</td>
I would like to add "else" into this code that if ALL those icons facebook, instagram, google plus and facebook weren't available then it will show another image file like "nophoto.jpg"
Note: If all those icons (4 icons) weren't available then it will show nophoto.jpg
I highly appreciate if someone guide me how to add else into above code.
Regards
It'll be easier to just add an extra if:
if (!($rec->telegram || $rec->instagram || $rec->facebook || $rec->google_plus)) { //etc
But really you want to attach that kind of logic to the $rec object, so that it's neatly separated and it's easier to add new types of social media in the future. Hopefully your $rec object is indeed backed by a class and not just a stdClass from a database result or cast.
public function hasNoSocialIcon() {
return !($rec->telegram || $rec->instagram || $rec->facebook || $rec->google_plus);
}
And then use that in your template:
<?php if ($rec->hasNoSocialIcon()) { ?>
<img class="social" src="<?php echo base_url(); ?>social/nophoto.jpg" />
<?php } ?>
You should take the use of PHP's conditional operators like this:
<?php
$icons_available = (
!empty($rec->telegram) &&
!empty($rec->google_plus) &&
!empty($rec->instagram) &&
!empty($rec->facebook)
);
?>
<td>
<?php if(!$icons_available) { ?>
<a href="default-pic.jpg">
<img class="social" src="default-pic.jpg" />
</a>
<?php } else { ?>
<?php $url = echo base_url() . 'social/telegram.jpg'; ?>
<a href="<?php echo $url; ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/google_plus.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/instagram.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/facebook.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php } ?>
</td>
Hope this helps!
foreach($photos as $photo) {
$lastChild = "";
if( $count%3 == 0 && $count != 0 )
echo "</tr><tr>";
$count++;
?> <td>
<a href="<?php echo ($photo['source'])?>" title="<?php echo ($photo['name'])?>">
<img id="thumb" class="thumb" src="<?php echo ($photo['picture'])?>">
</a></td>
This is the script for draggable. Ive done this but it still does not work
only the first one
does the foreach loop affects?
$thumb.draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
helper: "clone",
});
im unable to set ui-draggable to all the img src other than the first img.
any one can help?
Use
<img class="thumb" src="<?php echo ($photo['picture'])?>">
instead of
<img id="thumb" class="thumb" src="<?php echo ($photo['picture']); ?>" />
in your PHP and
$('.thumb').draggable({
instead of
$thumb.draggable({
in your jQuery. This should fix it.
If you absolutely need IDs on the images for any reason, make them unique like this:
<img id="thumb-<?php echo $count; ?>" class="thumb" src="<?php echo ($photo['picture'])?>">
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)
Consider the following :
$img_pathm = JURI::root().'images/properties/images/'.$this->datos->id.'/';
$peque_path = JURI::root().'images/properties/images/thumbs/'.$this->datos->id.'/';
$result = count($this->Images);
$resultf = $result-1;
while ($resultf>=0){ ?>
<span class="editlinktip hasTip" title="<?php echo $this->datos->image1;?>::
<img border="1" src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" name="imagelib" alt="<?php echo JText::_( 'No preview available'.$img_pathm ); ?>" width="206" height="100" />">
<img src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" alt="Additional image <?php echo $resultf+1 ?>" width="65px" height="50px"/></span> <?php
$resultf--; }
This currently prints images one after the other. All I need to do is to invert the order in which these images are printed to the user. I am not sure where, or how I would insert something similar to ORDER by in this code? Thanks in advance!
Just loop the other way:
$img_pathm = JURI::root().'images/properties/images/'.$this->datos->id.'/';
$peque_path = JURI::root().'images/properties/images/thumbs/'.$this->datos->id.'/';
$result = count($this->Images);
$resultf = 0;
while ($resultf<$result){ ?>
<span class="editlinktip hasTip" title="<?php echo $this->datos->image1;?>::
<img border="1" src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" name="imagelib" alt="<?php echo JText::_( 'No preview available'.$img_pathm ); ?>" width="206" height="100" />">
<img src="<?php echo $peque_path.$this->Images[$resultf]->name; ?>" alt="Additional image <?php echo $resultf+1 ?>" width="65px" height="50px"/></span> <?php
$resultf++; }
Maybe you can use array_reverse