Possibly the most basic PHP question ever asked!

Discussion in 'PHP' started by san321, Jun 26, 2007.

  1. #1
    How so you spell PHP?

    Kidding.
    I cant perform a tax calculation on a variable, it just wont do it. I am using a datafeed script from oscommerce.

    Pasted below.

    The part I am trying to add 10% to is the price part.
    $row['price'] * 1.1 . "\n";
    the * 1.1 makes the script just print 1.1 instead of multiplying the price by 1.1

    I have tried placing it in brackets etc, its just not working - way past my php hack skills....

    Anything over $1000 will just dissapear and show 1.1
    It may have something to do with the thousandth seperator " , " so a price of 1,899 becomes 1.1

    Full script here.

    <?php
    
    require('includes/application_top.php');
    
    
    
    $OutFile = session_save_path()."/temp_file_".rand()."_sfdata.txt";
    $destination_file = "sfdata.txt";
    $source_file = $OutFile;
    $already_sent = array();
    // LANGUAGE ID - DEFAULT VALUE IS 1 FOR ENGLISH
    $language_id = 1;
    
    // Grab the products
    $products_query = tep_db_query("select
    manuf.manufacturers_name as manufacturer,
    prd.products_id AS id,
    '' AS stock,
    '' AS mpn,
    prd.products_artist5 AS mpc,
    prdsc.products_name AS name,
    '' AS description,
    FORMAT(prd.products_price,2) AS price,
    prd.products_image,
    
    if(catdescparent2.categories_name!='',
    concat_ws( ' > ' , catdescparent2.categories_name , concat_ws( ' > ' , catdescparent.categories_name , catdesccurrent.categories_name )),
    concat_ws( ' > ' , catdescparent.categories_name , catdesccurrent.categories_name ) 
    )
    AS category
    
    FROM " . TABLE_CATEGORIES . " ,
    " . TABLE_PRODUCTS . " prd,
    " . TABLE_PRODUCTS_DESCRIPTION . " as prdsc,
    " . TABLE_CATEGORIES_DESCRIPTION . " as catdesccurrent,
    " . TABLE_PRODUCTS_TO_CATEGORIES . " as prdtocat
    left join " . TABLE_CATEGORIES_DESCRIPTION . " as catdescparent on ( catdescparent.categories_id = categories.parent_id AND catdescparent.language_id = '$language_id' )
    left join " . TABLE_CATEGORIES . " as cat2 on ( cat2.categories_id = categories.parent_id )
    left join " . TABLE_CATEGORIES_DESCRIPTION . " as catdescparent2 on ( catdescparent2.categories_id = cat2.parent_id AND catdescparent2.language_id = '$language_id' )
    left join " . TABLE_MANUFACTURERS . " as manuf on ( manuf.manufacturers_id = prd.manufacturers_id )
    WHERE 
    ( prd.products_id=prdsc.products_id AND prdsc.language_id = '$language_id' )
    AND prd.products_id=prdtocat.products_id
    AND prdtocat.categories_id=categories.categories_id
    AND ( catdesccurrent.categories_id = categories.categories_id AND catdesccurrent.language_id = '$language_id' )
    AND prd.products_status != 0");
    
    // Check for any applicable specials for the corresponding products_id
    $specials_query = tep_db_query("select
    specials.products_id as idS,
    format(specials.specials_new_products_price,2) as priceS
    from
    specials,
    products
    where
    specials.products_id=products.products_id
    and specials.status != 0
    and products.products_status != 0");
    while( $row_s = tep_db_fetch_array( $specials_query ) )
    {
    	foreach ($row_s as $i=>$v) {
    		$SPECIALS[$row_s['idS']][$i] = $v;
    	}
    }
    
    
    $_strip_search = array(
    "![\t ]+$|^[\t ]+!m", // Remove leading/trailing space chars
    '%[\r\n]+%m',
    ); // Remove CRs and newlines
    $_strip_replace = array(
    '',
    '',
    );
    
    $output = "Category\tISBN\tGTIN\tManufacturer\tModel\tMerchant Product Code\tProduct Name\tProduct Description\tProduct URL\tProduct Image URL\tStock\tPrice\n";
    
    // PRINT THE PRODUCTS
    while( $row = tep_db_fetch_array( $products_query ) )
    {
    	// If we've sent this one, skip the rest of the while loop
    	if ($already_sent[$row['product_url']] == 1) continue;
    	
    	$row['product_url'] = tep_href_link(FILENAME_PRODUCT_INFO, 'products_id='.$row['id'], 'NONSSL', false);
    	if($row['products_image'])
    	{
    		$row['image_url'] = HTTP_SERVER . DIR_WS_CATALOG . DIR_WS_IMAGES . $row['products_image'];
    	}	
    	
    	// Reset the products price to our special price if there is one for this product
    	if( $SPECIALS[$row['id']]['idS'] ){
    		$row['price'] = $SPECIALS[$row['id']]['priceS'];
    	}
    	
    	// $QTY IS THE QUANTITY VARIABLE	
    	if($row['stock']>0){
    		$row['stock'] = "";
    	}
    	elseif($row['stock']==0){
    		$row['stock'] = "";
    	}
    	else{
    		$row['stock'] = "";
    	}
    	
    	$output .=
    	$row['category'] . "\t\t\t" .
    	$row['manufacturer'] . "\t" .
    	$row['mpn'] . "\t" .
    	$row['mpc'] . "\t" .
    	preg_replace($_strip_search, $_strip_replace, $row['name']) . "\t" .
    	preg_replace($_strip_search, $_strip_replace, $row['description']) . "\t" .
    	$row['product_url'] . "\t" .
    	$row['image_url'] . "\t" .
    	$row['stock'] . "\t" .
    	$row['price'] * 1.1 . "\n";
    	
    	$already_sent[$row['product_url']] = 1;
    }
    
    print $output;
    exit;
    
    ?>
    
    
    PHP:
     
    san321, Jun 26, 2007 IP
  2. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What is the value of $row['price'] ?, Is it 1000$ or 1000?, If 1000$ remove $ sign and try it.
     
    tamilsoft, Jun 26, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    When converting a string to a numeric value, anything other than . e or E are invalid characters. If you've got a , in there, it will only read up to the comma and ignore the rest of the string. You should really strip everything that isn't a digit but if you know exactly what format the value is in, you won't need the extra overhead of regex:

    ( preg_replace('/[^0-9]/', '', $row['price]) * 1.1 )

    ( str_replace(',', '', $row['price]) * 1.1 )

    See: string conversion to numbers
     
    rodney88, Jun 27, 2007 IP
  4. Acecool

    Acecool Peon

    Messages:
    267
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also, instead of storing these with dollar signs, just store as digits like..

    1111118329.22 or something like it, and use number_format() to format it to your liking :)
     
    Acecool, Jun 27, 2007 IP
  5. mojojuju

    mojojuju Peon

    Messages:
    53
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think it's spelled "Pee Aich Pee" :D
     
    mojojuju, Jun 27, 2007 IP