How to pass a variable from one module to another and then into mysql

Posted on 2007-10-06
Last Modified: 2013-12-13
I'm customizing Zen Cart, an open source shopping cart program based on PHP and MySQL.  I really don't know too much about PHP... just enough to alter some code but not really write it. So I'm not sure what you need to help me out, so I'll include as much as I can reckon matters.

I have a variable ($shipping_methods_types) inside a function called quote within a class / module called ups.  $shipping_methods_types contains the available UPS methods (not the chosen method - the available methods). For example, it'll contain: 1DA,2DA,GND.

I want to store that value in a field called shipping_allowed in the orders table.

It looks like the place to insert it is in a function called create within a class called order.  When the customer places the order, it seems that code runs.  There's a portion of the function that seems to take all the variables and insert it into the orders table through $sql_data_array. I've been able to put in a dummy value into the shipping_allowed field by adding this code: $sql_data_array[shipping_allowed] = 'dummy value';  after the long the list of other variables. Just a snippet of that code:

    $sql_data_array = array('customers_id' => $_SESSION['customer_id'],
                            'customers_name' => $this->customer['firstname'] . ' ' . $this->customer['lastname'],
                            'customers_company' => $this->customer['company'],
                            'ip_address' => $_SESSION['customers_ip_address'] . ' - ' . $_SERVER['REMOTE_ADDR']

    $sql_data_array[shipping_allowed] = 'dummy value';

    zen_db_perform(TABLE_ORDERS, $sql_data_array);

My problem is that I don't know how to pass the real value of $shipping_methods_types from the UPS module to the create function.
Question by:JoeFletcher
    LVL 21

    Accepted Solution

    How about using a session variable to temporary store your variable $shipping_methods_types:

    $_SESSION["temp_shipping_method_types"] = $shipping_methods_types;

    Then getting it out again in the create function.

    Let me know how you go.
    LVL 1

    Author Comment

    Dude, you rock. That worked! I was hoping there'd be an easy way like that.  One last question: what if there's no value (null?) for $shipping_methods_types?  Will it make a difference?  For example when I store, and/or when I call it again, which is:

    $sql_data_array = array('customers_id' => $_SESSION['customer_id'],
          'shipping_allowed' => $_SESSION['temp_shipping_method_types'],
    zen_db_perform(TABLE_ORDERS, $sql_data_array);
    LVL 21

    Expert Comment

    Glad it worked. You can additionally add an IF statement to check whether $shipping_methods_types is null:

    if($_SESSION["temp_shipping_method_types"] != null)
    // Do what you need to do here...

    It should not make a difference though.

    Also you can set a default value for $shipping_methods_types, as below:

    if($_SESSION["temp_shipping_method_types"] == null)
    $_SESSION["temp_shipping_method_types"] = "N/A";

    Therefore, these always a value in shipping_method_types, and the string value "N/A" represents that the user has not selected anything.

    Hope that clarifies it up.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now