如何使用order_id作为发票号码并将任何变量转换为全局变量 [英] How to used order_id as invoice number and converting any variable as gloabal

查看:95
本文介绍了如何使用order_id作为发票号码并将任何变量转换为全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次尝试过,但已经被拖了很长时间,好心的提到哪种方法更好,S_SESSION或GET方法将某些变量用作全局变量。以及如何

  $ date = date('Y-m-d H:i:s'); 

将其转换为SESSION [],或者将其转换为全局以在其他页面上访问。其他的事情是如何访问

  $ i 

用作order_id,我想用它作为我的发票号码。如何可能代码如下

 <?php include('header.php'); 
if(isset($ _ POST ['order']))
{
$ user_id = $ _SESSION ['id'];
$ date = date('Y-m-d H:i:s');
$ username = $ _ SESSION ['username'];
mysql_query(INSERT INTO tborder(user_id,`date`,username)VALUES('$ user_id','$ date','$ username'))或die(mysql_error());
$ i = mysql_insert_id();
if($ i> 0)
for($ l = 0; $ l< count($ _ POST ['product_id']); $ l ++)
{
$ product_id = $ _POST ['product_id'] [$ l];
$ quantity = $ _POST ['quantity'] [$ l];
$ price = $ _POST ['price'] [$ l];
$ discount = $ _POST ['discount'] [$ l];
$ amount = $ _POST ['amount'] [$ l];
$ username = $ _SESSION ['username'];
$ user_id = $ _SESSION ['id'];
$ b mysql_query(INSERT INTO tborderdetail(order_id,product_id,quantity,price,discount,amount,username,user_id)VALUES('$ i','$ product_id','$ quantity','$价格','$折扣','$金额','$用户名','$ user_id'))或死亡(mysql_error());
header('location:order.php');
}
}
?>
< form action =method =post>

< div class =row>
< div class =col-md-6>

< div class =panel panel-default>
< div id =Tabsrole =tabpanel>
<!--- ---导航标签 - >

< ul class =nav nav-tabsrole =tablist>

<?php
$ q = mysql_query(SELECT * FROM tbcategory);
while($ row = mysql_fetch_object($ q))
{
?>
< li class =>
< a href =#<?= $ row-> id?> aria-controls =personalrole =tabdata-toggle =tab><?= $ row-> category_name?>< / a>
< / li>
<?php
}
?>
< / ul>
<! - 标签窗格 - >
< div class =tab-contentstyle =padding-top:20px>
<?php
$ q = mysql_query(SELECT * FROM tbcategory);
while($ row = mysql_fetch_object($ q))
{
?>
< div role =tabpanelclass =tab-paneid =<?= $ row-> id?>>
<?php
$ p = mysql_query(SELECT * FROM tbproduct WHERE cat_id ='$ row-> id');
while($ r = mysql_fetch_object($ p))
{
?>
< div class =col-md-4>
< a class =datavaluehref =#data-id =<?= $ r-> id?>数据名称= < = $ R-> PRODUCT_NAME>? data-price =<?= $ r-> unit_price?>>
< img width =100height =100src =product /<?= $ r-> image?>>
< h5><?= $ r-> product_name?>< / h5>
< / a>
< / div>
<?php
}
?>
< / div>
<?php
}
?>
< / div>
< / div>
< / div>


< / div>

< div class =col-md-6>
< table class =table>
< center>< h2>我的产品< / h2>< / center>

< tr>
<?php
date_default_timezone_set('Asia / Karachi');
$ currenttime = date('h:i A');
?>
< td>
< label> Inv Date:< / label>
<?= date('d-M-Y'); ?>
< / td>
< td>

< label> Inv Time:< / label>
<?= $ currenttime?>
< / td>
< / tr>
< tr>
< td>
< label> Inovice No.< / label>

< / td>
< td>
< label>使用者名称< / label>
<?= $ _ SESSION ['username']?>
< / td>
< / tr>
< / table>
< table class =table table-bordered table-hover>
< thead>
< tr>
< th> ProductName< / th>
< th>数量< / th>
< th>价格< / th>
< th>折扣< / th>
< th>金额< / th>
th X< / th>
< / tr>
< / thead>
< tbody class =details>
< / tbody>
< tfoot>
< tr>
< td>
SubTotal:< input type =textname =subtotalclass =form-control subtotal>
现金接收:< input type =textname =payclass =form-control pay>
回款:< input type =textname =paybackclass =form-control payback>
< / td>
< / tr>
< / tfoot>
< / table>
< input type =submitname =ordervalue =Orderclass =btn btn-primary>

