Uutiset:

Wondering if this will always be free?  See why free is better.

Main Menu
Advertisement:

Function inside function or not?

Aloittaja The Wizard, syyskuu 06, 2013, 08:02:59 AP

« edellinen - seuraava »

The Wizard

Hello:

Below is the code I'm working on, and I'm wondering if the "function formatMoney($money)" should be a separate function, or is it OK to have it in "function ShopMain()" ? If so could you show me how the code should look as I'm unsure how it should look.

Thanks

Wiz



<?php

function ShopMain()
{
global $context$scripturl$txt$smcFunc$modSettings;

loadTemplate('WizardsShopUser');
loadLanguage('WizardsShop');

// Page title for the main page
$context['page_title'] = $txt['wizards_shop_main_page_title'];

// Navigational link tree to be shown at the top of the page.
$context['linktree'][] = array(
'url' => $scripturl '?action=shop_main',
'name' => $txt['wizards_shop_main']
);

// Title Text
$context['shop_main_Head'] = $txt['wizards_shop_main'];

// Write the money in a suitable format.
// Casts the value to a float, and add the prefix and suffix
function formatMoney($money)
{
global $modSettings;

// Cast to float
$money = (float) $money;
// Return amount with prefix and suffix added
return $modSettings['shopCurrencyPrefix'] . $money $modSettings['shopCurrencySuffix'];
}

// 10 richest people (pocket)
// Start with empty list
$context['shop_richest'] = array();

// Get the richest people
$result $smcFunc['db_query'](''"
SELECT id_member, real_name, money
FROM {db_prefix}members
ORDER BY money DESC, real_name
LIMIT 10"
, array());

// Loop through all results
while ($row $smcFunc['db_fetch_assoc']($result))

// And add them to the list
$context['shop_richest'][] = array(
'ID_MEMBER' => $row['id_member'],
'realName' => $row['real_name'],
'money' => $row['money']
);

$smcFunc['db_free_result']($result);

// 10 richest people (Bank)
// Start with an empty list
$context['shop_richestBank'] = array();

// Get the richest people
$result $smcFunc['db_query'](''"
SELECT id_member, real_name, moneyBank
FROM {db_prefix}members
ORDER BY moneyBank DESC, real_name
LIMIT 10"
, array());

// Loop through all results
while ($row $smcFunc['db_fetch_assoc']($result))

// Add them to the list
$context['shop_richestBank'][] = array(
'ID_MEMBER' => $row['id_member'],
'realName' => $row['real_name'],
'moneyBank' => $row['moneyBank']
);

$smcFunc['db_free_result']($result);

// We're using the "main" template
$context['sub_template'] = 'main';
}

// End of function ShopMain()

Ricky.

If you are going to use this function always inside the ShopMain() then it is fine to be there given that you are using it multiple times and hence making a function makes sense.

However, in your above code, you are not using it anywhere, so technically it is not casting value to float and adding prefix unless it is not specifically called with $money fed into it.

Arantor

Making a function is fine, putting a function inside a function is usually bad form. Put it in the same file outside ShopMain()

