Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder: Plugins & Add-ons:
Membership plugin - author specific photo

 

 


Gabums85
User

Jan 27, 2012, 2:45 PM

Post #1 of 16 (1331 views)
Shortcut
Membership plugin - author specific photo Can't Post

Hello all,

I was hoping someone could help me out here. I have added a photo uploader under user accounts so that way, when a user posts something, their image will show up next to their post. I found this code

Code
<?php echo $record['createdBy.fullname'] ?>

that works perfect for adding the users name that posted the information but I can't seem to get anything to work for images.

I've tried

Code
<?php foreach ($record['avatar'] as $upload): ?> 
<?php echo $upload['createdBy.avatar'] ?>
<?php endforeach ?>


But that doesn't work.. No image shows up at all...I've tried...

Quote
<?php list(list($current_user_with_uploads),) = getRecords(array('tableName' => 'accounts', 'where' => mysql_escapef('num = ?', $CURRENT_USER['num']), 'allowSearch' => false)); ?>
<?php if (sizeof(@$current_user_with_uploads['avatar'])): $upload = $current_user_with_uploads['avatar'][0] ?>
<img src="<?php echo $upload['urlPath'] ?>" />
<?php else: ?>
<img src="images/fpo_avatar.jpg" width="75" height="75" />
<?php endif ?>


Where I get an image (my image) but it shows up for EVERY post. I'm assuming because it is using "current_user" commands and I'm the one signed in. Is there anything I can do to make this work?

Thanks!
Gab


gkornbluth
Veteran

Jan 28, 2012, 10:04 AM

Post #2 of 16 (1322 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

Hi Gab,

Don't have a lot of time to work this through right now, but if you’ve added an avatar upload field in the accounts section then it would seem that you could:

1) Use an if statement to match the createdByUserNum on the post with your user account record numbers, and
2) If there’s an avatar image uploaded in that user's account record, show the avatar, if not show your placeholder image.

Just a thought...

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com


Gabums85
User

Jan 30, 2012, 8:01 AM

Post #3 of 16 (1309 views)
Shortcut
Re: [gkornbluth] Membership plugin - author specific photo [In reply to] Can't Post

Hi Jerry,

Thanks for your reply. I've tried to figure out your suggestions but I feel so lost. I've tried this:


