Php 计数帮助,用于计算数组中的数组数

Php 计数帮助,用于计算数组中的数组数,php,paypal,cart,Php,Paypal,Cart,我从Matthew Pennell(购物车教程)那里得到了一个购物车,在他的购物车脚本中,我想实现paypal。然而,我遇到了一个我不能真正解决的问题。由于在他的代码中,他正在执行foreach循环,因此我的paypal“item\u name\uuu”和“amount\uu”必须是可变的。我需要计算一个数组中有多少个数组。我尝试使用count($content),它确实给了我数组的数量,但是,结果是我的购物车中每隔一行的数量都在增加。 即 我想知道我是否错过了其他函数,或者即使实际返回数据是3

我从Matthew Pennell(购物车教程)那里得到了一个购物车,在他的购物车脚本中,我想实现paypal。然而,我遇到了一个我不能真正解决的问题。由于在他的代码中,他正在执行foreach循环,因此我的paypal“item\u name\uuu”和“amount\uu”必须是可变的。我需要计算一个数组中有多少个数组。我尝试使用count($content),它确实给了我数组的数量,但是,结果是我的购物车中每隔一行的数量都在增加。 即

我想知道我是否错过了其他函数,或者即使实际返回数据是3,3,3,是否有办法只得到1个结果

最后,对于paypal_数量,是否存在添加到购物车的变量

<?php
    $i = 1;
    function checkout() {
        global $db;
        $cart = $_SESSION['cart'];
        if ($cart) {
            $items = explode(',', $cart);
            $contents = array();
            foreach ($items as $item) {
                $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
            }
    ?>

            <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
                <input type="hidden" name="cmd" value="_cart"></input>
                <input type="hidden" name="upload" value="1"></input>
                <input type="hidden" name="business" value="my_email.com"></input>
        <?php


            foreach ($contents as $id => $qty) {

                echo $contents;

                $sql = 'SELECT * FROM books WHERE id = ' . $id;
                $result = $db->query($sql);
                $row = $result->fetch();
                extract($row);
        ?>
                <input type="hidden" name="item_name_<?php echo count($contents); ?>" value="<?php echo $title; ?>"></input>
                <input type="hidden" name="amount_<?php echo count($contents); ?>" value="<?php echo $price; ?>"></input>
                <input type="hidden" name="quantity" value="<?php echo $qty; ?>"></input>
        <?php
            }
        ?>
            <input type="submit" value="PayPal"></input>

        </form>


那是因为这些线

            <input type="hidden" name="item_name_<?php echo count($contents); ?>" value="<?php echo $title; ?>"></input>
            <input type="hidden" name="amount_<?php echo count($contents); ?>" value="<?php echo $price; ?>"></input>

我查看了您的代码并提出了一些改进建议。看起来有一些重复的工作正在进行,希望你能理解我所做的帮助。看起来这可能会很好地解决您的问题,在最低限度

<?php
    $i = 1; // not used in the below function.
    function checkout() {
        global $db;
        // check for isset, it is more defensive and PHP is less inclined to issue a warning.
        if( isset( $_SESSION['cart'] ) && $_SESSION['cart'] ) {
            $items = explode( ',', $_SESSION['cart'] );
            // array_count_values is pretty cool.
            // it does exactly what your first for loop did.
            $contents = array_count_values( $items );                 
    ?>
            <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
                <input type="hidden" name="cmd" value="_cart"></input>
                <input type="hidden" name="upload" value="1"></input>
                <input type="hidden" name="business" value="my_email.com"></input>
        <?php

            foreach ($contents as $id => $qty) {
                // echo $contents; <!-- this should echo 'Array' continually
                $sql = 'SELECT * FROM books WHERE id = ' . $id;
                $result = $db->query($sql);
                $row = $result->fetch();
                // extract is normally not the best practice, frequently it leads to accidental replacement of 
                // important variables -- if `books` had a `contents` column or quantity, that would be no good.
                // so I've replaced it with what I expect are the keys to the array.
        ?>
                <?php
                  /* 
                     A caution about hidden inputs. They can be modified by the client, so if you were to, say,
                     trust the price listed below and your client had no scruples, your client could simply set
                     that value to, say, $0.01. Or worse, free!
                  */
                  /* 
                     I've changed up your input naming convention just slightly (it is easy to fix, but hear me
                     out first). You've used something which will render <name-1>_1, <name-1>_2... which means
                     that your $_POST (If you're buying something with this form, $_POST really is your better 
                     bet) will have $_POST[<name-1>_1], $_POST[<name-1>_2]... In order to get all of the different
                     products grouped properly, you'll actually need to parse the $_POST indexes... it will get 
                     messy. It's doable, but it will be annoying.

                     Instead, I put the naming convention <name-1>[1], <name-2>[2]. This means that $_POST will 
                     have an array for each of the <names>. This means that you can do this:

                     $quantity  = ""; 
                     $ammount   = "";
                     foreach( $_POST[ 'item_name' ] as $key => $item )
                     {
                        $quantity = $_POST[ 'quantity' ][ $key ];
                        $ammount  = $_POST[ 'ammount'  ][ $key ];
                        // you now have all three quickly and easily with no string parsing! Set it and forget it!
                     }
                  */
                ?>
                <input type="hidden" name="item_name[<?php 
                      // before you were using count($contents) here. That would mean that everything would have 
                      // the same name and you'd only get one value back in $_REQUEST. I think you meant ID.
                      echo $id; 
                ?>]" value="<?php echo $row['title']; ?>"></input>
                <input type="hidden" name="amount[<?php echo $id; ?>" value="<?php 
                      // ammount may not be your best choice of input name for something that refers to price.
                      // I know that when I look at it, I expect that to refer to quantity and not to cost
                      // and your first job as a developer is writing code which is as obvious as possible.
                      // But that is more stylistic than not so feel free to disregard
                      echo $row['price']; 
                ?>]"></input>
                <input type="hidden" name="quantity[<?php 
                      // I took the liberty of adding id to this input as well -- otherwise you'd only have one 
                      // quantity
                      echo $id; 
                ?>]" value="<?php echo $qty; ?>"></input>
        <?php
            }
        ?>
            <input type="submit" value="PayPal"></input>
        </form>
<?php
        }
    }
?>


真不敢相信我这么笨。这里要说的是非常感谢你的时间和帮助。但我自己很容易就解决了这个问题

 foreach ($contents as $id => $qty) {
                $rowid++;
                $sql = 'SELECT * FROM books WHERE id = ' . $id;
                $result = $db->query($sql);
                $row = $result->fetch();
                extract($row);
                echo $rowid;

是的,我通过回显$content/$count(content)得出了这个结论。在函数中,$content变量似乎是显示在我的购物车上的行数的唯一键。但是我不知道如何通过只显示一次来解决它。我想知道我是否可以做一个if($content==?){variable$i=1;并让它显示$i++}?谢谢你的建议!我现在要试试,但不知道它是否能用,因为还有其他功能,因为这是我从别人那里得到的一个脚本,我对它做了一些编辑工作。嗯,试过你的方法。我相信我的表单名是为了贝宝命名约定?无论如何,Paypal无法检测购物车中的任何项目,以前当我使用echo count($content)获取购物车中的行数时,我可以使用不同的输入名生成多个输入。
 foreach ($contents as $id => $qty) {
                $rowid++;
                $sql = 'SELECT * FROM books WHERE id = ' . $id;
                $result = $db->query($sql);
                $row = $result->fetch();
                extract($row);
                echo $rowid;