General Community > Scripting Help
some code from the old profiles.php
Haase:
In PfaBB, we had an issue where when a user does not have a designated skin, and they go to edit their profile for the first time, the skin select box should default to $modSettings[default_skin].
In the Profile page, there is a block of code that handles default values:
--- Code: --- // set up the default values
$memsettings['realName'] = (isset($memsettings['realName']) ? $memsettings['realName'] : '');
$memsettings['usertitle'] = (isset($memsettings['usertitle']) ? $memsettings['usertitle'] : '');
$memsettings['location'] = (isset($memsettings['location']) ? $memsettings['location'] : '');
$memsettings['websiteTitle'] = (isset($memsettings['websiteTitle']) ? $memsettings['websiteTitle'] : '');
$memsettings['websiteUrl'] = (isset($memsettings['websiteUrl']) ? $memsettings['websiteUrl'] : '');
$memsettings['ICQ'] = (isset($memsettings['ICQ']) ? $memsettings['ICQ'] : '');
$memsettings['YIM'] = (isset($memsettings['YIM']) ? $memsettings['YIM'] : '');
$memsettings['timeFormat'] = (isset($memsettings['timeFormat']) ? $memsettings['timeFormat'] : '');
$memsettings['timeOffset'] = (isset($memsettings['timeOffset']) ? $memsettings['timeOffset'] : '0');
$memsettings['lngfile'] = (strlen($memsettings['lngfile']) > 2 ? $memsettings['lngfile'] : $language);
--- End code ---
I really haven't worked with this kind of construct before, but in copying the pattern, I came up with:
--- Code: --- $memsettings['skin'] = (isset($memsettings['skin']) ? $memsettings['skin'] : $modSettings[default_skin]);
--- End code ---
I thought that would work, but it didn't.
So instead, I went with what I know, which is now working fine:
--- Code: --- if ($memsettings[skin] == '') { $memsettings[skin] = $modSettings[default_skin]; }
--- End code ---
Could someone explain to me the original function, or point me to a tutorial or www.php.net page that describes how it works. Ultimately, I'd like to know why I couldn't get it to work, and if these two different methods are functionally equivelant, is there a particular advantage to doing it one way versus the other.
Thanks,
-Haase
Spaceman-Spiff:
i think you should use empty() instead
or maybe empty(trim($var))
http://www.php.net/manual/en/types.comparisons.php
Jack.R.Abbitâ„¢:
--- Code: ---$memsettings['skin'] = (isset($memsettings['skin']) ? $memsettings['skin'] : $modSettings[default_skin]);
--- End code ---
the isset($memsettings['skin'] is your condition. If that condition evaluates as "true" then you basically get the equivalent of $memsettings['skin'] = $memsettings['skin']. If it evaulates to "false" you basically get $memsettings['skin'] = $modSettings[default_skin]. Nothing is technically wrong with that code you tried (however, you might try putting single quotes in the $modSettings[default_skin] part)... but just knowing it didn't work for you is not enough info. What didn't work? Did you get an error? Are you not getting the proper skin loaded? Are they loading opposite of what you would expect?
As for why to use one method over the other... I don't know. Soome find the method I explained easier to read... other find it harder. I don't know which is faster or better.
-Jack
Haase:
Thanks Jack.
The issue that I was getting, was that no matter whether there was a value in the table from $memsettings['skin'] or not, it was always returning true. I verified that by changing the code !isset, in which case it always returned with the value of $modSettings['default_skin'].
So I guess I *did* understand the code structure. Now I have to find out why isset() returned true with a null in the database...
[edit]
from php.net:
--- Quote ---$var = '';
// This will evaluate to &true; so the text will be printed.
if (isset($var)) {
print "This var is set set so I will print.";
}
--- End quote ---
[/edit]
Jack.R.Abbitâ„¢:
Based on the comparison chart at the link Spaceman-Spiff posted, I'd try just treating it as a Boolean compare.
--- Code: ---$memsettings['skin'] = ($memsettings['skin'] ? $memsettings['skin'] : $modSettings[default_skin]);
--- End code ---
It will basically be true as long as the value of $memsettings['skin'] contains something value besides 0. But this method in this case seems kind of strange. Unless this is faster, I might just go with what you already got to work.
-Jack
Navigation
[0] Message Index
[#] Next page
Go to full version