Announcement

Collapse
No announcement yet.

php help required

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

    php help required

    we use a php code to calculate the cost per item on our site

    <actinic:block php="true" >
    $units = '<actinic:variable encoding="perl" name="CUST_UNITS_PER_OUTER" selectable="false" />'; // load as string in case variable empty
    if ( $units != '' )
    {
    $cost = '<actinic:variable encoding="perl" name="ProductPriceRaw" selectable="false" />'; // load as string in case variable empty
    $cost = preg_replace('/£|,/', '', $cost); // remove any £ and ,
    $costinc = $cost * 1.15 ;
    $unitcost = ($cost / $units) * 100; // do the arithmetic
    printf(' %.1fp', $unitcost);
    }
    </actinic:block>

    the problem is we need to make the script a bit more complex

    we need 2 results to be displayed

    if final price is 99p or less we need the script to work as is showing for example 69.2p

    (We currently put the p in after the calculation has been done)

    if the price is 100p or more we need the price to be displayed in £ i.e £1.59

    My php skills is not that good and I would appreciate a point in the right direction.
    David Mawson
    Phoenix Trading

    http://www.sweetswholesale.co.uk - Wholesale confectionery suppliers

    #2
    Code:
    $fstring = $unitcost < 100 ? ' %.1fp' : ' £%01.2f';
    printf($fstring, $unitcost);
    Norman - www.drillpine.biz
    Edinburgh, U K / Bitez, Turkey

    Comment


      #3
      as usual Norman you are the PHP master

      will test now
      David Mawson
      Phoenix Trading

      http://www.sweetswholesale.co.uk - Wholesale confectionery suppliers

      Comment


        #4
        Just redid it to make it much more concise.
        Norman - www.drillpine.biz
        Edinburgh, U K / Bitez, Turkey

        Comment


          #5
          Code:
          <actinic:block php="true" >
          $units = '<actinic:variable encoding="perl" name="CUST_UNITS_PER_OUTER" selectable="false" />';	// load as string in case variable empty
          if ( $units != '' ) 
            {
            $cost = '<actinic:variable encoding="perl" name="ProductPriceRaw" selectable="false" />';	// load as string in case variable empty
            $cost = preg_replace('/£|,/', '', $cost);					// remove any £ and ,
          $costinc = $cost * 1.15  ;
          $unitcost = ($cost / $units) * 100;							// do the arithmetic
          
           }
           if ( $unitcost < 100 )
            {
            printf(' %.1fp', $unitcost);
            }
          else
            {
          $unitcostover100 = ($unitcost / 100);	  
          printf(' £%01.2f', $unitcostover100);
            }
          </actinic:block>

          Norman works like a treat

          Just had to make a little change as the price over 100p needed to be divided by 100 to display correctly in £
          David Mawson
          Phoenix Trading

          http://www.sweetswholesale.co.uk - Wholesale confectionery suppliers

          Comment


            #6
            I see problems in your code.


            1) $costinc is being calculated but you don't use it.

            2) Something will print even if CUST_UNITS_PER_OUTER is empty (possibly on subsequent products).

            3) Over complicated arithmetic. No need to multiply and divide.

            4) ProductPriceRaw doesn't contain any formatting so no need to remove £ sign.

            5) Potential fatal error if Units is zero.

            Try:
            Code:
            <actinic:block php="true" >
            $units = '<actinic:variable encoding="perl" name="CUST_UNITS_PER_OUTER" selectable="false" />';	// load as string in case variable empty
            if ( ($units != '' ) && ($units > 0) ) 
            	{
            	$cost = '<actinic:variable encoding="perl" name="ProductPriceRaw" selectable="false" />';	// load as string in case variable empty
            	$costinc = $cost * 1.15 ;
            	$unitcost = $costinc / $units;							// do the arithmetic
             	if ( $unitcost < 1 )
              		{
             		printf(' %.1fp', $unitcost * 100);
              		}
            	else
              		{
            		printf(' £%01.2f', $unitcost);
              		}
             	}
            </actinic:block>
            Norman - www.drillpine.biz
            Edinburgh, U K / Bitez, Turkey

            Comment


              #7
              I agree with you norman will try your new code

              we do use the $cost inc

              $unitcost = $costinc / $units;

              thanks again
              David Mawson
              Phoenix Trading

              http://www.sweetswholesale.co.uk - Wholesale confectionery suppliers

              Comment


                #8
                David,

                I may have been editing the above post. Best look again.
                Norman - www.drillpine.biz
                Edinburgh, U K / Bitez, Turkey

                Comment


                  #9
                  oops yes you are right, we sell to business customers so all prices are ex vat hence why we did not use the $costinc bit. We only used that part of the code on the retail site.

                  my mistake, should have read my original post before replying
                  David Mawson
                  Phoenix Trading

                  http://www.sweetswholesale.co.uk - Wholesale confectionery suppliers

                  Comment


                    #10
                    Norman

                    we have an other variable called "cust_sell_at" which is the sell price in pence

                    if you wanted to use php to format it into £ if more than 99p how would we do it. I just tried and I keep getting parse errors
                    David Mawson
                    Phoenix Trading

                    http://www.sweetswholesale.co.uk - Wholesale confectionery suppliers

                    Comment


                      #11
                      Just adapt the code above.
                      Norman - www.drillpine.biz
                      Edinburgh, U K / Bitez, Turkey

                      Comment


                        #12
                        I must be missing something all I keep getting is parse errors

                        Code:
                        <actinic:block php="true" >
                        $units = '<actinic:variable name="CUST_SELL_AT" />';	// load as string in case variable empty
                         	if ( $units < 1 )
                          		{
                         		printf(' %.1fp', $units * 100);
                          		}
                        	else
                          		{
                        		printf(' £%01.2f', $units);
                          		}
                         	}
                        </actinic:block>
                        David Mawson
                        Phoenix Trading

                        http://www.sweetswholesale.co.uk - Wholesale confectionery suppliers

                        Comment


                          #13
                          Unmatched braces. The line with the last } should be deleted.
                          Norman - www.drillpine.biz
                          Edinburgh, U K / Bitez, Turkey

                          Comment


                            #14
                            Thanks for that Norman

                            Just tried

                            Code:
                            <actinic:block php="true" >
                            $units = '<actinic:variable name="CUST_SELL_AT" />';	// load as string in case variable empty
                             	if ( $units < 1 )
                              		{
                             		printf(' %.1fp', $units * 100);
                              		}
                            	else
                              		{
                            		printf(' £%01.2f', $units);
                              		}
                             	</actinic:block>
                            But I am still getting parse errors
                            David Mawson
                            Phoenix Trading

                            http://www.sweetswholesale.co.uk - Wholesale confectionery suppliers

                            Comment


                              #15
                              Originally posted by davie_ne1 View Post
                              Thanks for that Norman

                              Just tried

                              Code:
                              <actinic:block php="true" >
                              $units = '<actinic:variable name="CUST_SELL_AT" />';	// load as string in case variable empty
                               	if ( $units < 1 )
                                		{
                               		printf(' %.1fp', $units * 100);
                                		}
                              	else
                                		{
                              		printf(' £%01.2f', $units);
                                		}
                               	</actinic:block>
                              But I am still getting parse errors
                              Does it help if you change

                              Code:
                              <actinic:block php="true" >
                              to
                              Code:
                              <actinic:block php="true" selectable="false">
                              ?
                              Tracey

                              Comment

                              Working...
                              X