2011-12-14 192 views
0

這裏是我的功能爲什麼我的函數沒有返回任何東西?

function get_products($business_id){ 
    $sql = "SELECT 
      b.*, b.name as business_name, p.builder_id, 
      bs.*, p.price, p.name as product_name, p.description, p.image, p.per_station, p.skip_screen 
     FROM 
      builder_product bp 
     JOIN builder_step bs ON bp.builder_step_id = bs.builder_step_id 
     JOIN builder b ON bp.builder_id = b.builder_id 
     WHERE b.business_id = '{$business_id}' 
     ORDER by b.step_number, b.price"; 
    $result = mysql_query($sql); 
    $steps = array(); 
    while($row_r = mysql_fetch_assoc($result)){ 
     $steps["all_steps"]['business_id'] = $business_id; 
     $steps["business_name"] = $row_r['business_name']; 
     $steps["builder_id"] = $row_r['builder_id']; 

    if (count($steps) == 0) { 
     $business_id = 59; 
     get_products($business_id); 
    }else{ 
     $steps["business_id"] = $business_id; 
     return $steps; 
    } 
} 

這裏是調用函​​數

$products = get_products($_GET['business_id']); 

爲什麼我沒有得到這個函數返回....我之前做的print_r權返回,我有我的數組噸噸的數據,但如果我做一個print_r調用這個函數下面,我什麼也得不到。我做一個遞歸調用,以確保我有一個返回數組...我很想念

+3

* sigh * http://bobby-tables.com/ – Quentin 2011-12-14 15:05:03

+2

在`else`情況下只有`return`語句。如果你不輸入`else`的情況下,你將不會返回一個值。 – 2011-12-14 15:05:24

+0

我輸入其他...我正在做一個print_r之前返回,我得到的數組 – Trace 2011-12-14 15:06:57

回答

0
if (count($steps) == 0) { 
    $business_id = 59; 
    get_products($business_id); 
}else{ 
    $steps["business_id"] = $business_id; 
    return $steps; 
} 

當你點擊如果代替了其他任何想法,什麼都不會返回。

編輯:改爲return get_products($business_id)

0

你不必在你的回報,如果:

 
if (count($steps) == 0) { 
     $business_id = 59; 
     get_products($business_id); 
     return $whatYouwant; 
    }else{ 
     $steps["business_id"] = $business_id; 
     return $steps; 
    } 

0

這是全功能?看起來像你缺少一個右括號:

function get_products($business_id){ 
    $sql = "SELECT 
      b.*, b.name as business_name, p.builder_id, 
      bs.*, p.price, p.name as product_name, p.description, p.image, p.per_station, p.skip_screen 
     FROM 
      builder_product bp 
     JOIN builder_step bs ON bp.builder_step_id = bs.builder_step_id 
     JOIN builder b ON bp.builder_id = b.builder_id 
     WHERE b.business_id = '{$business_id}' 
     ORDER by b.step_number, b.price"; 
    $result = mysql_query($sql); 
    $steps = array(); 
    while($row_r = mysql_fetch_assoc($result)){ 
     $steps["all_steps"]['business_id'] = $business_id; 
     $steps["business_name"] = $row_r['business_name']; 
     $steps["builder_id"] = $row_r['builder_id']; 

     if (count($steps) == 0) { 
      $business_id = 59; 
      get_products($business_id); 
     }else{ 
      $steps["business_id"] = $business_id; 
      return $steps; 
     } 
    } 
} 
1

我認爲你需要一個return語句添加到代碼的if (count($steps) == 0)分公司:

if (count($steps) == 0) { 
    $business_id = 59; 
    return get_products($business_id); 
}else{ 
    $steps["business_id"] = $business_id; 
    return $steps; 
} 
0

我找到了解決辦法....

if (count($steps) == 0) { 
    $business_id = 59; 
    $steps = get_products($business_id); 
    $steps["business_id"] = $business_id; 
    return $steps; 
}else{ 
    $steps["business_id"] = $business_id; 
    return $steps; 
} 

我需要趕上從數組中返回功能

相關問題