php-general Digest 31 Dec 2012 20:37:29 -0000 Issue 8081

Topics (messages 319975 through 319981):

Re: Shopping Cart Discount System
        319975 by: Ashley Sheridan

variable placeholders in a text file
        319976 by: Nelson Green
        319977 by: Stephen
        319978 by: Ashley Sheridan
        319979 by: Stuart Dallas
        319980 by: Nelson Green
        319981 by: Nelson Green

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---

Karl DeSaulniers <k...@designdrumm.com> wrote:

>Hello Everyone,
>Hope all are well. Quick and hopefully painless question.
>Is there any examples on how to build a discount system
>into your shopping cart out there that anyone knows of?
>I am using MySQL and PHP. I have built one of my own so far,
>but am having trouble making sense of what goes where.
>
>For example. If a product has miscellaneous charges,
>lets say.. glitter is extra on your shirt.
>How is discounts applied? I mean, as far as discounts go,
>lets say its 20% a shirt and this shirt has glitter.
>Is it best practice to apply the discount to just the shirt
>or the shirt and glitter as a combo discount.
>I know this is somewhat dependent on the owners choice
>of what he/she wants to give the discount on,
>but my question is of the programing of it.
>Do I build conditions for the shirt to get a discount applied
>then the miscellaneous charges, or combine the totals of the two,
>then apply the discount to the sum?
>
>Then lets say there is a cart discount also being applied.
>Is it best practice to apply this to the total of items then
>add the shipping, rush charges and tax, or to the total of the whole  
>cart
>including shipping, rush charges then add the tax after applying the  
>discount?
>
>Sorry for the run-on question, hope this makes sense enough to merit  
>help.
>
>HNY,
>
>Best,
>
>Karl DeSaulniers
>Design Drumm
>http://designdrumm.com
>
>PS: Because this question could be answered by either database or  
>general, is the only reason for the double post.
>Wont be making a habit of it.. :)

I would apply the discounts where they made sense from a customer POV. Would 
you expect a t-shirt discount to be applied to only the base item or the 
product as a whole? I'd say tge whole thing. You could have various areas in 
your code that apply discounts at different levels: per item, per group of 
items (bogof, etc), and per basket.


Thanks,
Ash
http://www.ashleysheridan.co.uk

--- End Message ---
--- Begin Message ---
Hello,

I have created a simple function that prints a personalized greeting by reading
the greeting contents from a file. I pass the user's name to the function,
and the function reads the file contents into a string variable. I am then
using str_replace to replace the word USER in the string with the user's
name that was passed to the function. Then the function correctly prints
the personalized greeting as I wish.

My question is, is there another way to do something similar, such as
embedding a variable name directly into the text file? In other words,
instead of my text file reading:

Hello USER ...

Can I do something like this:

Hello $user_name ...

and then write my function to replace $user_name with the passed
parameter prior to printing?

The reason I ask is because I am going to want to do three substitutions,
and I'd rather not do three str_replace calls if I don't have to. Plus the
latter seems to be a more robust way of making the changes.

Thanks, and apologies if this has been asked before and I missed it. I'm
just not sure how to phrase this for a search engine.

Nelson                                    

--- End Message ---
--- Begin Message ---
Yes!

Easy standard stuff.

$title = 'Mr.";
$user_name = 'John Doe';

$message = "Hello $title $user_name ...."

Just define the value for the variables before defining the value for the message.

Note that $message has to use double quotes for the expansion. Also consider using HEREDOC instead of the double quotes.

You may want to put your message in a text file and using the include function.

On 12-12-31 02:39 PM, Nelson Green wrote:
Hello,

I have created a simple function that prints a personalized greeting by reading
the greeting contents from a file. I pass the user's name to the function,
and the function reads the file contents into a string variable. I am then
using str_replace to replace the word USER in the string with the user's
name that was passed to the function. Then the function correctly prints
the personalized greeting as I wish.

My question is, is there another way to do something similar, such as
embedding a variable name directly into the text file? In other words,
instead of my text file reading:

Hello USER ...

Can I do something like this:

Hello $user_name ...

and then write my function to replace $user_name with the passed
parameter prior to printing?

The reason I ask is because I am going to want to do three substitutions,
and I'd rather not do three str_replace calls if I don't have to. Plus the
latter seems to be a more robust way of making the changes.

Thanks, and apologies if this has been asked before and I missed it. I'm
just not sure how to phrase this for a search engine.

Nelson                                  


--
Stephen


--- End Message ---
--- Begin Message ---
On Mon, 2012-12-31 at 13:39 -0600, Nelson Green wrote:

> Hello,
> 
> I have created a simple function that prints a personalized greeting by 
> reading
> the greeting contents from a file. I pass the user's name to the function,
> and the function reads the file contents into a string variable. I am then
> using str_replace to replace the word USER in the string with the user's
> name that was passed to the function. Then the function correctly prints
> the personalized greeting as I wish.
> 
> My question is, is there another way to do something similar, such as
> embedding a variable name directly into the text file? In other words,
> instead of my text file reading:
> 
> Hello USER ...
> 
> Can I do something like this:
> 
> Hello $user_name ...
> 
> and then write my function to replace $user_name with the passed
> parameter prior to printing?
> 
> The reason I ask is because I am going to want to do three substitutions,
> and I'd rather not do three str_replace calls if I don't have to. Plus the
> latter seems to be a more robust way of making the changes.
> 
> Thanks, and apologies if this has been asked before and I missed it. I'm
> just not sure how to phrase this for a search engine.
> 
> Nelson                                          


