php-general Digest 1 Jan 2013 22:08:46 -0000 Issue 8082

Topics (messages 319982 through 319991):

Re: Shopping Cart Discount System
        319982 by: tamouse mailing lists
        319987 by: Karl DeSaulniers

Re: variable placeholders in a text file
        319983 by: tamouse mailing lists
        319984 by: Bastien
        319985 by: Nelson Green
        319988 by: Stephen
        319990 by: tamouse mailing lists
        319991 by: Nelson Green

Re: [PHP-DB] Re: [PHP] Shopping Cart Discount System
        319986 by: Karl DeSaulniers

Re: SOLVED: variable placeholders in a text file
        319989 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 ---
On Mon, Dec 31, 2012 at 2:31 AM, 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?

These sound much more like business operating questions than
programming questions, per se. I think it really depends a lot on what
other businesses in the same niche as yours do, or if you can find
out. Once you have figured out the business practices, coding them
should be easier, I'd say, but doing it the other way 'round seems
like would lead to confusion.

Some questions from a really non-business person:

1. Why are you offering discounts at all? What do you hope to
accomplish by offering discounts?
2. Who qualifies for which discounts and why?
3. Do discounts apply to just the base merchandise, or the whole end
piece, with whatever customisations it might have, and if so or if
not, why?
4. Are there different discount rates applied to base merchandise and
subsequent customisations, and if so, why?
5. Is there any reason to make this complicated, or instead just offer
a flat rate discount on final price, before taxes and S&H?

--- End Message ---
--- Begin Message ---

On Dec 31, 2012, at 3:51 PM, tamouse mailing lists wrote:

On Mon, Dec 31, 2012 at 2:31 AM, 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?

These sound much more like business operating questions than
programming questions, per se. I think it really depends a lot on what
other businesses in the same niche as yours do, or if you can find
out. Once you have figured out the business practices, coding them
should be easier, I'd say, but doing it the other way 'round seems
like would lead to confusion.

Some questions from a really non-business person:

1. Why are you offering discounts at all? What do you hope to
accomplish by offering discounts?
2. Who qualifies for which discounts and why?
3. Do discounts apply to just the base merchandise, or the whole end
piece, with whatever customisations it might have, and if so or if
not, why?
4. Are there different discount rates applied to base merchandise and
subsequent customisations, and if so, why?
5. Is there any reason to make this complicated, or instead just offer
a flat rate discount on final price, before taxes and S&H?


My appologies if this is an OT thread. I was in the midst of coding these things when I asked it. Yes, it is a little confusing. This is a small t-shirt company a friend and I are putting together. I have thought out most of the business logic for the company and the website. The only thing I guess I forgot to plan in was these discounts and how to apply them. I currently have discounts per item and per the cart. The discounts are set up to apply on birthdays and added by admins for discounting items to clear inventory. The cart discounts are set to apply at a certain interval of purchase amounts. For eg. (and im really just throwing numbers) if someone buys $1,000 worth of merch, they are sent a 50% off your cart discount to their email.

I guess the question was more how to apply the discounts to the cart. Discount item + miscellaneous or just item. Discount cart or cart + shipping + handling. I was also asking if anyone had or knew of a sample script I could pick apart. That was probably the programing part of the question. But thank you guys for taking the time to answer.
I believe it helped. We will see.. :)

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com


--- End Message ---
--- Begin Message ---
On Mon, Dec 31, 2012 at 2:37 PM, Nelson Green <nelsongree...@hotmail.com> wrote:
>
> 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.

I use the include("template") method for this alla time, it works
great. Most especially for HTML emails coming from a web site to a
group of users, just slick as anything. include does basically just
what your print_greeting function does less the actual printout, but
using php variables instead of a str_replace. Also, this way the
templates can be stored elsewhere, outside the actual code base if
need be.

--- End Message ---
--- Begin Message ---

Bastien Koert

On 2012-12-31, at 4:58 PM, tamouse mailing lists <tamouse.li...@gmail.com> 
wrote:

> On Mon, Dec 31, 2012 at 2:37 PM, Nelson Green <nelsongree...@hotmail.com> 
> wrote:
>> 
>> 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.
> 
> I use the include("template") method for this alla time, it works
> great. Most especially for HTML emails coming from a web site to a
> group of users, just slick as anything. include does basically just
> what your print_greeting function does less the actual printout, but
> using php variables instead of a str_replace. Also, this way the
> templates can be stored elsewhere, outside the actual code base if
> need be.
> 

This is exactly what I do. Dead simple fast and the templates are fully self 
contained.

--- End Message ---
--- Begin Message ---
On 2012-12-31, at 4:58 PM, tamouse mailing lists <tamouse.li...@gmail.com> 
wrote:
> I use the include("template") method for this alla time, it works
> great. Most especially for HTML emails coming from a web site to a
> group of users, just slick as anything. include does basically just
> what your print_greeting function does less the actual printout, but
> using php variables instead of a str_replace. Also, this way the
> templates can be stored elsewhere, outside the actual code base if
> need be.

This sounds like it might be what I'm looking for. If I'm understanding
correctly, you are saying to use the include function to "read" in my
greeting file. I think I've got the basic gist of the concept, and will see
what I can hobble together real quick. This thought had not occurred
to me.

Thanks!
                                          

--- End Message ---
--- Begin Message ---
On 12-12-31 03:37 PM, Nelson Green wrote:
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.
                                        
The reason you get $user_name printed is because of the way you are populating the variable $message. You need to have $user_name embedded in double quotes or a HEREDOC when PHP parses $messsage. And $user_name has to have already been defined.

Here is a sample from one of my sites. It is a simple one for the contact page.

=====contact.php====

