ankh2054 Posted May 5, 2014 Share Posted May 5, 2014 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 More sharing options...
kongondo Posted May 5, 2014 Share Posted May 5, 2014 ? U forgot to ask your question? Link to comment Share on other sites More sharing options...
k07n Posted May 5, 2014 Share Posted May 5, 2014 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 More sharing options...
Martijn Geerts Posted May 5, 2014 Share Posted May 5, 2014 $thumb->url where do you define $thumb ? Link to comment Share on other sites More sharing options...
ankh2054 Posted May 5, 2014 Author Share Posted May 5, 2014 sorry question no attached still full code Link to comment Share on other sites More sharing options...
Martijn Geerts Posted May 5, 2014 Share Posted May 5, 2014 $thumb = $image->size(142); <-- weird And if code looks cleaner, people are more willing to help. Link to comment Share on other sites More sharing options...
adrian Posted May 5, 2014 Share Posted May 5, 2014 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 More sharing options...
ankh2054 Posted May 5, 2014 Author Share Posted May 5, 2014 thanks so much for all the help. I made the code above cleaner and will update once I have implemented the final code. Link to comment Share on other sites More sharing options...
adrian Posted May 5, 2014 Share Posted May 5, 2014 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 More sharing options...
k07n Posted May 5, 2014 Share Posted May 5, 2014 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 More sharing options...
ankh2054 Posted May 6, 2014 Author Share Posted May 6, 2014 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 More sharing options...
diogo Posted May 6, 2014 Share Posted May 6, 2014 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 More sharing options...
Soma Posted May 6, 2014 Share Posted May 6, 2014 @diogo, that is dependent of if output formatting for that page is on or off. If off, it's always a WireArray. Link to comment Share on other sites More sharing options...
ankh2054 Posted May 6, 2014 Author Share Posted May 6, 2014 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 Link to comment Share on other sites More sharing options...
diogo Posted May 6, 2014 Share Posted May 6, 2014 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 More sharing options...
diogo Posted May 6, 2014 Share Posted May 6, 2014 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 More sharing options...
Soma Posted May 6, 2014 Share Posted May 6, 2014 For user page(s) it is always off I remember. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted May 6, 2014 Share Posted May 6, 2014 Its only on in template level, but an extra set to false isn't hurting. Link to comment Share on other sites More sharing options...
Soma Posted May 6, 2014 Share Posted May 6, 2014 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 More sharing options...
ankh2054 Posted May 6, 2014 Author Share Posted May 6, 2014 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 More sharing options...
Soma Posted May 6, 2014 Share Posted May 6, 2014 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 More sharing options...
ankh2054 Posted May 6, 2014 Author Share Posted May 6, 2014 thanks, Even though multiple images are stored against the field it only shows the single image using the array. Link to comment Share on other sites More sharing options...
Soma Posted May 6, 2014 Share Posted May 6, 2014 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 More sharing options...
ankh2054 Posted May 6, 2014 Author Share Posted May 6, 2014 Below is link showing the settings for the field, should I be checking anywhere else? http://www.tiikoni.com/tis/view/?id=e8d80f6 Link to comment Share on other sites More sharing options...
ankh2054 Posted May 6, 2014 Author Share Posted May 6, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now