Add New Profile Fields without Custom Profile Fields

Started by Blues Light, June 24, 2012, 04:51:15 PM

Previous topic - Next topic

Blues Light

Hello all!

If I wanted to add a new field to my members' profiles, but for the sake of argument I didn't want to do it through the Custom Profile Fields but rather actually edit the php files myself and make it as much a default portion of the profile as Posts, how would I best do this?

I can already add an entry to the SQL database, but I need it to correspond to a profile field.

Any help would be amazing!

Arantor

I'd really do it via the custom profile fields, seeing how you'd need to make multiple edits in the code in several places (Load.php, Profile-Modify.php, Profile.template.php, possibly Display.template.php if you wanted to show it on threads) and that's before you change the DB schema.

Why don't you want to use the custom profile fields?

Blues Light

My aim is to have some RPG stats for a portion of my members that want it, so I intended to have, say, an HP field in the profile with a default value of, say, 100. They could purchase an item through SMF Shop to increase that total to, say, 110 or 200 or whatever.
I could do this in the same manner as increasing post count, but connecting it to a custom field has driven be mad for the last week or so, so I surmised that if I could do it with post count, I should be able to add new fields similar to post count, yet only modifiable via those RPG items.


The things I do for my people @.@. If you have any suggestions or would like more information, by all means let me know.

Arantor

Seriously, set it up with a regular field, visible by regular users, editable by admin. That alone will save you so much work in terms of displaying it.

