Announcement

Collapse
No announcement yet.

Math with variables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Math with variables

    Hi,

    I have two variables,
    RRP (in pounds) and YouSave (as a percentage of the RRP)

    At the moment, i just use them in plain text on the site, so that underneath my price, it shows the values of these, but formatted. The trouble is, i have to work out the 'YouSave' bit every time. I would really like to automate it.

    TO be clear, the math would be, ((RRP/100)*(RRP-Price))='YouSave'

    Is it possible to get actinic to calculate this value?

    Many Thanks

    Matthew
    http://www.itwiz.co.uk

    http://www.AntiV.net - The Discount Antivirus Store

    #2
    Something like (NB untested)
    Code:
    <actinic:block php="true">
    $rrp = <actinic:variable name="RRP" selectable="false" />;
    $rawprice = <actinic:variable name="ProductPriceRaw" selectable="false" />;
    $rawprice = $rawprice * 1.175;  // add in vat if showing VAT inc prices
    // only display is we have an RRP and there's a saving to show
    if ( ($rrp != 0) && ($rrp > $rawprice) )
      {
      $savepercent = round((($rrp - $rawprice) / $rrp) * 100);
      echo "RRP £$rrp - you save $savepercent%";
      }
    </actinic:block>
    You won't need the YouSave variable any more. It's calulated for you.
    Norman - www.drillpine.biz
    Edinburgh, U K / Bitez, Turkey

    Comment


      #3
      I tried something along these lines a few days ago, but i couldn't get the productPriceRaw to work properly, as soon as i tried anything mathetical on it, it seemed to fall apart at the seams, much like my php skills actually.

      Comment


        #4
        Lee,

        If you didn't have selectable="false" as above you may have problems. As far as I know, if there's no price for product then "0" is returned. You could always quote it in case it's empty. E.g.

        $rawprice = '<actinic:variable name="ProductPriceRaw" selectable="false" />';
        if ( $rawprice == '' ) $rawprice = 0;

        I use ProductPriceRaw in a few add-ons and don't have any problems.
        Norman - www.drillpine.biz
        Edinburgh, U K / Bitez, Turkey

        Comment


          #5
          Thanks for the heads up Norman, i will jump in and have another go, things are very green for me at the mo, but i will soldier on as it has me intrigued. I have also released my first ever bit of php on a website - i was so proud lol. Here it is in all it's glory, it started off as umpteen variables until i found this single function to do it all for me, please don't rip it apart Norman, me and php are at an early stage at the moment :

          <actinic:block php="true">
          echo substr("<actinic:variable name="ProductReference" encoding="perl" selectable="false" />","1");
          </actinic:block>

          Come on Norman, admit it, it's a thing of beauty huh?

          What encoding="perl" means and what the selectable="false" is on about, i have no idea yet, i am just proud to have created such a thing of beauty, one that will rock the world of many php programmers no doubt .

          Comment


            #6
            The selectable part stops the borders appearing around the code in design tab preview.


            Bikster
            SellerDeck Designs and Responsive Themes

            Comment


              #7
              In my fledgling state and you never missing an opportunity to play me up, i am left wondering whether what you say is true or a wind up. It's even funnier that i just tried to remove that part from my code to test it and it has frozen and crashed actinic. Oh my.

              Comment


                #8
                Me! Wind you up? As if


                Bikster
                SellerDeck Designs and Responsive Themes

                Comment


                  #9
                  Originally posted by leehack
                  i just tried to remove that part from my code to test it and it has frozen and crashed actinic. Oh my.
                  See. Serves you right for messing with Uncle Normans code. Tsk.


                  Bikster
                  SellerDeck Designs and Responsive Themes

                  Comment


                    #10
                    Well it crashes any time i select the design tab now!

                    Comment


                      #11
                      Originally posted by jont
                      See. Serves you right for messing with Uncle Normans code. Tsk.
                      It was my thing of beauty i tried it on, that is probably what went wrong. I feel Norman about to rip into my code, i'm braced for it though.

                      Comment


                        #12
                        Hi, Lee

                        Some minor corrections to the above are required.

                        I'd use

                        echo substr('<actinic:variable name="ProductReference" encoding="perl" selectable="false" />',1);

                        If you're converting Actinic variables into PHP strings then you should do it via single quotes. And you don't need quotes around numbers.

                        The encoding="perl" tell Actinic that you want to use this variable in a quoted string. I'll show you why. Lets's imagine we don't use encoding="perl". If your variable contains ABCD we're going to generate

                        echo substr('ABCD',1);

                        Which is just what we wanted.

                        Now if your variable contained Plenty O'Tool we'd have

                        echo substr('Plenty O'Tool',1);

                        And, oh-oh, that's a PHP syntax error as PHP thinks that the middle quote ends the string and tries to interpret Tool' as code.

                        Now with encoding="perl" what Actinic does with Plenty O'Tool is

                        echo substr('Plenty O\'Tool',1);]

                        And PHP now knows that the middle quote is part of the string.

                        The selectable="false" tells Actinic not to embed any additional code around the variable. It does this mainly in the Design view (so the objects can be clicked upon) and this stuff will break our PHP. When the page is Uploaded or Previewed normally, all that guff disappears.
                        Norman - www.drillpine.biz
                        Edinburgh, U K / Bitez, Turkey

                        Comment


                          #13
                          Wow! What a lot of banter was going on while I was crafting my above explanation.
                          Norman - www.drillpine.biz
                          Edinburgh, U K / Bitez, Turkey

                          Comment


                            #14
                            Originally posted by NormanRouxel
                            The selectable="false" tells Actinic not to embed any additional code around the variable. It does this mainly in the Design view (so the objects can be clicked upon) and this stuff will break our PHP. When the page is Uploaded or Previewed normally, all that guff disappears.
                            hahaha.... see


                            Bikster
                            SellerDeck Designs and Responsive Themes

                            Comment


                              #15
                              WOW Norman, thank you very much, that's really helped a lot. Luckily my attempt to remove the selectable=false from the code to see if Jont was winding me up has been rectified by a snapshot import. It crashed actinic each time i went to the design tab with it removed.

                              As a noob, i looked at encoding="perl" and was thinking huh, what's perl got to do with it, i'm using php. I am chuckling to myself writing this, so i expect you are reading it also.

                              I bought a php anthology and to my dismay there was not a single mention of a maths function in it, i waited 4 weeks for the bloody thing to arrive also. I am very good with excel functons for example, so understand the logic on these things, i just lack the experience in php, but i love the look of it so far.

                              One thing that blows my mind is i keep forgetting to close the line with the semi-colon. It has also been very funny tonight as two things i have attempted to do in php this week, the 'you save' percentage and the 'product description split' have both been done by you tonight, if that isn't sods law then i don't know what is. However, i needed to crack the product description split whilst also using your 'tabber'. That resulted in many snapshot corrections, i might be in touch for a quote soon .

                              Comment

                              Working...
                              X