2015-10-06 30 views
-3

我創建的PHP函數調用clear_url,但我想出變量$ idc和$類型,但我不能這樣做任何人都可以告訴我該怎麼做嗎?

function clear_url($f1,$f2,$table){  
    include('db.php'); 
    $sql=$Con->query("SELECT {$f1},{$f2} FROM {$table}"); 

    $array=array(); 
    while($rowt=mysqli_fetch_object($sql)){ 
     $name1 = trim($rowt->$f1); 
     $name1 = strtolower($name1); 
     $name1 = stripslashes($name1); 
     $name1 = str_replace(" ","-",$name1); 
     $name1 = str_replace(".","-",$name1); 
     $name1 = str_replace(" ","-",$name1); 
     $name1 = str_replace("(","",$name1); 
     $name1 = str_replace(")","",$name1); 
     $name1 = str_replace("/","-",$name1);   
     $name1 = str_replace(",","-",$name1); 
     $name1 = str_replace("'","-",$name1);      
     $name1 = str_replace("&","-",$name1); 
     $name1 = str_replace("---","-",$name1); 
     $array[]=$name1.','.$rowt->$f2; 
    } 
    $leng=count($array); 
    for($i=0;$i<$leng;$i++){   
     $array[$i]; 

     $str=$array[$i]; 
     $sp=explode(",",$str); 
     if($sp[0]==$_GET['name']){ 
      $idc=$sp[1]; 
      $type=$sp[0]; 
     } 

    } 
} 
+2

你可以發佈您的代碼的功能? –

+1

將它放在顯示器上,我們將看看 – DevDonkey

+0

「脫離變量」是什麼意思?你想從函數中返回它們嗎?你想打印它們到輸出嗎?你需要更清楚。 – Anders

回答

0

您只能從函數返回一個變量;因此返回兩個值的最簡單方法是將其放入數組中。

//編輯 - 如果您選擇返回某一點時,您還沒有確定您已得到結果;你需要一個條件來實際檢查。

if (isset($type) && isset($idc)) { 
    return array(
     'idc' => $idc, 
     'type' => $type 
    ); 
} else { 
    return false; 
} 

,然後當你調用

$sample = clear_url($f1,$f2,$table); 
$idc = $sample['idc']; 
$type = $sample['type']; 
+0

我試過但沒有工作,它顯示錯誤未定義的變量類型。 – DOM

+0

請參閱編輯。當然,如果你實際上沒有獲得'$ idc'和'$ type'變量,那完全是另一個問題。 –

+0

@GeoffAtkins你可以將'isset()'合併爲一個。 – Raptor

相關問題