【问题标题】:How to combine php & python code in Python如何在 Python 中结合 php 和 python 代码
【发布时间】:2013-04-23 16:08:35
【问题描述】:

我正在寻找一种将我的 PHP 代码合并到我的 Python 文件中的方法。也许很奇怪,但对我来说这会很有帮助。我只是想要这部分 PHP 代码在 Python 中,因为我不知道如何用 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();
?>

【问题讨论】:

  • 您的 PHP 代码引用了 $_POST 和 $_SESSION,这表明它应该在 http 服务器的上下文中运行。 Python 如何适应这种设置?
  • 他想把它改写成python

标签: php python forms session python-2.7


【解决方案1】:

这是我使用web.py 编写的 Python Web 应用程序,它近似于一个可以清除的计数器。也许它可以帮助您移植 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()

您的 PHP 示例中缺少很多内容来涵盖您问题中的每个功能。例如$order 是如何填充的,$refname 包含什么样的值?

【讨论】:

  • 您好,感谢您的反应!!但是,我仍在寻找解决方案。 “订单”是用户在想要订购特定数量的商品时选择的数字。 'Refname' 是产品的参考编号。我正在使用一个表单(method="POST"),其中“order”是用户选择的数字,“refname”是按下“提交”按钮时应该传递的参考编号。我正在寻找 cgi 模块来发布和获取这些数字到购物车和购物车的柜台,但它不起作用:(
猜你喜欢
  • 2018-07-21
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
相关资源
最近更新 更多