Customizing SMF > SMF Coding Discussion
implode Array problem
The Wizard:
objective - Start or add more data into a Column using - comma-delimited text
I'm using SMF 2.0.2
Assume there is a column in the SMF Database under each members profile called - wizard
example of data to be stored - image1, image2, image4, image20
What ever code is use must be in - function onUse()
My plan is to add - wizard - to the following code in Load.php
--- Code: ---if ($set == 'normal')
{
$select_columns = '
IFNULL(lo.log_time, 0) AS is_online, IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type, wizard,
--- End code ---
and add wizard here as well -
--- Code: ---elseif ($set == 'profile')
{
$select_columns = '
IFNULL(lo.log_time, 0) AS is_online, IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type, wizard,
--- End code ---
I'm not sure if I need to add wizard to both places as I'm unsure as to how that code works so if you can answer this please do so.
I'm guessing in reads the info in the database and makes it available to use quickly. So thats how I plan on reading from the database
column - wizard
So this is what I'm thinking might work -
--- Code: ---function onUse()
{
global $user_info;
// Get all previous items in wizard and convert string to array
$W => explode (',', $profile['wizard']);
$Z => implode(',', $W, image12);
updateMemberData($user_info['id'], array('wizard' => '$Z'));
return '<br>You now own 12 long-stem roses.<br /><br />We here at Acme Florists division hopes that they will brighten your day.<br />
<br />Enjoy<br />
<br />Acme Division Products Company<br /><br />';
}
--- End code ---
OK so what did I leave out and what did I do wrong?
Note: I'm working on - I guess you would call it a plug in for the SMF Shop mod so all the image files as stored where the shop mod is.
I know this is not good form, but I did not design the SMF Shop mod nor did I decide to put it in the Sources Dir.
I'm just trying to keep all the shop/plug in stuff together. Maybe some day I'll know enough to rework the SMF Shop mod until then lay off.
If you still want to point out this flaw take it up with the guys who reworked the mod to work with 2.0.2.
The Wizard:
anyone?
emanuele:
PHP's manual is your friend: http://php.net/manual/en/function.implode.php
implode accepts two arguments and only two.
So you should add it *before* imploding the array, e.g.
--- Code: ---$W = explode (',', $profile['wizard']);
$W[] = 'image12';
$Z = implode(',', $W);
--- End code ---
But then, I wonder why do you explode to just implode it again?
Better do something like:
--- Code: ---$profile['wizard'] .= !empty($profile['wizard']) ? ',' : '' . 'image12';
--- End code ---
Other random "mistakes":
1) when you assign a value to a variable you do it with an equal sign (=), not equal+greater than (=>),
2) equal+greater than (=>) is used when you assign values to array indexes:
array('index' => 'value');
3) strings should always be delimited by quotes (single or double, the two behave differently).
MrPhil:
--- Quote from: emanuele on August 22, 2012, 09:54:37 AM ---
--- Code: ---$profile['wizard'] .= !empty($profile['wizard']) ? ',' : '' . 'image12';
--- End code ---
--- End quote ---
should probably be
--- Code: ---$profile['wizard'] .= (!empty($profile['wizard']) ? ',' : '') . 'image12';
--- End code ---
so that 'image12' is added on to the end regardless of whether a comma or null string is first added (operator precedence).
This all assumes that $profile['wizard'] is always defined in the first place. If there are cases where it might not be, you would want to check isset($profile['wizard']) and (if not) decide whether you want to create the array first, or just skip the whole operation.
The Wizard:
For some reason all this does is rewrite over anything that's in the column - wizard.
If I have image1,image2,image4 in the column after using the code I get - image12
Do I need to use a global to call up stuff on the Load.php? if so what do I use? - just my guess.
Wiz
--- Code: ---function onUse()
{
global $user_info;
$profile['wizard'] .= (!empty($profile['wizard']) ? ',' : '') . 'image12';
updateMemberData($user_info['id'], array('wizard' => $profile['wizard']));
return '<br>You now own 12 long-stem roses.<br /><br />We here at Acme Florists division hopes that they will brighten your day.<br />
<br />Enjoy<br />
<br />Acme Division Products Company<br /><br />';
}
--- End code ---
Navigation
[0] Message Index
[#] Next page
Go to full version