MyBB Community Forums

Full Version: How to get Control Panel to save choices?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Perhaps this belongs in the Code Modifications forum, but I will go ahead and post it here. It could be both.

Anyway, I don't like how MyBB sticks all custom profile fields into the same table, so I am editing few templates to my liking. This topic deals with the "User Control Panel Templates > usercp_profile_profilefields" template.

The problem is, when a user goes to /usercp.php?action=profile, fills out the custom profile fields and hits the "Update Profile" button, although their profile updates, the Control Panel doesn't remember their selections for the next time they choose to update their profile. In other words, their profile updates, but their Control Panel doesn't remember these updates. Make sense? Undecided

I'm relatively positive I simply need to make a small code change to the <select> and <input> tags (such as adding value="some sort of variable" or whatnot), but I don't know what it is.

Would anyone be willing to help? Here's a simplified portion of what I have (I deleted what was already there):

<br />

<fieldset class="trow2">
<legend><strong>Stats</strong></legend>
<table cellspacing="0" cellpadding="4" width="100%">

<tr>
<td>
<span class="smalltext"><a title="Your rank." style="text-decoration: none;">Rank</a>:</span>
</td>
</tr>
<tr>
<td>
<select name="profile_fields[fid4]" size="1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></select>
</td>
</tr>

<tr>
<td>
<span class="smalltext"><a title="Your location." style="text-decoration: none;">Location</a>:</span>
</td>
</tr>
<tr>
<td>
<input type="text" name="profile_fields[fid5]" class="textbox" size="0" maxlength="50" />
</td>
</tr>

</table>
</fieldset>

Thanks. Cool
I think you've made a mistake here, Jed K.

The usercp_profile_profilefields template uses "{$customfields}", which is actually the usercp_profile_customfield template, to display the custom profile fields. The values are hard coded into it.

From your post, are you adding these custom fields straight into the _profilefields template?
There's no mistake, it's just hard to explain.

I already created the custom profile fields, but I am hardcoding them into usercp_profile_profilefields (instead of calling {$customfields}) because I do not like the way MyBB lumps all custom profile fields together under "Additional Information". By hardcoding them into usercp_profile_profilefields I can organize them into different <fieldset> tables.

Hopefully that makes a bit more sense.

With the code in my original post everything works as far as updating user profiles (/user-1.html, etc.), but the Control Panel does not remember the values/information users chose/typed for the next time they choose to update their profile. It's as if they never filled anything out.

I need to figure out how to make my code retain this information. I'm pretty sure I need to add a value="$some sort of MyBB variable" to the <input> tag and perhaps a selected="$some sort of MyBB variable" to the <option> tag. I'm just not sure what those variables are. Or perhaps I'm on the wrong track completely.

Sorry for the confusion. Hopefully you can help me figure this out.

Thanks.

Cool
You 'might' be able to do this for an input tag...

value="{$mybb->user['fidX']}"

... where X is the ID of the profile field.
Thanks, Matt. That worked for the <input> tag. Cool

Now how about the <select> tag? The majority of my custom profile fields are <select>. Undecided I tried using the same method as the <input> tag, but it didn't work. I also tried...
selected="{$mybb->user['fidX']}"
...but no luck.

Here's hoping you have an idea...

Angel
It won't work with the select tag. Select's is like this for the option you want for default:

selected="selected"

You'll need to do it in PHP.
OK, so how can I go about doing it in PHP inside that template? I have Yumi's "PHP and Template Conditionals" plugin activated, so I'm assuming it's possible?

I would really appreciate some guidance.

Thanks.
I've never used it, so I have no idea what it's capable of I'm afraid. I can give you the PHP code and hopefully you can figure it out!

if($memprofile['fid4'] == 1)
{
     $value1 = " selected=\"selected\"";
}
elseif($memprofile['fid4'] == 2)
{
     $value2 = " selected=\"selected\"";
}
// [... continue on for the length of the select]

Then, your select will need to look like this:

<select name="profile_fields[fid4]" size="1">
<option value="1"{$value1}>1</option>
<option value="2"{$value2}>2</option>
[...]
</select>

So, if the profile field is "2", then option 2 should be selected. At least, that's the easiest way I can explain it really...

Smile
Perhaps it's asking a lot, but could you possibly give it a quick look? There's no documentation on how to use the plugin as far as I can see, so I'm not entirely sure what the command is for an elseif.
http://community.mybboard.net/thread-31860-page-1.html

Also, that code you provided will become quite long if there are 20 choices for a custom field. If I can get it to work, of course I will do it; however, in the usercp.php, the code for the <select> custom field is quite short:
		elseif($type == "select")
		{
			$expoptions = explode("\n", $options);
			if(is_array($expoptions))
			{
				foreach($expoptions as $key => $val)
				{
					$val = trim($val);
					$val = str_replace("\n", "\\n", $val);
					$sel = "";
					if($val == $userfield)
					{
						$sel = " selected=\"selected\"";
					}
					$select .= "<option value=\"$val\"$sel>$val</option>";
				}
				if(!$profilefield['length'])
				{
					$profilefield['length'] = 1;
				}
				$code = "<select name=\"profile_fields[$field]\" size=\"{$profilefield['length']}\">$select</select>";
			}
		}

Can I not use something similar to that? Or is that not all of it?

I'm not really sure how to go about this; I simply wish to find the most efficient way.

Thank you!
Looking at it, there isn't the capacity for a loop or a switch statement with the plugin.

You'll need to either do a loop before the template is eval'd in the PHP file, or go with one big if statement...
Pages: 1 2