(It's a lot more complex than that as to why/why not, but there are few reasons you should be declaring it as such.)
Holder of controversial views, all of which my own.


The Wizard

Lainaus käyttäjältä: Arantor - syyskuu 06, 2013, 09:41:06 AP
Making a function is fine, putting a function inside a function is usually bad form. Put it in the same file outside ShopMain()

(It's a lot more complex than that as to why/why not, but there are few reasons you should be declaring it as such.)

That's what I thought. So what I posted below is the correct ?

Wiz


// Write the money in a suitable format.
// Casts the value to a float, and add the prefix and suffix
function formatMoney($money)
{
global $modSettings;

// Cast to float
$money = (float) $money;
// Return amount with prefix and suffix added
return $modSettings['shopCurrencyPrefix'] . $money . $modSettings['shopCurrencySuffix'];
}

// End of function formatMoney()

function ShopMain()
{
global $context, $scripturl, $txt, $smcFunc, $modSettings;

loadTemplate('WizardsShopUser');
loadLanguage('WizardsShop');
formatMoney($money);

// Page title for the main page
$context['page_title'] = $txt['wizards_shop_main_page_title'];

// Navigational link tree to be shown at the top of the page.
$context['linktree'][] = array(
'url' => $scripturl . '?action=shop_main',
'name' => $txt['wizards_shop_main']
);

// Title Text
$context['shop_main_Head'] = $txt['wizards_shop_main'];

// 10 richest people (pocket)
// Start with empty list
$context['shop_richest'] = array();

// Get the richest people
$result = $smcFunc['db_query']('', "
SELECT id_member, real_name, money
FROM {db_prefix}members
ORDER BY money DESC, real_name
LIMIT 10", array());

// Loop through all results
while ($row = $smcFunc['db_fetch_assoc']($result))

// And add them to the list
$context['shop_richest'][] = array(
'ID_MEMBER' => $row['id_member'],
'realName' => $row['real_name'],
'money' => $row['money']
);

$smcFunc['db_free_result']($result);

// 10 richest people (Bank)
// Start with an empty list
$context['shop_richestBank'] = array();

// Get the richest people
$result = $smcFunc['db_query']('', "
SELECT id_member, real_name, moneyBank
FROM {db_prefix}members
ORDER BY moneyBank DESC, real_name
LIMIT 10", array());

// Loop through all results
while ($row = $smcFunc['db_fetch_assoc']($result))

// Add them to the list
$context['shop_richestBank'][] = array(
'ID_MEMBER' => $row['id_member'],
'realName' => $row['real_name'],
'moneyBank' => $row['moneyBank']
);

$smcFunc['db_free_result']($result);

// We're using the "main" template
$context['sub_template'] = 'main';
}

// End of function ShopMain()

Arantor

Better, certainly. I have no idea why you're casting $money to float though...
Holder of controversial views, all of which my own.


The Wizard

Lainaus käyttäjältä: Arantor - syyskuu 06, 2013, 02:16:39 IP
Better, certainly. I have no idea why you're casting $money to float though...

Answer:


function template_main()
{
global $txt, $context, $modSettings, $scripturl, $boardurl;

loadTemplate('ShopWizard');

// Catagory Header
echo '
<div class="cat_bar">
<h3 class="catbg" align="center">', $context['shop_main_Head'], '</h3>
</div>
<table>
<tr valign="top">
<td><br />', shop_links(), '</td>
<td colspan="2" style="width: 75%;"><br />', shop_home(), '</td>
<td ><br />', wizard_image(),'</td>
</tr>
<tr>
<td colspan="2"><br />', richest_members_pocket(), '</td>
<td colspan="2"><br />', richest_members_bank(), '</td>
</tr>
</table>
';
}

function shop_home()
{
global $txt, $modSettings, $context;

// Display Shop Home
echo '
<div class="cat_bar">
<h3 class="catbg" align="center">', $txt['wizards_shop_link_home'], '</h3>
</div>
<div class="windowbg">
<span class="topslice"><span></span></span>
<div class="content">
<br />', sprintf($txt['wizards_shop_welcome_full'], formatMoney($modSettings['shopPointsPerTopic']), formatMoney($modSettings['shopPointsPerPost'])), '
';

// Are we using any bonuses at all?
if ($modSettings['shopPointsPerWord'] != 0 || $modSettings['shopPointsPerChar'] != 0)
{
echo $txt['wizards_shop_welcome_full2'];
// Are they capped?
if ($modSettings['shopPointsLimit'] != 0)
printf($txt['wizards_shop_welcome_full3'], formatMoney($modSettings['shopPointsLimit']));
}

echo '
<br /><br />
', sprintf($txt['wizards_shop_currently_have1'], formatMoney($context['user']['money'])), ($modSettings['shopBankEnabled'] ? sprintf($txt['wizards_shop_currently_have2'], formatMoney($context['user']['moneyBank'])) : ''), '
<br /><br />';

// Any bonuses? Show the heading
if ($modSettings['shopPointsPerWord'] != 0 || $modSettings['shopPointsPerChar'] != 0)
echo '
<strong>', $txt['wizards_shop_bonuses'], '</strong><br />';

// Are we using the points per word bonus?
if ($modSettings['shopPointsPerWord'] != 0)
echo '
', formatMoney($modSettings['shopPointsPerWord']), ' ', $txt['wizards_shop_per_word2'], '<br />';

// Are we using the points per char bonus?
if ($modSettings['shopPointsPerChar'] != 0)
echo '
', formatMoney($modSettings['shopPointsPerChar']), ' ', $txt['wizards_shop_per_char2'], '<br />';

echo '
</div>
<span class="botslice"><span></span></span>
</div>
';
}

function richest_members_pocket()
{
global $txt, $context, $scripturl;

// Display 10 Richest Members (Pocket)
echo '
<div class="cat_bar">
<h3 class="catbg" align="center">', $txt['wizards_shop_richest_pocket'], '</h3>
</div>
<div class="windowbg">
<span class="topslice"><span></span></span>
<div class="content">
';

foreach ($context['shop_richest'] as $row)
echo '
<strong><a href="', $scripturl, '?action=profile;u=', $row['ID_MEMBER'], '">', $row['realName'], '</a></strong> - ', formatMoney($row['money']), '<br />';

echo '
</div>
<span class="botslice"><span></span></span>
</div>
';
}

function richest_members_bank()
{
global $modSettings, $txt, $context, $scripturl;

// Display 10 Richest Members (Bank)

// Check to see if the bank's enabled
if ($modSettings['shopBankEnabled'])
{
echo '
<div class="cat_bar">
<h3 class="catbg" align="center">', $txt['wizards_shop_richest_bank'], '</h3>
</div>
<div class="windowbg">
<span class="topslice"><span></span></span>
<div class="content">
';

// Richest people (in bank)
foreach ($context['shop_richestBank'] as $row)
echo '
<strong><a href="', $scripturl, '?action=profile;u=', $row['ID_MEMBER'], '">', $row['realName'], '</a></strong> - ', formatMoney($row['moneyBank']), '<br />';

echo '
</div>
<span class="botslice"><span></span></span>
</div>
';
}
}

Arantor

Shoving a bunch of code at me and expecting me to discern what bit of it requires you to cast something to float is a waste of my time.

I take it you understand that float is not an accurate conception of numbers in general such that even something like 1.1 cannot properly be represented at all with it?
Holder of controversial views, all of which my own.


The Wizard

LainaaShoving a bunch of code at me and expecting me to discern what bit of it requires you to cast something to float is a waste of my time.

I did not mean to waste your time nor was I trying to aggravate you in anyway. I was just trying to give the best answer. I thought showing how the code would work would be easier then me trying to use words. Again I did not mean to offend you in anyway.

Wiz

SwedishMarch1964

If you intend. Yes, use it. This function is always inside ShopMain () then said to fine. Yes, it is, use it. More times and themselves.

However, the above with that. No, use it. There. Where you go, Yes, technical, No value. Floats and he added prefix apart if that is not special, invited with money $ Nourished. To her
Go to http://photobucket.com

The easy way to upload your avatar. Just click the direct link.

Burke ♞ Knight

I am so not going to even try to discern that last post. :P

The Wizard,
What Arantor means, is posting the code does not tell him WHY you need that part to float.
You need to TELL him in words, not posted code. :)

The Wizard

The more I think about Arantor's question the more I think I do not need to float at all. This code was part of the original shop mod code, and since then I have change it so much that I don't think floating is needed at all.

Wiz

The Wizard

Hello:

Just in case you all missed it - Arantor comment about the float was to get me to walk through every line of code (or instruction) I posted, and think what does it do and what happens. As he is fond of saying computers are not smart and they only do what you tell them to do.

In this case I did not need

// Cast to float
$money = (float) $money;


Wiz


Ricky.

I love reading aRantor's discussion dragged and generally deflated from original question.


However, for Wiz, I have a general question, which aRantor already pointed out..  code is of no mean to anyone unless you do not tell us in advance what is it intend to do, even then we went through it like puzzle and gave you our views.

Anyone who has good logic of programming will first take the trouble of explaining code and then will share code  and then will ask for advice on that code.

Arantor

Lainaus käyttäjältä: Ricky. - syyskuu 07, 2013, 05:58:48 AP
I love reading aRantor's discussion dragged and generally deflated from original question.

I love the implied insult right there.
Holder of controversial views, all of which my own.


The Wizard

LainaaHowever, for Wiz, I have a general question, which aRantor already pointed out..  code is of no mean to anyone unless you do not tell us in advance what is it intend to do, even then we went through it like puzzle and gave you our views.


The following is a work in progress - Wizards Shop is a branch, or a variation if you like on the SMF Shop mod.

Some of you might know I was working on a small mod called Wizards Gallery that allows you to Add, Remove, Buy, and Show images of the items you have in inventory.

The more I worked with the original shop mod the more I found out that it needed to be updated to work properly with Wizards Gallery. There was also the problem of Wizards Gallery being a mod of a mod or plug in if you will. I dreamed of a mod where you just use the package manager to install and your done.

And So here I am designing the next generation shop mod - Wizards Shop. I decided to take the best of the old shop mod and junk the rest.
I have VB Gamers permission to use the original shop code, and fully plan on giving him,  and the original creator credit as well as anybody else who helps on this project. I am not in this for the money, and so this project will be a free like so many of the other great mods available. I also can only work on this project in the free time I have in the real world, and we all know how that gets in the way.

This project will be submitted for approval, and so it needs to be written that way. I now have a 1st draft, and know it needs more work. Learning from past experience I'm not going to post the first draft until I have everything working and have smoothed out allot of the rough spots. Don't get me wrong I'm not deliberately hiding anything it's just my coding skill still lack and so it takes me longer to get to the same pace as the rest of you.

I have also learned that if I were to post the entire php page that this code was part of nobody would look at it. I know I sure wound not look at it. If you were to print out this php page on paper it would have been around 75 pages long! Because of the help provided here I was able to cut that down by about 5 pages.  Understand that this project is huge, and so I have to post bits instead the whole.

Wiz

Ricky.

Lainaus käyttäjältä: Arantor - syyskuu 07, 2013, 08:36:04 AP
Lainaus käyttäjältä: Ricky. - syyskuu 07, 2013, 05:58:48 AP
I love reading aRantor's discussion dragged and generally deflated from original question.

I love the implied insult right there.
Seriously, made me laugh ...


Man.. you are smart :)


@Wiz..
Good luck with your mod, you can always come to this place when you need help, just try to be more descriptive.. thats all we asks.

Advertisement: