Customizing SMF > SMF Coding Discussion
Small problem with undefined index
phantomm:
Hi, so I made this package with button (something like "Like" from Facebook, but from some Polish site..), and in logs I have two repeating errors:
Undefined index: nk_fajne1
and
Undefined index: nk_fajne2
Here is code:
In Display.template, before // Maybe they want to report this post to the moderator(s)?
--- Code: --- if (!empty($modSettings['nk_fajne']))
{
if ($message['id'] == $context['topic_first_message'] || !empty($modSettings['display_nk_fajne_all']))
{
echo '
<span class="floatleft" style="margin: 6px;" id="nk">
<script>
(function() {
var id = \'nk-widget-sdk\';
var js, first_js = document.getElementsByTagName(\'script\')[0];
if (document.getElementById(id)) return;
js = document.createElement(\'script\');
js.id = id; js.async = true;
js.type = \'text/javascript\';
js.src = \'http://0.s-nk.pl/script/packs/nk_widgets_all.js\';
first_js.parentNode.insertBefore(js, first_js);
}());
</script>
<div class = "nk-fajne"
data-nk-url = "', $scripturl, '?topic=', $context['current_topic'], '"
data-nk-type = "',$modSettings['nk_fajne1'],'"
data-nk-color = "',$modSettings['nk_fajne2'],'"
data-nk-title = "', $message['subject'], '"
data-nk-image = ""
data-nk-description = "', $message['body'], '">
</div>
</span>';
}
}
--- End code ---
Source file(settings):
--- Code: ---<?php
/**
* @package NK Fajne
* @version 1.0
* @author phantom
* @copyright Copyright (c) 2012, phantom
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/ CC BY-NC-ND 3.0
*/
if (!defined('SMF'))
die('Hacking attempt...');
function nk_fajne_admin_areas($areas)
{
global $txt;
loadLanguage('NK-Fajne');
$areas['config']['areas']['modsettings']['subsections']['nk_fajne'] = array($txt['nk_fajne_admin']);
}
function nk_fajne_modify_modifications($sub_actions)
{
global $context;
$sub_actions['nk_fajne'] = 'nk_fajne_settings';
}
function nk_fajne_settings($return_config = false)
{
global $context, $txt, $scripturl, $sourcedir;
$context['page_title'] = $txt['nk_fajne_admin'];
$context['settings_title'] = $txt['nk_fajne_admin'];
$context['settings_message'] = $txt['nk_fajne_admin_desc'];
$context['post_url'] = $scripturl . '?action=admin;area=modsettings;save;sa=nk_fajne';
$config_vars = array(
array('check', 'nk_fajne'),
array('select', 'nk_fajne1', array('0' => $txt['nk_fajne_size1'], '1' => $txt['nk_fajne_size2'], '2' => $txt['nk_fajne_size3'], '3' => $txt['nk_fajne_size4'], '4' => $txt['nk_fajne_size5'], '5' => $txt['nk_fajne_size6'], )),
array('select', 'nk_fajne2', array('0' => $txt['nk_fajne_color1'], '1' => $txt['nk_fajne_color2'],)),
array('check', 'display_nk_fajne_all'),
);
if ($return_config)
return $config_vars;
if (isset($_GET['save'])) {
checkSession();
saveDBSettings($config_vars);
writeLog();
redirectexit('action=admin;area=modsettings;sa=nk_fajne');
}
prepareDBSettingContext($config_vars);
}
--- End code ---
and language:
--- Code: ---<?php
/**
* @package NK Fajne
* @version 1.0
* @author phantom
* @copyright Copyright (c) 2012, phantom
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/ CC BY-NC-ND 3.0
*/
$txt['nk_fajne'] ='Enable NK Fajne mod';
$txt['nk_fajne_admin'] = 'NK Fajne';
$txt['nk_fajne_admin_desc'] = 'Settings...';
$txt['nk_fajne1'] = 'Select size and type';
$txt['nk_fajne_size1'] = '1';
$txt['nk_fajne_size2'] = '2';
$txt['nk_fajne_size3'] = '3';
$txt['nk_fajne_size4'] = '4';
$txt['nk_fajne_size5'] = '5';
$txt['nk_fajne_size6'] = '6';
$txt['nk_fajne2'] = 'Color';
$txt['nk_fajne_color1'] = 'White';
$txt['nk_fajne_color2'] = 'Black';
$txt['display_nk_fajne_all'] ='Show NK Fajne button in every post';
--- End code ---
Have no idea why, so I'm asking you for help - where is error?
Arantor:
well, you're using the variables without checking if they're defined or not:
--- Code: --- data-nk-type = "',$modSettings['nk_fajne1'],'"
--- End code ---
Set a default with an inline ternary expression:
--- Code: --- data-nk-type = "', empty($modSettings['nk_fajne1']) ? 'some default value' : $modSettings['nk_fajne1'], '"
--- End code ---
(Note, do NOT do this with . between expressions, it will not work properly, but it's OK because you're using , between things here)
phantomm:
I didn't have this problem in another very similar mod, and this option is dropdown list, so I was thinking that this always has some value
Arantor:
You cannot ever rely on things having a value unless you expressly set it that execution. $modSettings drops values at times when the resultant value would be 0 or an empty string, to save space.
phantomm:
ah.. this explains it. By default both options are = 0.
Thank you again for help :)
Navigation
[0] Message Index
[#] Next page
Go to full version