All you need to do to then update that field is find the relevant row in smf_themes (it'll have the same id_member as the person concerned, its 'variable' will be cust_ followed by the first 8 characters of the field name), and update it.

You won't be able to just update it in a simple update query because the value is held in a text field, but in terms of code change, it's much easier not to mess with all the various pieces of code when you can just keep it solely in your RPG code to update that DB value. (Or in your case a shop item, but you get my point: this way you only have to write a custom shop item as opposed to mulching a lot of code elsewhere that is not designed to be messed with)

Blues Light

This is probably the dumbest reply I could make, but I don't seem to have the table smf_themes.
I have smf_forumcustom_fields, and that holds the individual fields I've created, but it doesn't appear to hold the data that the fields hold.

I'm missing something aren't I?

Arantor

smf_ is the default prefix for tables, but in your case the prefix is smf_forum, so look for smf_forumthemes.

Blues Light

Ok, that clears up a good bit of my confusion, although I should have been smart enough to think it through for myself.

Before I derp further, here is my _themes table at current.
hxxp:i96.photobucket.com/albums/l165/hobbitkid37/screenshot1.png [nonactive]

I would be needing to reference the cust_attack, cust_health, cust_speed, and cust_ki.

The item I'm creating is patterned after the Add to Post Count Item in SMF Shop, and currently looks like this:
hxxp:i96.photobucket.com/albums/l165/hobbitkid37/screensho21.png [nonactive]

I'm not terribly knowledgeable in PHP, just enough to get myself into trouble.

Arantor

That's the right table, yes.

As I sort of hinted, that wouldn't work all that well, unfortunately. The query is completely different.

The code would better be set as:
$request = $smcFunc['db_query']('', '
  SELECT value
  FROM {db_prefix}themes
  WHERE id_member = {int:id}
    AND variable = {string:var}',
  array(
    'id' => $context['user']['id'],
    'var' => 'cust_health',
  )
);
if ($smcFunc['db_num_rows']($request) == 0)
{
  // There's no value for this user! Insert a default of 50 or something...
  $smcFunc['db_insert']('replace',
    '{db_prefix}themes',
    array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string', 'value' => 'string'),
    array($context['user']['id'], 1, 'cust_health', 50),
    array('id_member', 'id_theme', 'variable')
  );
}
else
{
  list($value) = $smcFunc['db_fetch_row']($request);
  $value += $item_info[1];
  $smcFunc['db_query']('', '
    UPDATE {db_prefix}themes
    SET value = {string:value}
    WHERE id_member = {int:id}
      AND variable = {string:var}',
    array(
      'value' => $value,
      'id' => $context['user']['id'],
      'var' => 'cust_health',
    )
  );
}

Blues Light

Works absolutely perfectly :3

Thank you so much for taking the time to help me.

Blues Light

Excuse me for bumping the thread, but I do have a follow-up question.

The above script works perfectly for items adding value to individual stats for my players, but what would I need to alter to have an item add value to two stats?

Arantor

Copy the code so there's two lots of code and just change the second one.

Blues Light

Sorry, I've taken time off from this and have come back to it. Copy the whole lot or a specific bit? I've gotten it to increase 2 fields, but it increases them by 1 defined amount. I'd rather be able to set it so an item could increase 2 or 3 profile fields with individually set amounts.

Arantor

Well, if one lot of code updates one value, two lots of the code can do two different values - if on the second time you change the field it's pointing to, instead of cust_health...

Blues Light

Here's what I've come up with. I still can't create a field for a second value, although if I set the value for cust_health to be, say, 100, then it will apply to cust_attack as well.


<?php
/**********************************************************************************
* SMFShop item - Takes additional fields when adding to admin panel               *
***********************************************************************************
* SMFShop: Shop MOD for Simple Machines Forum                                     *
* =============================================================================== *
* Software Version:           SMFShop 3.1 (Build 14)                              *
* Software by:                DanSoft Australia (http://www.dansoftaustralia.net/)*
* Copyright 2009 by:          vbgamer45 (http://www.smfhacks.com)                 *
* Copyright 2005-2007 by:     DanSoft Australia (http://www.dansoftaustralia.net/)*
* Support, News, Updates at:  http://www.dansoftaustralia.net/                    *
*                                                                                 *
* Forum software by:          Simple Machines (http://www.simplemachines.org)     *
* Copyright 2006-2007 by:     Simple Machines LLC (http://www.simplemachines.org) *
*           2001-2006 by:     Lewis Media (http://www.lewismedia.com)             *
***********************************************************************************
* This program is free software; you may redistribute it and/or modify it under   *
* the terms of the provided license as published by Simple Machines LLC.          *
*                                                                                 *
* This program is distributed in the hope that it is and will be useful, but      *
* WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY    *
* or FITNESS FOR A PARTICULAR PURPOSE.                                            *
*                                                                                 *
* See the "license.txt" file for details of the Simple Machines license.          *
* The latest version of the license can always be found at                        *
* http://www.simplemachines.org.                                                  *
***********************************************************************************/

if (!defined('SMF'))
die('Hacking attempt...');

class 
item_AddToHealthandAttack extends itemTemplate
{
function getItemDetails()
{
$this->authorName 'Daniel15';
$this->authorWeb 'http://www.dansoftaustralia.net/';
$this->authorEmail '[email protected]';

$this->name 'Add 25 to Health and 15 to Attack';
$this->desc 'Increase your total Health by 25 and your total Attack by 15!';
$this->price 150;

$this->require_input false;
$this->can_use_item true;
$this->addInput_editable true;
}


// This is the best bit of this item. When you add the item into the admin panel, you
// can set additional variables (up to 4). Make sure to call them info1, info2, info3
// and info4.
function getAddInput()
{
global $item_info;
if ($item_info[1] == 0$item_info[1] = 10;
return 'Amount to change Health by: <input type="text" name="info1" value="' $item_info[1] . '" />';
}

// The additional parameters (see 'getAddInput' above) are in the $item_info array.
// Make sure to make it global (like shown here) otherwise you won't be able to access
// its contents. THE ARRAY IS 1-BASED (1 IS THE FIRST ITEM) NOT 0-BASED!
function onUse()
{
global $smcFunc$context$item_info;

$request $smcFunc['db_query']('''
SELECT value
FROM {db_prefix}themes
WHERE id_member = {int:id}
AND variable = {string:var}'
,
array(
'id' => $context['user']['id'],
'var' => 'cust_health',
)
);
if ($smcFunc['db_num_rows']($request) == 0)
{
// There's no value for this user! Insert a default of 50 or something...
$smcFunc['db_insert']('replace',
'{db_prefix}themes',
array('id_member' => 'int''id_theme' => 'int''variable' => 'string''value' => 'string'),
array($context['user']['id'], 1'cust_health'50),
array('id_member''id_theme''variable')
);
}
else
{
list($value) = $smcFunc['db_fetch_row']($request);
$value += $item_info[1];
$smcFunc['db_query']('''
UPDATE {db_prefix}themes
SET value = {string:value}
WHERE id_member = {int:id}
AND variable = {string:var}'
,
array(
'value' => $value,
'id' => $context['user']['id'],
'var' => 'cust_health',
)
);
}
}

}
{

// This is the best bit of this item. When you add the item into the admin panel, you
// can set additional variables (up to 4). Make sure to call them info1, info2, info3
// and info4.
function getAddInput()
{
global $item_info;
if ($item_info[2] == 0$item_info[2] = 10;
return 'Amount to change Attack by: <input type="text" name="info2" value="' $item_info[2] . '" />';
}

// The additional parameters (see 'getAddInput' above) are in the $item_info array.
// Make sure to make it global (like shown here) otherwise you won't be able to access
// its contents. THE ARRAY IS 1-BASED (1 IS THE FIRST ITEM) NOT 0-BASED!
function onUse()
{
global $smcFunc$context$item_info;

$request $smcFunc['db_query']('''
SELECT value
FROM {db_prefix}themes
WHERE id_member = {int:id}
AND variable = {string:var}'
,
array(
'id' => $context['user']['id'],
'var' => 'cust_attack',
)
);
if ($smcFunc['db_num_rows']($request) == 0)
{
// There's no value for this user! Insert a default of 50 or something...
$smcFunc['db_insert']('replace',
'{db_prefix}themes',
array('id_member' => 'int''id_theme' => 'int''variable' => 'string''value' => 'string'),
array($context['user']['id'], 2'cust_attack'50),
array('id_member''id_theme''variable')
);
}
else
{
list($value) = $smcFunc['db_fetch_row']($request);
$value += $item_info[2];
$smcFunc['db_query']('''
UPDATE {db_prefix}themes
SET value = {string:value}
WHERE id_member = {int:id}
AND variable = {string:var}'
,
array(
'value' => $value,
'id' => $context['user']['id'],
'var' => 'cust_attack',
)
);
}
}
}
?>


Arantor

Well, you've duplicated more of the code than I expected - I only meant for you to duplicate the smaller part, basically the bit I wrote.

But you have an item, part of that item's details is the value it updates - you're using that same value both times (it's the $item_info part) - how exactly do you expect to use two different values when it only has one to start with?

Advertisement: