Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I’m looking for a way to combine my PHP code into my Python file. Maybe strange, but for me it would be quite helpfull. I simply want this part of PHP-code in Python because I don’t know how to rewrite it in Python :(

  function counter_winkelkar()
  {
    global $order;         
    global $refname;

if(isset( $_POST['clearknop'] )) 
{
    $_SESSION['count']= '0'; 
    unset($_SESSION['array_products']);   
    unset($_SESSION['array_counting_products']);
}

if (isset( $_POST['refname'] ))
{
    $_SESSION['count']= $_SESSION['count'] + $order;        
    print("$_SESSION[count]");
}
}

function products_winkelkar()
{
global $order;          
global $db;
global $refname;

if (!isset($_SESSION['array_products']) ) 
{
    $_SESSION['array_products'] = Array();
    $_SESSION['array_counting_products'] = Array();
}

if ($order != 'number' ) 
{
    array_push($_SESSION['array_products'],$refname);
    array_push($_SESSION['array_counting_products'],$order);
}   
}

function winkelkar()
{
counter_winkelkar();
products_winkelkar();
}

winkelkar();
?>
share|improve this question
1  
Your PHP code is referencing $_POST and $_SESSION which says that it is expected to run in the context of a http server. How does Python fit into this set up? –  Jason Sperske Apr 29 '13 at 20:54
    
He wants it rewritten as python –  Ryan Naddy Apr 29 '13 at 20:54

1 Answer 1

Here is a Python web app I coded up using web.py that approximates a counter that can be cleared. Maybe it can help you port over your PHP:

import web

_SESSION = {}

urls = (
    '/', 'index'
)
app = web.application(urls, globals())

class index:        
    def POST(self):
        if 'clearknop' in web.input():
            _SESSION['count'] = 0
        else:
            if 'count' not in _SESSION: 
                _SESSION['count'] = 0
            _SESSION['count'] = _SESSION['count'] + 1

        return _SESSION['count']

    def GET(self):
        return """<form method='POST' action='/'>
                    <input type='checkbox' name='clearknop'/> Clear
                    <input type='submit'/>
                  </form>"""

if __name__ == "__main__":
    app.run()

There is a lot missing form your PHP example to cover every feature in your question. For instance how is $order populated and what kind of value does $refname contain?

share|improve this answer
    
Hi, thank you for your reaction!! However I'm still looking for a solution. 'Order' is a number that the user selects when he wants to order a specific number of items. 'Refname' is the reference number of the product. I'm using a form (method="POST") where 'order' is the number selected by the user and 'refname' is the reference number that should be passed when pressing the "submit" button. I was looking for the cgi module to POST and GET these numbers to the shopping cart and the counter of the shopping cart but it won't work :( –  Thomas1207 May 4 '13 at 15:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.