Jump to content

Total price from multidimensional array.


rajaroy

Recommended Posts

I am currently building (for my first time) a shoppingcart.

Its an multidimensional array in a session that looks like this.

 

Array
(
    [23] => Array
        (
            [0] => 23
            [1] => 11.32.097.
            [2] => 635
            [3] => 2
            [4] => 89
        )

    [800] => Array
        (
            [0] => 800
            [1] => 20.40.017.
            [2] => 100
            [3] => 1
            [4] => 125
        )

)

 

The 4th key contains the price value. What i am trying to do is get all the sub-array's 4th key value and count them together to get a total price.

I tried doing this with a foreach loop but its not my strong point, sadly.

 

I you have some time can you please take a look how to solve this?

 

 

Thanks in advance,

 

Roy

Link to comment
Share on other sites

@Muddy_Funster

Sorry if I pronounced the sentence wrong. I tried my best to explain my problem, but if its not understandable i can try to explain again.

 

@wepnop

I tried the code but i get a total of 0. I only changed $array to my array name.

Dont know if you had noticed, but the sub-array's keys are the id's of the product. Could it be that the for loop is having difficulties with it when counting ++?

Link to comment
Share on other sites

It's not a problem with the explination, it's that you are writing a shopingcart to run through session variables stored in the PHP.  That just stikes me as the wrong way to do things.  I would have had it run from a database, but that's just me.

 

I think you probably want a foreach():

$total = 0;
foreach ($array as $key => $value) {
    $total = $total + $value;
}

but I could be way off, I don't use arrays too much.

Link to comment
Share on other sites

@Muddy_Funster

Sorry if I pronounced the sentence wrong. I tried my best to explain my problem, but if its not understandable i can try to explain again.

 

@wepnop

I tried the code but i get a total of 0. I only changed $array to my array name.

Dont know if you had noticed, but the sub-array's keys are the id's of the product. Could it be that the for loop is having difficulties with it when counting ++?

 

For what i know, php values in arrays can be accesed using keys or the natural position in they where filled in, so, that have to work, i think.

 

You can use the foreach example also, i wrote a for because you say you get stuck with it.

 

$total = 0;
foreach ($array as $key => $value) {
    $total += $value[4];
}

 

 

Link to comment
Share on other sites

@wepnop

I really really suck at loopings cause for some reason it doenst make sense to me.

But the foreach worked fabulously, thanks  ;)

 

@Muddy_Funster

My first tought was also to use database for shoppingcarts but i thought of the folowing problems that i couldn't think a solution for.

First the user that visits the website is a guest. When he/she is adding products to his/her cart then it would be inserted into database.

But if this guest leave's the website, the cartdata would still be in the database, causing to fill up unnecessarily.

How would you empty the cartdata?

 

I couldn't think of any solutions, maybe you can  :).

Link to comment
Share on other sites

@Muddy_Funster

 

I do agree with you that guest should not be able to create a cart without registering first, unfortunatly the person for whom i am making the website wants it this way.

 

Dont really know much about cronjobs, but i definitely am going to take a look at it for future shoppingcarts.

 

I really am greatfull for your help and information, thanks again. ;)

 

 

Greetings,

 

Roy

Link to comment
Share on other sites

No problem Roy.

...

I do agree with you that guest should not be able to create a cart without registering first, unfortunatly the person for whom i am making the website wants it this way.

...

There are a good amount of people here who can associate quite vividly with this kind of scenario.  If you've not been there already check out http://clientsfromhell.net/  ;D
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.