我具有以下功能,这些功能基于shoppingCart类生成购物车。

function render_shopping_cart()
{
    $shopping_cart = get_shopping_cart();

    $output = "<table class='shoppingCart'>
                <tr>
                    <th>
                        Course
                    </th>
                    <th>
                        Valid Until
                    </th>
                    <th>
                        Quantity
                    </th>
                                <th>
                        Price
                    </th>
                </tr>
    ";
    $line_item_counter = 1;
    foreach ($shopping_cart->GetItems() as $product_id)
    {
          $output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter);
          $line_item_counter++;
    }


    //$output .= render_shopping_cart_shipping_row($shopping_cart);

    $output .= render_shopping_cart_total_row($shopping_cart);


    $output .="</table>";

    return $output;
}
function render_shopping_cart_total_row(ShoppingCart $shopping_cart)
{
    return  "<tr>
                <td>
                </td>
                        <td>
                        </td>
                <td>
                Total:
                </td>
                <td>
                <input type='hidden' name='no_shipping' value='0'>
                $".$shopping_cart->GetTotal()."
                </td>
             </tr>";

}

function render_shopping_cart_row(ShoppingCart $shopping_cart , $product_id, $line_item_counter)
{
    $quantity = $shopping_cart->GetItemQuantity($product_id);
    $amount = $shopping_cart->GetItemCost($product_id);
    $unit_cost = get_item_cost($product_id);
    $shipping_amount = $shopping_cart->GetItemShippingCost($product_id);
    $title = get_item_title($product_id);
    $validUntil = expiration();




    return "
            <tr>
                <td>
                    $title
                    <input type='hidden' name='item_name_$line_item_counter' value='$product_id' />
                </td>
                <td>
                    $validUntil
                    <input type='hidden' name='quantity_$line_item_counter' value='$validUntil' />
                </td>
                     <td>
                    $quantity
                    <input type='hidden' name='quantity_$line_item_counter' value='$quantity' />
                </td>
                <td>
                    $$amount
                    <input type='hidden' name='amount_$line_item_counter' value='$unit_cost' />

                    </td>

            </tr>
     ";
}


我要弄清楚的是如何采用为每个项目生成的$product_id。特别是这部分$title<input type='hidden' name='item_name_$line_item_counter' value='$product_id' />并将其放入$ sqlProducts数组中,或将它们分别作为product1, product2, product3, etc插入到单个mysql字段中

我已经添加

      $sqlProducts = serialize($product_id);
      echo unserialize($sqlProducts);


到line_item计数器看起来像

 $line_item_counter = 1;
        foreach ($shopping_cart->GetItems() as $product_id)
        {
              $output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter);
              $line_item_counter++;

      $sqlProducts = serialize($product_id);
      echo unserialize($sqlProducts); // I've made $abc = unserialize($sqlProducts) as well and it hasn't worked.

        }


它们在函数中很好地呼应,但是我无法在函数外部访问变量。提交表单后,我需要一些如何将$sqlProducts传递给process.php。或者是否存在另一种方法来获取生成的$product_id数组,并在表单发布后将其插入到mysql表中。

更新

我试过了

      $sqlProducts = serialize($product_id);
      $_SESSION['sqlProducts'] = unserialize($sqlProducts);


可以,但是仅回显数组中的最后一项而不是整个内容。

在页面中,

          $sqlProducts = serialize($product_id);
          $_SESSION['sqlProducts'] = unserialize($sqlProducts);
          echo $_SESSION['sqlProducts'];


工作良好

目前,该表单发布到process.php,不幸的是,当我回显echo $_SESSION时,该行仅包含echo $sqlProducts$sqlProducts的一行,该值是未定义的,如果我回显$_SESSION['sqlProducts'],则只能得到数组

对于任何其他有此问题的人,建议使用以下解决方案

我重写了函数并创建了数组:

function createCart()
{

    //Create a new cart as a session variable with the value being an array
    $_SESSION['paypalCart'] = array();

}

function insertToCart($productID, $productName, $price, $qty = 1)
{

    //Function is run when a user presses an add to cart button

    //Check if the product ID exists in the paypal cart array
    if(array_key_exists($productID, $_SESSION['paypalCart']))
    {

        //Calculate new total based on current quantity
        $newTotal = $_SESSION['paypalCart'][$productID]['qty'] + $qty;

        //Update the product quantity with the new total of products
        $_SESSION['paypalCart'][$productID]['qty'] = $newTotal;

    }
    else
    {

        //If the product doesn't exist in the cart array then add the product
        $_SESSION['paypalCart'][$productID]['ID'] = $productID;
        $_SESSION['paypalCart'][$productID]['name'] = $productName;
        $_SESSION['paypalCart'][$productID]['price'] = $price;
        $_SESSION['paypalCart'][$productID]['qty'] = $qty;

    }

}


现在我可以使用

<?php
            if (isset ($_SESSION['paypalCart']))
            {
                foreach($_SESSION['paypalCart'] as $product)
                    {
                    $custom .= $product['name'].", ";


                    }
            }
            ?>
            <input type="hidden" name="custom" value="<?php echo $custom?>">


只需记住在调用变量之前在页面中的某处初始化$custom(或正在使用的任何内容)即可,例如:$custom:"";

最佳答案

您自己说过,产品ID已经在隐藏的输入中,这意味着在提交表单时可以通过process.php中的$_POST数组对其进行访问,我只是将输入重命名,以便它们创建多维数组,以便于处理,例如:

return "
        <tr>
            <td>
                $title
                <input type='hidden' name='items[$line_item_counter][id]' value='$product_id' />
            </td>
            <td>
                $validUntil
                <input type='hidden' name='items[$line_item_counter][valid_until]' value='$validUntil' />
            </td>
                 <td>
                $quantity
                <input type='hidden' name='items[$line_item_counter][quantity]' value='$quantity' />
            </td>
            <td>
                $$amount
                <input type='hidden' name='items[$line_item_counter][amount]' value='$unit_cost' />

                </td>

        </tr>
 ";


然后在process.php中,您可以访问以下项的数组:

$items = isset($_POST['items']) ? $_POST['items'] : array();


要获取ID,您可以执行以下操作:

if(count($items)){//Check if array is not empty
    $ids = array_map(function($item){return $item['id'];},$items); //get ids array
    $sql_ids = implode(',',$ids); //format a string like id1,id2,id3...
}
//*Note that anonymous functions are only supported starting from PHP 5.3


现在,如果您不想重命名输入,则可以使用id创建另一个隐藏字段:

$line_item_counter = 1;
$product_ids = $shopping_cart->GetItems();
foreach ($product_ids as $product_id)
{
      $output .= render_shopping_cart_row($shopping_cart , $product_id, $line_item_counter);
      $line_item_counter++;
}
$sqlProducts = implode(',',$ids);
$output.= "<input type='hidden' name='product_ids' value='$sqlProducts' />";


并使用$_POST['product_ids']访问它

07-24 21:06