Jump to content

Image field - if else


ankh2054
 Share

Recommended Posts

Hi all,

I am trying to create a if else statement to show a default image if no image is uploaded.

image field = avatar  (maximum files allowed set to 1)

Below is the full code now. The code below does not output "this is a test" if the image field is not populated.

 
<?php

// Run through all images and set thumbnail
foreach($user->avatar as $image) { 
    $thumb = $image->size(142); 
}

//Check if avatar field contains a value 
if (isset($thumb)){
    $avatar_thumb .= "<img class='img-rounded img-responsive' src='{$thumb->url}'' alt='{$thumb->description}'' />"; 
}
else {
    $avatar_thumb .= "This ia a test"; 
}

?>
<!--Display Avatar image--> 
<div class="user-image"> 
  <?php echo $avatar_thumb; ?> 
</div>
Link to comment
Share on other sites

else {
  $avatar_thumb .= "<img class='img-rounded img-responsive' src='{$u->avatar->url}' alt='{$u->avatar->description}' />";  
}

m?

upd:

$u = $users->get($user);

if( $ava = $u->avatar ) {
	
	$avatar_thumb = "<img class='img-rounded img-responsive' src='{$ava->url}' alt='{$ava->description}' />";
	
} else {

	$avatar_thumb = "this is a test";
	
}

mm?

Link to comment
Share on other sites

I don't understand why you are doing this:

$u = $users->get($user); 

if (count($u->avatar)){

You have already done a foreach: 

foreach($user->avatar as $image) {

to define $large and $thumb

Think about this vs using first() if the avatar field can contain more than one image (although makes sense to set it to one in this scenario I would think).

Keep in mind that right now you'd be re-setting the $large and $thumb for each image if there was more than one.

But the key thing is that you can check if they are set for your if/else statement.

if (!isset($thumb)){
    $avatar_thumb .= "this is a test"; 
}
else {
    $avatar_thumb .= "<img class='img-rounded img-responsive' src='{$thumb->url}'' alt='{$thumb->description}'' />"; 
}

Oh, and definitely try to follow Martijn's suggestion of making your code better formatted before posting, as well as trying to reduce it down to just where the problem is - I know this can sometimes be difficult but it does make it look less overwhelming for those considering helping.  :)

Link to comment
Share on other sites

Thanks for cleaning things up, although you might have gone too far - now we don't know where $u comes from :)

Actually with it cleaner I did notice something new - your check for:

if (count($u->avatar)){

If you want to go with the count approach don't you want it to be:

if (count($u->avatar) == 0){

since you are trying to find a case when there is no avatar?

Of course I think you should be using $user->avatar directly, but you get what I am saying.

Link to comment
Share on other sites

if (count($u->avatar)){                // if user HAS an avatar image
    $avatar_thumb .= "this is a test"; // do this
}
else { // else do this, but user has not an avatar, so you don't see image or text in both cases 
   $avatar_thumb .= "<img class='img-rounded img-responsive' src='{$thumb->url}'' alt='{$thumb->description}'' />";  
}

so in your code you need ... (ooh.. I'm typing too slow) what adrian said.

Link to comment
Share on other sites

I ended up going with the isset method. thanks :)  Hope the code makes more sense now. Otherwise ill update. 

isset()

From PHP manual – isset():

 

isset — Determine if a variable is set and is not NULL

 

In other words, it returns true only when the variable is not null.

Read more: http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/#ixzz30uwVmlIV 
 

Link to comment
Share on other sites

Still doesn't make sense. If the maximum files allowed is set to 1 you don't need to foreach because avatar is not an array.

Do this instead:

$avatar = $user->avatar;

if ($avatar->url){
    $thumb = $avatar->size(142);
    $avatar_thumb .= "<img class='img-rounded img-responsive' src='{$thumb->url}'' alt='{$avatar->description}'' />"; 
}
else {
    $avatar_thumb .= "This ia a test"; 
}

?>
<!--Display Avatar image--> 
<div class="user-image"> 
  <?php echo $avatar_thumb; ?> 
</div>
Link to comment
Share on other sites

I'm assuming it's not off as it would be the normal in this case.

To make it clear, this is what Soma is referring to:

https://processwire.com/api/fieldtypes/images/

*Note for advanced users: the single image behavior described above is only applicable when output formatting is ON, as it is by default in template files. When output formatting is OFF, image fields always behave as arrays. 
Link to comment
Share on other sites

thanks Diogo, I tried that and it throws the following error:

Error: Exception: Method Pageimages::size does not exist or is not callable in this context

Can you confirm if the maximum files allowed is really set to 1? Or in the situation that Soma referred, are you turning the output off in any place of the code before this?

Link to comment
Share on other sites

Well there used to be something in the past with user pages not having output formatting, but could be wrong. Just tested and it seems to be enabled (at least on my side 2.4+), so it shouldn't be an array if image max file is set to 1.

Link to comment
Share on other sites

Maximum files allowed is set to 1 on the actual image field in backend control panel.

Plus below:

// Upload avatar settings
    $upload_path = $config->paths->assets . "files/avatar_uploads/"; 
    $f = new WireUpload('avatar'); //avatar is the name of the input-file field
    $f->setMaxFiles(1);
    $f->setMaxFileSize(3*1024*1024);
    $f->setOverwrite(true);
    $f->setDestinationPath($upload_path);
    $f->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif'));

Again this might not be the right way, but I did not want users to be able to upload multiple pictures at once and overwrite original.

Link to comment
Share on other sites

Well if you upload an image though API , even if image field is set to only allow 1, it will still will add them and turn the single image into multiple (at least that's what I think), thus you have an array. That setting isn't recognized on API level when adding images. :)

Edit: Quickly tested this and added a second image through API. While the image is added to the field, the image field is still single image and not an array. So it's an Pageimage. If you get a Pageimages object, you don't have set max file to 1 or turned off output formatting somehow.

Link to comment
Share on other sites

thanks,

Even though multiple images are stored against the field it only shows the single image  using the array. 

So the image field is not max 1 file.

Link to comment
Share on other sites

I have done the following as a test and it works.

 <?php
$user->setOutputFormatting(true);
$avatar = $user->avatar;

if ($avatar->url){
    $logo = $avatar->size(20);
    $avatar_logo .= "<img class='img-rounded img-responsive' src='{$logo->url}'' alt='{$avatar->description}'' />"; 
}

?>

 <div class="user-image">
                    <?php echo $avatar_logo; ?>
                </div>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...