<?php
$thispage = "Contact";
$contenttop = "<p>$thispage</p>";
$contentbody = <<<HEREDOC
<p>step...@roissy.ca</p>
HEREDOC;

require_once "include.php";
require_once "utilities.php";

echo $header . $markup;

=================

The common stuff for every page is defined in the file "include.php". I define the variable $markup in that file.

Here is the definition for $markup

$markup=<<<HEREDOC

<body>
  <div id="all">

    <div id="top">
      <img src=$titlepng alt=$title />
    </div>

    <div id="main">

      <div id="mainrow">

        <div id="left">
          $leftimage

          <div id="mainnav">

            <ul>
              $menu
            </ul>

          </div>

        </div>

        <div id="content">
          <div id="content-top">
            $contenttop
          </div>
          <div id="content-body">
            $contentbody
          </div>
          <div id="content-bottom">
          </div>
        </div>

      </div>
    </div>

    <div id="footer">
      $copyright
    </div>

  </div>

</body>
</html>
HEREDOC;

There is lots more code, but this is the important stuff.

By using require_once instead of fopen and fread, I have simpler code and PHP evaluates the embedded variables in $markup without any need to use string functions.

In your case, I would make the file greeter.php

=====greeter.php===
$message = "Hello $user_name. How are you today?"
===============

You replace the fopen and fread stuff with a require_once function and $message gets included and the user name resolved all in one line of code.

Hope this helps

--
Stephen


--- End Message ---
--- Begin Message ---
On Mon, Dec 31, 2012 at 5:47 PM, Stephen <stephe...@rogers.com> wrote:
> The common stuff for every page is defined in the file "include.php". I
> define the variable $markup in that file.
>
> Here is the definition for $markup
>
> $markup=<<<HEREDOC
[[snippy]]
> HEREDOC;
>
> By using require_once instead of fopen and fread, I have simpler code and
> PHP evaluates the embedded variables in $markup without any need to use
> string functions.

While using the *_once works in many cases, if you're doing a mass
mailing kind of thing, you want to use the standard include/require so
you can re-include it as your variables change:

foreach ($customers as $customer) {
  $fullname = $customer['fullname'];
  $address = $customer['address'];
  // and so on
  include("mailing.php");
  process($mailing,$customer);
}

where mailing.php defines the $mailing variable as the content that
got included and substituted.

(Back before I came up with this, I was starting off in the same place
as the OP -- and then I just realized "wait --- PHP *IS* a templating
system!" a voila)

--- End Message ---
--- Begin Message ---
> While using the *_once works in many cases, if you're doing a mass
> mailing kind of thing, you want to use the standard include/require so
> you can re-include it as your variables change:
>
> foreach ($customers as $customer) {
> $fullname = $customer['fullname'];
> $address = $customer['address'];
> // and so on
> include("mailing.php");
> process($mailing,$customer);
> }
>
> where mailing.php defines the $mailing variable as the content that
> got included and substituted.
>
> (Back before I came up with this, I was starting off in the same place
> as the OP -- and then I just realized "wait --- PHP *IS* a templating
> system!" a voila)

For what I am trying to do here, I think what I'm doing will suffice. Like you
did, I am going through the learning process. What I've read about templating
has whetted my appetite for bigger and better things, but I should probably
finish this first. Right now I'm just trying to personalize a rather dry
page just a bit.

Thanks!
                                          

--- End Message ---
--- Begin Message ---

On Dec 31, 2012, at 3:36 AM, Ashley Sheridan wrote:



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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



That does make more sense now that you say.
I guess I would want or expect the discount to apply to the whole thing.
Thanks Ash.

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com


--- End Message ---
--- Begin Message ---
On Mon, 31 Dec 2012 1On Mon, 31 Dec 2012 17:47:00 Stephen D wrote:

> By using require_once instead of fopen and fread, I have simpler code
> and PHP evaluates the embedded variables in $markup without any need to
> use string functions.
>
> In your case, I would make the file greeter.php
>
> =====greeter.php===
> $message = "Hello $user_name. How are you today?"
> ===============
>
> You replace the fopen and fread stuff with a require_once function and
> $message gets included and the user name resolved all in one line of code.
>
> Hope this helps
>
> --
> Stephen

Aha! Thanks for the more detailed explanation. I totally misunderstood your
first reply. I understood why I was seeing "$user_name" instead of "Nelson",
but I did not understand what you were trying to tell me.

My greeter file is now:
<?php
$message=<<<HEREDOC
Hello $user_name.
How are you today?
HEREDOC;
?>

And my function is simply:
function print_greeting($user_name)
{
   require_once GREETER_FILE;
   print($message);
}

Now when I call the function, passing the string "Nelson", it is printing
exactly as I wanted. I used a HEREDOC in the GREETER_FILE because I want to
make it more than that one message, and I want to preserve the line breaks
and formatting when I do.

>From what I've been reading based on other replies, I think templating
will be the way for me to go at some point. I am trying to design a fairly
simple site that will be used internally as a front-end to a simple inventory
database that I've designed. The back-end works fine, but command line result
sets don't appeal to others, so I've embarked on a front-end design. Your
help has gotten me over this step and I can start to expand things. I find
I learn best when I start with some basic functionality and build it up. My
first incarnation was to simply build a static page that output exactly what
I put into it. Once I got that going, I then broke parts off into functions,
and then added parameter passing to the functions. I've already explained
where it went from there to here.

Not to detract from the other replies, but you have lead me to exactly what
I was looking for at the moment. I find smarty (and now a couple of others
picked up from reading about smarty) to be fascinating stuff, but I'm nowhere
near that point yet.

Thanks a million!
Nelson
                                          

--- End Message ---

Reply via email to