< / div>
< / div>
< / form>


<?php include('footer.php');?>


解决方案使用 $ _ SESSION ,你必须在你的php代码启动前运行 session_start();

 <?php 
session_start();

if(isset($ _ POST ['order']))
{
$ user_id = $ _SESSION ['id'];
$ date = date('Y-m-d H:i:s');
$ username = $ _SESSION ['username'];
mysql_query(INSERT INTO tborder(user_id,`date`,username)VALUES('$ user_id','$ date','$ username'))或die(mysql_error());
$ i = mysql_insert_id();
if($ i> 0)
for($ l = 0; $ l< count($ _ POST ['product_id']); $ l ++)
{
$ product_id = $ _POST ['product_id'] [$ l];
$ quantity = $ _POST ['quantity'] [$ l];
$ price = $ _POST ['price'] [$ l];
$ discount = $ _POST ['discount'] [$ l];
$ amount = $ _POST ['amount'] [$ l];
$ username = $ _SESSION ['username'];
$ user_id = $ _SESSION ['id'];

if(!isset($ _ SESSION ['product_cart']))
$ _SESSION ['product_cart'] = [];

if(!isset($ _ SESSION ['product_cart'] ['products']))
$ _SESSION ['product_cart'] ['products'] = [];

//将产品数组保存到$ _SESSION中
$ _SESSION ['product_cart'] ['products'] [] = [
'id'=> $ PRODUCT_ID;
'quantity'=> $数量;
'price'=> $价格;
'discount'=> $折扣;
'amount'=> $量;
'username'=> $的用户名;
'user_id'=> $ USER_ID;
];

//我不明白
是什么查询mysql_query(INSERT INTO tborderdetail(order_id,product_id,quantity,price,discount,amount,username,user_id)VALUES(' $ i','$ product_id','$ quantity','$ price','$ discount','$ amount','$ username','$ user_id'))或die(mysql_error());

}

//在循环结束后重定向,
//除非它只处理第一个数组元素
header('location:order。 PHP');
}

//在你不确定你会留在这个页面之前,不要包含html模板
include('header.php');


I tried multiple times but have been stucked for long time, kindly mention that which method is better S_SESSION or GET Method to used some variable as global. And how to

$date = date('Y-m-d H:i:s');

convert this into SESSION[] ,or as global to access on other pages. Other thing is that how to acces

$i

which is used as order_id , i wanna to used this as my invoice number. How can it is possible The code is give below

<?php include ('header.php'); 
if(isset($_POST['order']))
{
    $user_id  = $_SESSION['id'];
     $date     = date('Y-m-d H:i:s');
    $username =$_SESSION['username'];
    mysql_query("INSERT INTO tborder(user_id,`date`,username) VALUES('$user_id','$date','$username')") or die(mysql_error());
    $i = mysql_insert_id();
    if($i >0)
        for($l=0;$l<count($_POST['product_id']);$l++)
        {
            $product_id = $_POST['product_id'][$l];
            $quantity   = $_POST['quantity'][$l];
            $price      = $_POST['price'][$l];
            $discount   = $_POST['discount'][$l];
            $amount     = $_POST['amount'][$l];
            $username   = $_SESSION['username'];
            $user_id    = $_SESSION['id'];

            mysql_query("INSERT INTO tborderdetail(order_id,product_id,quantity,price,discount,amount,username,user_id) VALUES('$i','$product_id','$quantity','$price','$discount','$amount','$username','$user_id')")or die(mysql_error());
            header('location:order.php');
        }
}
?>
<form action="" method="post">

