2013-07-30 35 views
0

我遇到以下問題。我正在嘗試編寫從csv文件導入數據的函數。在價格欄中的這個文件中,如果有像'<>'這樣的符號,這意味着價格是以美元計算的,需要轉換。我明白這個變量是以數字形式表示的。它如何轉換爲字符串?或者爲什麼這個陳述根本不起作用?一如既往的源代碼。如果語句在關聯數組中不起作用

$str='<>'; 
    if($variant['price'] ==$variant['price'].$str) 
    { 
    $sql = mysql_query("SELECT rate_to FROM s_currencies WHERE id=1 LIMIT 0, 30 "); 
    $course= mysql_fetch_row($sql); 
//$rate=$course[0]; 
    $variant_price = $item['price']*$course[0]; 
    $variant['price']=$variant_price; 
     } 

請幫忙!

+16

如何將一個字符串與兩個字符添加到最後? – Barmar

+2

'$ variant ['price']'實際上是什麼樣的? – Barmar

+0

echo $ variant ['price']'。' =='。$ variant ['price']。$ str;這是什麼打印? – cartina

回答

1

您需要檢查該字符串是否存在,而不是使用當前的IF語句。 strpos會給你想要你需要

if(strpos($variant['price'],$str) !== false) // <> is present 
{ 
    // run your sql code 
} 

我也建議從mysql_越來越遠*充當他們棄用。查看帶有綁定參數的PDO或mysqli查詢。

+0

非常感謝你的幫助! –

+0

太棒了!不要忘記接受答案 – aynber

1
$str='<>'; 
    if(stristr($variant['price'],$str){ 
     $sql ="SELECT rate_to FROM s_currencies WHERE id=1 LIMIT 0, 30 "; 
     $qry = mysql_query($sql); 
     if ($qry && mysql_num_rows($qry)>0){ 
      $variant['price'] = (str_replace($str,'',$variant['price'])*mysql_result($qry,0,0)); 
     } else { 
      echo 'error while converting:' . mysql_error(); 
     } 
    } 
+0

如果'<>'在$ variant ['price']'的開頭,'stristr()'將返回'0',即'false'。 – Barmar

+0

不,它不? <? \t \t $ variant ['price'] ='<> test'; \t \t $ str ='<>'; (stristr($ variant ['price'],$ str)){ \t \t如果(stristr($ variant ['price'],$ str)){ \t \t \t echo'test'; \t \t} \t \t?> 回聲 '測試' – Garytje

+0

對不起,我在想'stripos函數()'。 – Barmar

3

如果條件存在,您發佈的代碼將不會進入。檢查代碼。

For eg. if $variant['price'] = '1'; 

if ('1' == '1<>') 
{ 
} 

上述情況不會進入if語句。