Welcome Guest, Not a member yet? Register   Sign In
not sure how to deal with this array in cart class
#1

[eluser]FireMe[/eluser]
OK, let me lay it out I am using the cart class, for my cart the cart lays out the content in an array like this.


Code:
Array ( [1679091c5a880faf6fb5e6087eb1b2dc] => Array (

                                        [rowid] => 1679091c5a880faf6fb5e6087eb1b2dc
                                        [id] => 6
                                        [qty] => 1
                                        [price] => 19.99
                                        [name] => 25
                                        [product_code] => 00006
                                        [subtotal] => 19.99 )

          [6512bd43d9caa6e02c990b0a82652dca] => Array (

                                         [rowid] => 6512bd43d9caa6e02c990b0a82652dca
                                         [id] => 11
                                         [qty] => 1
                                         [price] => 5.99
                                         [name] => 26
                                         [product_code] => 000011
                                         [subtotal] => 5.99 )
)

ok so it outputs like this as
Code:
print_r ($this->cart->contents());

I am using a paypal lib that needs the contents to be output like this

Code:
$this->paypal->add_field( 'item_name', "test");
$this->paypal->add_field( 'amount', '19.99' );
$this->paypal->add_field( 'quantity', '1');

and as I am new to php and codeigniter I am struggling to work out how to work with this multidimensional array?
#2

[eluser]PravinS[/eluser]
you can use foreach

Code:
foreach($this->cart->contents() as $cart_key => $cart_values)
{
       echo  $cart_values['rowid']."<br>";
       echo  $cart_values['id']."<br>";
       echo  $cart_values['qty']."<br>";
       echo  $cart_values['price']."<br>";
       echo  $cart_values['name']."<br>";
       echo  $cart_values['product_code']."<br>";
       echo  $cart_values['subtotal']."<br>";
}
#3

[eluser]FireMe[/eluser]
thanks for you reply on this seems to be working, but I seem to have come up with another issue

My code now
Code:
foreach($this->cart->contents() as $cart_key => $cart_values)
   {
          $this->paypal->add_field( 'item_name', $cart_values['name']);
          $this->paypal->add_field( 'amount', $cart_values['price']);
          $this->paypal->add_field( 'quantity', $cart_values['qty']);
          $this->paypal->add_field( 'item_number', $cart_values['product_code']);
   }

The problem is that paypal cart seems to want multiple items output like this

Code:
'item_name_1' 'item name'
'amount_1' '13.99'
'quantity_1' '1'
'item_number_1' '9393993'


'item_name_2' 'another name'
'amount_2' '5.99'
'quantity_2' '1'
'item_number_2' '040440'


'item_name_3' 'and another'
'amount_3' '1.99'
'quantity_3' '2'
'item_number_3' '300303'


etc
etc



how would i get each field to increment by 1 in its name e.g. name_1 name_2 name_3 etc etc

Thanks
#4

[eluser]FireMe[/eluser]
I thought I might of been able to do some thing like this, but it doesn't seem to be working, maybe I got the incorrect syntax or something?

Code:
$i = 1;
       foreach($this->cart->contents() as $cart_key => $cart_values)
   {
          $this->paypal->add_field( 'item_name_'.$i++, $cart_values['name']);
          $this->paypal->add_field( 'amount_'.$i++, $cart_values['price']);
          $this->paypal->add_field( 'quantity_'.$++, $cart_values['qty']);
          $this->paypal->add_field( 'item_number_'.$i++, $cart_values['product_code']);
   }

with the $i++ but i get syntax error, unexpected '++'

any help with this would be a great help

Thanks
#5

[eluser]noideawhattotypehere[/eluser]
$this->paypal->add_field( 'quantity_'.$++, $cart_values['qty']); - you have $++, without i.

anyway, why not:
Code:
$i = 0;
       foreach($this->cart->contents() as $cart_key => $cart_values)
   {
          $i++;
          $this->paypal->add_field( 'item_name_'.$i, $cart_values['name']);
          $this->paypal->add_field( 'amount_'.$i, $cart_values['price']);
          $this->paypal->add_field( 'quantity_'.$i, $cart_values['qty']);
          $this->paypal->add_field( 'item_number_'.$i, $cart_values['product_code']);
   }
otherwise you would have every field number increased by 1 even for the same item, eg
Item 1:
item_name_1
amount_2
quantity_3
item_number_4

etc.
#6

[eluser]FireMe[/eluser]
Thanks noideawhattotypehere thats done the trick, guess its easy when you know how, thanks to both of you. :-)




Theme © iAndrew 2016 - Forum software by © MyBB