<div class="row">
    <div class="col-md-6">

  <div class="panel panel-default">
    <div id="Tabs" role="tabpanel">
    <!--- Nav Tabs -->

        <ul class="nav nav-tabs" role="tablist">

        <?php
            $q =mysql_query("SELECT * FROM tbcategory");
            while($row =mysql_fetch_object($q))
            {
        ?>  
                <li class="">
                <a href="#<?=$row->id?>" aria-controls="personal" role="tab" data-toggle="tab"><?=$row->category_name?></a>
                </li>
        <?php
            }
        ?>
        </ul>
        <!--Tab Panes-->
        <div class="tab-content" style="padding-top: 20px">
        <?php
            $q=mysql_query("SELECT * FROM tbcategory");
            while($row = mysql_fetch_object($q))
                {
        ?>      
            <div role="tabpanel" class="tab-pane" id="<?=$row->id?>">
                <?php
                $p =mysql_query("SELECT * FROM tbproduct WHERE cat_id = '$row->id'");
                    while($r = mysql_fetch_object($p))
                    {
                    ?>
                    <div class="col-md-4">
                    <a class="datavalue" href="#" data-id="<?=$r->id?>" data-name="<?=$r->product_name?>" data-price="<?=$r->unit_price ?>">
                        <img width="100" height="100" src="product/<?=$r->image?>">
                        <h5><?=$r->product_name?></h5>
                    </a>
                    </div>
                    <?php
                }
                ?>
            </div>
        <?php 
                }
        ?>  
            </div>
        </div>
        </div>


</div>

    <div class="col-md-6">
    <table class="table">
<center><h2>MY PRODUCT</h2></center>

    <tr>
            <?php
               date_default_timezone_set('Asia/Karachi');
               $currenttime = date('h:i A');
            ?>
        <td>
            <label>Inv Date : </label>
            <?= date('d-M-Y'); ?> 
        </td>
        <td>

            <label>Inv Time : </label>
            <?=$currenttime ?> 
        </td>
    </tr>
    <tr>
        <td>
            <label>Inovice No.</label>
                   ????
        </td>
        <td>
            <label>User Name</label>
            <?=$_SESSION['username'] ?>
        </td>
    </tr>
</table>
        <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>ProductName</th>
                <th>Quantity</th>
                <th> Price </th>
                <th>Discount</th>
                <th>Amount</th>
                <th>X</th>
            </tr>
        </thead>
        <tbody class="details">
        </tbody>
        <tfoot>
            <tr>
                <td>
                    SubTotal : <input type="text" name="subtotal" class="form-control subtotal">
                    Cash Receive: <input type="text" name="pay" class="form-control pay">
                    Pay Back : <input type="text" name="payback" class="form-control payback">
                </td>
            </tr>
        </tfoot>    
        </table>
        <input type="submit" name="order" value="Order" class="btn btn-primary">

    </div>
</div>
</form>


<?php include ('footer.php') ;?>

解决方案

To use $_SESSION, you have to run session_start(); before your php code starts:

<?php
  session_start(); 

  if(isset($_POST['order']))
  {
     $user_id  = $_SESSION['id'];
     $date     = date('Y-m-d H:i:s');
     $username = $_SESSION['username'];
     mysql_query("INSERT INTO tborder(user_id,`date`,username) VALUES('$user_id','$date','$username')") or die(mysql_error());
     $i = mysql_insert_id();
     if($i >0)
        for($l=0;$l<count($_POST['product_id']);$l++)
        {
            $product_id = $_POST['product_id'][$l];
            $quantity   = $_POST['quantity'][$l];
            $price      = $_POST['price'][$l];
            $discount   = $_POST['discount'][$l];
            $amount     = $_POST['amount'][$l];
            $username   = $_SESSION['username'];
            $user_id    = $_SESSION['id'];

            if(!isset($_SESSION['product_cart']))
                 $_SESSION['product_cart'] = [];

            if(!isset($_SESSION['product_cart']['products']))
                 $_SESSION['product_cart']['products'] = [];

            // save product to cart products array in $_SESSION
            $_SESSION['product_cart']['products'][] = [
                'id' => $product_id;
                'quantity'  => $quantity;
                'price'     => $price;
                'discount'  => $discount;
                'amount'     => $amount;
                'username'  => $username;
                'user_id'   => $user_id;
            ];

            // I don't get what is this query for
            mysql_query("INSERT INTO tborderdetail(order_id,product_id,quantity,price,discount,amount,username,user_id) VALUES('$i','$product_id','$quantity','$price','$discount','$amount','$username','$user_id')")or die(mysql_error());

         }

        // redirect after loop ends, 
        // unless it'll process only first array element 
        header('location:order.php');
     }

// Do not include html templates before you not sure you'll stay on this page
include('header.php'); 

这篇关于如何使用order_id作为发票号码并将任何变量转换为全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