You could use an existing templating solution, like Smarty, although for
what you want to do it might be overkill. A few str_replace() calls
shouldn't produce too much overhead, but it depends on the size of the
text string in question. If it's just a couple of paragraphs of text, no
problem, something closer to a whole chapter of a book will obviously be
more expensive.

You could try eval() on the block of text, but if you do, be really
careful about what text you're using. I wouldn't recommend this if
you're using any text supplied by a user. As a last option, you could
have the text stored as separate parts which you join together in one
string later. This might be less expensive in terms of processing power
required, but it also makes maintenance more of a hassle later.


Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
Please excuse the top post, but this may be helpful: 
http://stut.net/2008/10/28/snippet-simple-templates-with-php/

-Stuart

-- 
Sent from my leaf blower

On 31 Dec 2012, at 19:59, Ashley Sheridan <a...@ashleysheridan.co.uk> wrote:

> On Mon, 2012-12-31 at 13:39 -0600, Nelson Green wrote:
> 
>> Hello,
>> 
>> I have created a simple function that prints a personalized greeting by 
>> reading
>> the greeting contents from a file. I pass the user's name to the function,
>> and the function reads the file contents into a string variable. I am then
>> using str_replace to replace the word USER in the string with the user's
>> name that was passed to the function. Then the function correctly prints
>> the personalized greeting as I wish.
>> 
>> My question is, is there another way to do something similar, such as
>> embedding a variable name directly into the text file? In other words,
>> instead of my text file reading:
>> 
>> Hello USER ...
>> 
>> Can I do something like this:
>> 
>> Hello $user_name ...
>> 
>> and then write my function to replace $user_name with the passed
>> parameter prior to printing?
>> 
>> The reason I ask is because I am going to want to do three substitutions,
>> and I'd rather not do three str_replace calls if I don't have to. Plus the
>> latter seems to be a more robust way of making the changes.
>> 
>> Thanks, and apologies if this has been asked before and I missed it. I'm
>> just not sure how to phrase this for a search engine.
>> 
>> Nelson                         
> 
> 
> You could use an existing templating solution, like Smarty, although for
> what you want to do it might be overkill. A few str_replace() calls
> shouldn't produce too much overhead, but it depends on the size of the
> text string in question. If it's just a couple of paragraphs of text, no
> problem, something closer to a whole chapter of a book will obviously be
> more expensive.
> 
> You could try eval() on the block of text, but if you do, be really
> careful about what text you're using. I wouldn't recommend this if
> you're using any text supplied by a user. As a last option, you could
> have the text stored as separate parts which you join together in one
> string later. This might be less expensive in terms of processing power
> required, but it also makes maintenance more of a hassle later.
> 
> 
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
> 
> 

--- End Message ---
--- Begin Message ---
On Mon, 31 Dec 2012 19:59:02, Ashley Sheridan wrote:
_______________________________
>  
> On Mon, 2012-12-31 at 13:39 -0600, Nelson Green wrote: 

 
> My question is, is there another way to do something similar, such as 
> embedding a variable name directly into the text file? In other words, 
> instead of my text file reading: 
>  
> Hello USER ... 
>  
> Can I do something like this: 
>  
> Hello $user_name ... 
>  
> and then write my function to replace $user_name with the passed 
> parameter prior to printing? 
>  

>  
> You could use an existing templating solution, like Smarty, although  
> for what you want to do it might be overkill. A few str_replace() calls  
> shouldn't produce too much overhead, but it depends on the size of the  
> text string in question.
>  
> You could try eval() on the block of text, but if you do, be really  
> careful about what text you're using. I wouldn't recommend this if  
> you're using any text supplied by a user. As a last option, you could  
> have the text stored as separate parts which you join together in one  
> string later. This might be less expensive in terms of processing power  
> required, but it also makes maintenance more of a hassle later. 
>  
>  
> Thanks, 
> Ash 
> http://www.ashleysheridan.co.uk 

Hi Ash,

Yep, smarty would be way more than I need right now. I'm just dabbling with
various things, trying to learn more about PHP. And my first thought was to
split the components, which worked fine. Then I tried a HEREDOC which did
allow variable substitution. This attempt is a move up from that, trying to
generalize things a bit more. My input will be 100% generated by me, but in the
back of my mind I'm looking towards the ability to use user supplied strings.
                                          

--- End Message ---
--- Begin Message ---
On Mon, 31 Dec 2012 14:47:20 Stephen D wrote:
>
> Yes!
>
> Easy standard stuff.
>
> $title = 'Mr.";
> $user_name = 'John Doe';
>
> $message = "Hello $title $user_name ...."
>
> Just define the value for the variables before defining the value for
> the message.
>
> Note that $message has to use double quotes for the expansion. Also
> consider using HEREDOC instead of the double quotes.
>
> You may want to put your message in a text file and using the include
> function.

Hi Stephen,

My message is in a text file, but I'm using fopen and fread in a self-defined
function, so message is actually defined as (GREETER_FILE is a defined
constant):
function print_greeting($user_name)
{ 
   $handle   = fopen(GREETER_FILE, "r");
   $message  = fread($file_handle, filesize(GREETER_FILE));
   $msg_text = str_replace("USER", $user_name, $message);
   
   print($msg_txt);
}

And my text file is simply:
$cat greet.txt
Hello USER. How are you today?

If I change USER to $user_name in the text file and change the print function
parameter to $message, $user_name gets printed verbatim. In other words
the greeting on my page becomes:
Hello $user_name. How are you today?

I want to pass the name Nelson to the function, and have it output:
Hello Nelson. How are you today?

after the function reads in text file input that contains a variable placeholder
for the user name. I actually had a HEREDOC in the function, and that worked.
But by reading a file instead, I can make things more flexible. I'd rather be
changing a text file instead of a code file.
                                          

--- End Message ---

Reply via email to