2016-06-16 102 views
-3

我無法從函數中獲取值。 雖然我要打印的值,它顯示舊值「8」。看來function validateInput()不起作用。php函數無法返回值

$current_val = "8"; 
function validateInput() { 
     $db = JFactory::getDbo(); 
    //$search = $this->item->producer; 
    $query = " SELECT * FROM aua8l_djc2_after_reg_page "; 
    $db->setQuery($query); 
    $result = $db->query(); 
     if ($result->num_rows > 0) { 
      while($row = $result->fetch_assoc()) { 
       $curr = $row['type']; 
       global $current_val; 
        switch ($curr) { 
        case '1': 
         $current_val = "youtube"; 
         return $current_val; 
         //echo $current_val; 
         //return $GLOBALS['current_val']; 
         break; 
        case '2': 
         $current_val = "rdparty"; 
         //return $GLOBALS['current_val']; 
         break; 
        case '3': 
         $current_val = "thankyou"; 
         //return $GLOBALS['current_val']; 
         break; 
        } 
      } 
     } 
} 
echo $current_val; 
+1

你想在函數的末尾執行'return $ current_val;'然後執行'$ current_val = validateInput();' – Albzi

+0

在任何時候都不會執行函數*,所以是的,它不會執行任何東西。 – deceze

+0

對不起,先生,我不能讓你明白...... – user2980415

回答

2

做的另一種方法是這樣的:

$current_val = "8"; 

function validateInput() { 
    $db = JFactory::getDbo(); 
    $query = " SELECT * FROM aua8l_djc2_after_reg_page "; 
    $db->setQuery($query); 
    $result = $db->query(); 
    if ($result->num_rows > 0) { 
     while($row = $result->fetch_assoc()) { 
      $curr = $row['type']; 
      global $current_val; 
      switch ($curr) { 
       case '1': 
        $current_val = "youtube"; 
       break; 
       case '2': 
        $current_val = "rdparty"; 
       break; 
       case '3': 
        $current_val = "thankyou"; 
       break; 
      } 
     } 
     return $current_val; 
    } 
} 

現在你可以做任何

echo validateInput(); 

$current_val = validateInput();  

的d $current_val將是你想要的。

+0

謝謝,先生......工作正常......你讓我的一天... – user2980415

1
$current_val = "8"; 
$current_val = validateInput($current_val); 

function validateInput($current_val) { 
    $db = JFactory::getDbo(); 
//$search = $this->item->producer; 
$query = " SELECT * FROM aua8l_djc2_after_reg_page "; 
$db->setQuery($query); 
$result = $db->query(); 
    if ($result->num_rows > 0) { 
     while($row = $result->fetch_assoc()) { 
      $curr = $row['type']; 
      global $current_val; 
       switch ($curr) { 
       case '1': 
        $current_val = "youtube"; 
        return $current_val; 
        //echo $current_val; 
        //return $GLOBALS['current_val']; 
        break; 
       case '2': 
        $current_val = "rdparty"; 
        //return $GLOBALS['current_val']; 
        break; 
       case '3': 
        $current_val = "thankyou"; 
        //return $GLOBALS['current_val']; 
        break; 
       } 
     } 
    } 
} 
echo $current_val; 
+0

其顯示...致命錯誤:調用未定義的函數validateImput()。 – user2980415

+0

@ user2980415錯字。輸入/輸入 – Andreas

+0

對不起先生...不能讓你 – user2980415