Customizing SMF > SMF Coding Discussion

Explode Array Question

(1/3) > >>

The Wizard:
objective - Get data out of Column and turn a string into an array - comma-delimited text and then post images

I'm using SMF 2.0.2

Assume there is a column in the SMF Database under each members profile called - wizard
and that this column stores the names of images - image1, image2, image3, image4,
Also note not all members may have data in this column nor may they have the same
number of images.

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

Now I need to take the data in wizard and turn it into a array

$W = explode (',', $wizard); - exactly where to put this code in Load.php I'm not sure.

Now I need to add -

--- Code: --- 'wizard_images' => empty($profile['$W']) ? '' : '<a href="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/popup/test1.html' . '" target="_blank"><img src="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/small/' . $profile['$W'] . '.png' . '"></a>',
                 
--- End code ---
                   
to the following in Load.php so I can pick up the results in Display.template.php -


--- Code: ---// What a monstrous array...
$memberContext[$user] = array(
--- End code ---
   
The following code goes under gender in the Display.template.php


--- Code: ---// Show SMFShop Item Wizard Image Version 1.5
     

$i = 0;
echo '<table><tr>';
foreach($message['member']['wizard_images'] as $image)
{
// Ignore it if it ain't there.
if (empty($image))
continue;
$i++;
// Show it if it is.
echo '<td>', $image, '</td>';
if($i % 3 == 0)
echo '</tr><tr>';
}
// Finish up depending on # of images in that row.
if($i % 3 == 1)
echo '<td colspan="2"></td></tr></table>';
if($i % 3 == 2)
echo '<td></td></tr></table>';
else
echo '</tr></table>';


// **********************************

--- 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.

emanuele:

--- Quote ---$W = explode (',', $wizard); - exactly where to put this code in Load.php I'm not sure.
--- End quote ---
Just "before" (i.e. in the same function :

--- Code: --- 'wizard_images' => empty($profile['$W']) ? '' : '<a href="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/popup/test1.html' . '" target="_blank"><img src="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/small/' . $profile['$W'] . '.png' . '"></a>',
--- End code ---

But changing it to:

--- Code: ---$W = explode (',', $profile['real_name']);
--- End code ---

Now that one:

--- Code: --- 'wizard_images' => empty($profile['$W']) ? '' : '<a href="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/popup/test1.html' . '" target="_blank"><img src="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/small/' . $profile['$W'] . '.png' . '"></a>',
--- End code ---
should be rather different.
$profile['$W'] doesn't exists, what you have is $W.
Additionally $W is an array, and not a string according to the explode.
So, something like this *could* work:

--- Code: --- 'wizard_images' => empty($W) ? '' : '<a href="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/popup/test1.html' . '" target="_blank"><img src="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/small/' . implode('.png"></a><a href="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/popup/test1.html' . '" target="_blank"><img src="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/small/', $W) . '.png' . '"></a>',
--- End code ---

not sure at the moment if there is anything else.

The Wizard:
Hi emanue!

using this code -
--- Code: ---$W = explode (',', $profile['real_name']);
--- End code ---

I dont get where you got - 'real_name' - from? should that be 'wizard' instead?

The Wizard:
Did you make a mistake here -

--- Code: ---'wizard_images' => empty($W) ? '' : '<a href="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/popup/test1.html' . '" target="_blank"><img src="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/small/' . implode('.png"></a><a href="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/popup/test1.html' . '" target="_blank"><img src="' . $boardurl . '/Sources/shop/wizards_item_image_gallery/small/', $W) . '.png' . '"></a>',
--- End code ---

becouse it looks like you did the links twice? is there a reason for this? To me it just feels wrong?

emanuele:

--- Quote from: The Wizard on August 20, 2012, 11:58:55 AM ---I dont get where you got - 'real_name' - from? should that be 'wizard' instead?

--- End quote ---
lol
Yes, I it was:

--- Code: ---$W = explode (',', $profile['wizard']);
--- End code ---
power of copy and paste! :P


--- Quote from: The Wizard on August 20, 2012, 12:04:12 PM ---Did you make a mistake here -
[...]
becouse it looks like you did the links twice? is there a reason for this? To me it just feels wrong?

--- End quote ---
Nope, that's intended.

Let's make an example:

--- Code: ---$a = array(1, 2);
echo 'before_' . implode('_after<br />before_', $a) . '_after';

--- End code ---

This should output:

--- Code: ---before_1_after<br />before_2_after
--- End code ---
Why?
let's follow the code.
First step:

--- Code: ---echo 'before_'
--- End code ---
easy, it outputs "before_".

--- Code: ---implode('_after<br />before_', $a)
--- End code ---
implode is the opposite of explode and put the first argumet between the elements of the array, so the above code will produce: "1_after<br />before_2"

and finally the last bit:

--- Code: --- . '_after';
--- End code ---
that will output "_after".

The three pieces are the:
1) "before_"
2) "1_after<br />before_2"
3) "_after"
putting them together you'll get:

--- Code: ---before_1_after<br />before_2_after
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version