2011-07-09 6 views
1

私はMatthew Pennell(ショッピングカートのチュートリアル)のショッピングカートを持っています。ショッピングカートのスクリプトでpaypalを実装したいと思います。しかし、私は本当に解決できない問題に遭遇します。彼のコードではforeachループを行っているので、私のpaypal "item_name_"と "amount_"は可変でなければなりません。配列内の配列の数を計算する必要があります。私はcount($ content)を使って試しましたが、配列の数は私に与えましたが、結果はカート内の他のすべての行で増えています。 すなわち配列の配列の数を数えます。

1 object return 1 
2 object return 2 , 2 
3 object return 3, 3 ,3 

私は他の機能を逃してしまった場合、私は思ったんだけど、または実際の戻りデータが3,3,3である場合でも、1つだけの結果を取得する方法があります。

最後にpaypal_quantityにはカートに追加する変数がありますか?これらの行が

  <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> 

は、foreachループの内側にあるので、あなたのループであれば二回、エコー数($内容)ためである

<?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> 

答えて

1

。 2回実行され、22になります。

+0

ya、これは$ content/$ count(content)をエコーアウトすることでわかります。関数内で、変数$ contentは、カートに表示される行数の唯一のキーを保持しているようです。私はIF($ content ==?){variable $ i = 1;}を実行できるかどうか疑問に思っていました。それを$ i ++としようか? – 3cross

0

私はあなたのコードを見て、いくつか改善を提案しました。何度も重複して作業が行われていたように見えました。うまくいけば、私が何をやったのか理解できます。これはおそらく、問題を最小限に抑えるためにはうまくいくようです。

<?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 
     } 
    } 
?> 
+0

提案していただきありがとうございます! Imaは今試してみるつもりですが、他の機能と同じように機能しているかどうかはわかりません。これは他の人たちから得たスクリプトであり、編集作業をしています。 – 3cross

+0

Hm、あなたの方法を試してみました。私は私のフォーム名はpaypal命名規則のためだと信じていますか?とにかく、エコーカウント($コンテンツ)を使用しているときに私のカートの行の数を取得することですので、私は別のinput_name_ "番号"で複数の入力を生成することができます。 – 3cross

1

私はとても愚かだと信じています。ここであなたのすべての時間と助けに感謝する必要があります。しかし、私はこれを本当に簡単に解決することができます。

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