2016-04-11 56 views
0

我在使用$.getJSON請求獲取JSON數據時遇到問題。在一個PHP文件中,我有我的功能。該代碼是:無法讓函數調用在PHP中工作

function get_topchart($dbc, $var_ziekenhuis_id, $var_department_id) { 

    if($var_department_id == "Not Entered") { 

     $query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' ORDER BY afdeling DESC"; 
     $result = mysqli_query($dbc,$query); 

     $data = array(); 
     while ($row = $result->fetch_assoc()) { 
      $data[] = $row; 
     } 
     echo json_encode($data); 
     //return(json_encode($data)); 

    } else { 

     $query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' and afdeling='$var_department_id' ORDER BY afdeling DESC"; 
     $result = mysqli_query($dbc,$query); 

     $data = array(); 
     while ($row = $result->fetch_assoc()) { 
      $data[] = $row; 
     } 
     echo json_encode($data); 
     //return(json_encode($data)); 
    } 
} 

我用下面的代碼來調用上面的函數,它是在getcharts.php:

<?php 
session_start(); 

include('../functions/datafunctions.php'); 
include('../config/db.php'); 


if(!isset($_SESSION['username'])) { 
    header('Location: ../login.php'); 
} 

//Determine the function that should be called 
$var_function_id = $_GET['funcid']; 
//Set variables 
$var_ziekenhuis_id = $_GET['hosp']; 
$var_department_id = $_GET['dept']; 

switch ($var_function_id) { 
    case "top_chart": 
     //Call function 
     get_topchart($dbc, $var_ziekenhuis_id, $var_department_id); 
    case "bottom_chart": 
     //New code here 
} 
?> 

$.getJSON我用的是:

$.getJSON('functions/getcharts.php', { 
    "hosp":hospital, 
    "dept":department, 
    "funcid":functionid}, 

當我跳過整個getcharts.php並直接使用開關功能時,事情確實起作用。這代碼:

<?php 

session_start(); 
include('../config/db.php'); 

//Controleer eerst of gebruiker succesvol is ingelogd: 

if(!isset($_SESSION['username'])) { 
    header('Location: ../login.php'); 
} 

//Determine the function that should be called 
$var_function_id = $_GET['funcid']; 
//Set variables 
$var_hospital_id = $_GET['hosp']; 
$var_department_id = $_GET['dept']; 

switch ($var_function_id) { 
    case "top_chart": 

     //function get_topchart($dbc, $var_ziekenhuis_id, $var_department_id) { 

      if($var_department_id == "Not Entered") { 

       $query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' ORDER BY afdeling DESC"; 
       $result = mysqli_query($dbc,$query); 

       $data = array(); 
       while ($row = $result->fetch_assoc()) { 
        $data[] = $row; 
       } 
       echo json_encode($data); 
       //return(json_encode($data)); 

      } else { 

       $query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' and afdeling='$var_department_id' ORDER BY afdeling DESC"; 
       $result = mysqli_query($dbc,$query); 

       $data = array(); 
       while ($row = $result->fetch_assoc()) { 
        $data[] = $row; 
       } 
       echo json_encode($data); 
       //return(json_encode($data)); 
      } 
     //} 
} 

?> 

$getJSON代碼在這種情況下是:

$.getJSON('functions/datafunctions.php', { 
    "hosp":hospital, 
    "dept":department, 
    "funcid":functionid}, 

我在做什麼錯?

回答

0

更換

echo json_encode($data); 

return json_encode($data); 

更換

switch ($var_function_id) { 
    case "top_chart": 
     //Call function 
     get_topchart($dbc, $var_ziekenhuis_id, $var_department_id); 
    case "bottom_chart": 
     //New code here 
} 

有:

switch ($var_function_id) { 
    case "top_chart": 
     //Call function 
     echo get_topchart($dbc, $var_ziekenhuis_id, $var_department_id); 
    case "bottom_chart": 
     //New code here 
} 
+0

非常感謝!我還發現在SQL語句中我使用了錯誤的變量。現在事情正在起作用。 – CostCare

+0

@CostCare閱讀http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Saty

+0

@CostCare,如果您覺得它解決了您的問題,請接受我的答案,謝謝! – LMS94

0

取消對return語句的功能: 功能get_topchart($ DBC,$ var_ziekenhuis_id,$ var_department_id){

if($var_department_id == "Not Entered") { 

    $query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' ORDER BY afdeling DESC"; 
    $result = mysqli_query($dbc,$query); 

    $data = array(); 
    while ($row = $result->fetch_assoc()) { 
     $data[] = $row; 
    } 
    return(json_encode($data)); 

} else { 

    $query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' and afdeling='$var_department_id' ORDER BY afdeling DESC"; 
    $result = mysqli_query($dbc,$query); 

    $data = array(); 
    while ($row = $result->fetch_assoc()) { 
     $data[] = $row; 
    } 
    return(json_encode($data)); 
} 

}

,影響您的功能上,當一個變量return語句你叫它。

變化:

get_topchart($dbc, $var_ziekenhuis_id, $var_department_id);

$topchart = get_topchart($dbc, $var_ziekenhuis_id, $var_department_id);

+0

感謝。我現在有工作。 – CostCare

+0

@CostCare閱讀http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Saty