Quote
<?php foreach ($accountsRecords as $record): ?>
<?php foreach ($record['avatar'] as $upload) {
if (mysql_count('uploads', "tableName = 'accountsRecords' AND fieldName = 'uploads' AND recordNum = '".mysql_escape($record['num'])."'")) {
print '<img src='<?php echo $upload['urlPath'] ?>' width="75" height="75" />';
} else {
print '<img src="images/avatar_fpo.jpg" width="75" height="75" />';
}
?>


But I can't even get the code to be error free. The bolded text always has that red error in Dreamweaver that tells you your not doing something right... Any other thoughts would be greatly appreciated.
Thanks!


gkornbluth
Veteran

Jan 30, 2012, 8:59 AM

Post #4 of 16 (1302 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

Hi Gab,

I'm going out for a few hours but when I return I'll take a look.

Jerry
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com


Jason
Staff / Moderator


Jan 30, 2012, 10:00 AM

Post #5 of 16 (1292 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

Hi,

If "avatar" is a field in your accounts table, you can access it through createdBy.avatar. You shouldn't need any extra queries.

For example:


Code
  <?php foreach ($newsRecords as $record) :?> 

<?php foreach ($record['createdBy.avatar'] as $upload): ?>
//output image here.
<?php endforeach ?>

<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


Gabums85
User

Jan 30, 2012, 10:41 AM

Post #6 of 16 (1291 views)
Shortcut
Re: [Jason] Membership plugin - author specific photo [In reply to] Can't Post

Hi Jason,

Thank you so much for your reply. I was hoping all I would need is code like that...but I cannot seem to get it to work. When I use this:


Code
 <?php foreach ($accountsRecords['createdBy.avatar'] as $upload): ?>  
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<? php endforeach ?>


I get this error:

Quote
Notice: Undefined index: createdBy.avatar in /home/ovation2/public_html/mriintranet/index.php on line 68 Warning: Invalid argument supplied for foreach() in /home/ovation2/public_html/mriintranet/index.php on line 68


When I try this:

Code
<?php foreach ($accountsRecords as $record) :?>  
<?php foreach ($record['createdBy.avatar'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php endforeach ?>
<?php endforeach ?>


I get the same error. Any thoughts?


Jason
Staff / Moderator


Jan 30, 2012, 10:45 AM

Post #7 of 16 (1290 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

Hi,

Are accountsRecords coming from the accounts table? If so, you shouldn't need the "createdBy.avatar" field, you should be able to use $record['avatar'].

Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


Gabums85
User

Jan 30, 2012, 11:02 AM

Post #8 of 16 (1289 views)
Shortcut
Re: [Jason] Membership plugin - author specific photo [In reply to] Can't Post

Hmmm... still not working but no error message this time. If I look at the page in firebug it looks like it cannot find the uploaded image. If I do an if statement like so:


Code
<?php foreach ($accountsRecord['avatar'] as $upload): ?> 
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php endforeach ?>
<?php if (!$accountsRecord['avatar']): ?>
<img src="images/fpo_avatar.jpg" width="75" height="75" />
<?php endif ?>


The fpo image does show up where the uploaded image should...

I'm going to try setting up the image in a different table and see if that makes a difference. I'll let you know what happens. Thank you!


Gabums85
User

Jan 30, 2012, 12:08 PM

Post #9 of 16 (1284 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

So I tried the new table and it still wont work. Every time I try adding 'createdBy', I get the "undefined index" error :(


Tom P
User


Jan 30, 2012, 3:01 PM

Post #10 of 16 (1260 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

Hi Gabums85,

CMSB has a great helper function to show you what the contents of an array are.

If you try

Code
<?php foreach ($accountsRecords as $record) :?>  
<?php showme($record); ?>
<?php die(); ?>
<?php foreach ($record['createdBy.avatar'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php endforeach ?>
<?php endforeach ?>


You should see a detailed output of the array that is $record (and only the first record as we call die() immediately after showing the output).

Check through the keys to that array (the keys are in the square brackets) and see if createdBy.avatar is set at all. If not, try finding the expected data in another field. If you find it, simply swap out the 'createdBy.avatar' key with the new one. If the data is being retrieved at all then the problem lies in the way that accountsRecords is being created.

Hope that helps,

Tom


Gabums85
User

Jan 30, 2012, 3:43 PM

Post #11 of 16 (1256 views)
Shortcut
Re: [Tom P] Membership plugin - author specific photo [In reply to] Can't Post

Wow...pretty neat! So I tested out the helper function for both the Accounts Table and the New Blog Image Table I created. I was a bit confused at the fact that when I ran the test for each one, there was a createdBy.avatar for both. I originally set up the Account Table to have the upload name of "avatar" and the Blog Image Table to have the upload name of "upload_image". But the "createdBy.upload_image" never came up. So I changed that to "avatar" as well. Here are the results...

For this code:

Code
<?php foreach ($accountsRecords as $record) :?>   
<?php showme($record); ?>
<?php die(); ?>
<?php foreach ($record['createdBy.avatar'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php endforeach ?>
<?php endforeach ?>

i got this:

Code
[createdBy.avatar] => Array ( )


for this code:

Code
<?php foreach ($blog_avatarsRecords as $record) :?>   
<?php showme($record); ?>
<?php die(); ?>
<?php foreach ($record['createdBy.avatar'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php endforeach ?>
<?php endforeach ?>


i got a huge string of information. But most notably it linked to my image (yay). So I changed the code to read as follows:

Code
<?php foreach ($blog_avatarsRecord['createdBy.avatar'] as $upload): ?>   
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php endforeach ?>
<?php if (!$blog_avatarsRecord['createdBy.avatar']): ?>
<img src="images/fpo_avatar.jpg" width="75" height="75" />
<?php endif ?>


but it shows up for EVERY posting. Not solely the one that I created. I wanted the "fpo" image to populate if the user has not uploaded their own image yet...

I can't tell you all how much I appreciate you everyone hanging in there with me for this and trying to help out. I feel like we are getting close(ish) and just wanted to cheer you on a bit longer :)
Thank you so much!


Tom P
User


Jan 30, 2012, 3:54 PM

Post #12 of 16 (1252 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

Hi Gabums85,

You're very close withg your code. Try this instead:


Code
<?php foreach ($blog_avatarsRecord['createdBy.avatar'] as $upload): ?>   
<?php if ( @$upload ) : ?>
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php else : ?>
<img src="images/fpo_avatar.jpg" width="75" height="75" />
<?php endif; ?>
<?php endforeach ?>


Basically, you want to check for the existence of the avatr while in the for loop, as the output of the img url happens in the loop.

Hope that helps,

Tom


gkornbluth
Veteran

Jan 30, 2012, 4:09 PM

Post #13 of 16 (1252 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

Hi Gab,

In the code that you're using, shouldn't the logic be something like:

Code
<?php foreach ($blog_avatarsRecords as $record) :?>  
<?php if ($record['createdBy.avatar']): ?>
<?php foreach ($record['createdBy.avatar'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php endforeach ?>
<?php else : ?>
<img src="images/fpo_avatar.jpg" width="75" height="75" />
<?php endif ?>
<?php endforeach ?>


Also, instead of urlPath, why not use thumbUrlPath and set the thumbnail sizes to 75 x 75 in the editor?

Using:

Code
<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo 
$upload['thumbHeight'] ?>

you'll get to display a small thumbnail image to begin with and not get distorted images if the aspect ratio of the original is not exactly square.

Jerry
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com

(This post was edited by gkornbluth on Jan 30, 2012, 4:10 PM)


Gabums85
User

Jan 31, 2012, 8:33 AM

Post #14 of 16 (1236 views)
Shortcut
Re: [gkornbluth] Membership plugin - author specific photo [In reply to] Can't Post

Hey everyone,

Ok I got it working kinda but there is some funky stuff going on. I'm going to attach a stripped down file to see if maybe that will help. So when I did the error testing again that Tom recommended, I noticed that the uploaded images from the Blog_Avatars table won't work unless there is an avatar upload form in the Accounts table as well... So now I have them both up and both with images downloaded to the tables. Anyways, using this code:


Code
<?php if ($record['createdBy.avatar']): ?>  
<?php foreach ($blog_avatarsRecord['avatar'] as $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" />
<?php endforeach ?>
<?php else : ?>
<img src="images/fpo_avatar.jpg" width="75" height="75" />
<?php endif ?>


Causes the last image that has been added to the manager to be the one that shows up when there is an image created by the user. So my boss uploaded her image after I had, and now for my posts, her image comes up as well as on her own posts. The else statement works. This is the closest I have gotten with every variation of the code I can think of...

Thanks again everyone! You're lifesavers :)
Attachments: index_test.php (4.57 KB)


Jason
Staff / Moderator


Jan 31, 2012, 9:29 AM

Post #15 of 16 (1236 views)
Shortcut
Re: [Gabums85] Membership plugin - author specific photo [In reply to] Can't Post

Hi,

You're really close. If the avatar is supposed to be an image associated with the person who created the record, you won't need this extra avatar table. In your code you are limiting the query to only retrieve a single record from the blog_avatars table, which is why it is repeating. You need the code to output the createdBy.avatar field, as this will be the image uploaded to the avatar field of the accounts table for the user that created the record.

try this:


Code
<?php if ($record['createdBy.avatar']): ?>  
<?php foreach ($record['createdBy.avatar'] as $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" />
<?php endforeach ?>
<?php else : ?>
<img src="images/fpo_avatar.jpg" width="75" height="75" />
<?php endif ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


Gabums85
User

Jan 31, 2012, 11:22 AM

Post #16 of 16 (1231 views)
Shortcut
Re: [Jason] Membership plugin - author specific photo [In reply to] Can't Post

It worked! But alas, I was informed that I have to have the code to pull from the Blog Avatar table and not the account manager... ::sigh::

::EDIT::

So as much as I would love to take credit for this being solved... my boss is the one who figured out the code :)

Here it is if anyone wants to know:


Code
<?php $post_author = $record['createdBy.username'] ?> 

<!-- SET VARIABLE TO DETERMINE IF USER UPLOADED AN IMAGE -->
<?php $avatar_match = 0; ?>

<!-- LOAD AVATAR -->
<?php foreach ($blog_avatarsRecords as $record): ?>
<?php if ($record['name'] == $post_author): ?>
<?php foreach ($record['avatar'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="75" height="75" />
<?php endforeach ?>
<?php $avatar_match +=1; ?>
<?php endif ?>
<?php endforeach ?>

<!-- IF NO RECORDS MATCH, LOAD GENERIC IMAGE -->
<?php if ($avatar_match == 0): ?>
<img src="images/fpo_avatar.jpg" width="75" height="75" />
<?php endif ?>


Thank you very much to everyone who has been helping me through this the last few days. Hopefully the above code helps others too.
Gab


(This post was edited by Gabums85 on Jan 31, 2012, 2:02 PM)