2014-04-09 156 views
0

我有下面的嵌套循環。第一個得到manufacturers_name,並且由於有時在該字段中有多個名稱,我使用第二個foreach循環來拆分數組並獲取每個製造商的ID。問題是循環停在第一個元素$piecesPHP foreach循環在爆炸後停止

循環:

foreach ($items as $v) { 
$getname = xtc_db_query("SELECT `manufacturers_name` FROM `manufacturers` WHERE `manufacturers_id`=(SELECT manufacturers_id FROM products WHERE products_id='".$v."')"); 
$getnameresult= xtc_db_fetch_array($getname); 
$pieces = explode(";", $getnameresult[manufacturers_name]); 
    foreach ($pieces as $n) 
    { 
    $getid = xtc_db_query("SELECT `manufacturers_id` FROM `authors` WHERE  `manufacturers_name` LIKE '".$n."' "); 
    $getidresult = xtc_db_fetch_array($getid); 
    echo $getidresult['manufacturers_id']; 
    echo $n."<br>"; 
    } 
} 

結果:

2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
228Weisenburger, Birgit 
228Weisenburger, Birgit 
2Achtzehn, Hans-Jürgen 
33Müller, Ulrike 
Schmitz, Irmgard 
Hampel, Heike 
Riedel, Sara 
Achtzehn, Hans-Jürgen 
Krüger, Andreas 
Vogt, Melanie 

預期結果:

2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
2Achtzehn, Hans-Jürgen 
228Weisenburger, Birgit 
228Weisenburger, Birgit 
2Achtzehn, Hans-Jürgen 
33Müller, Ulrike 
56Schmitz, Irmgard 
77Hampel, Heike 
75Riedel, Sara 
34Achtzehn, Hans-Jürgen 
65Krüger, Andreas 
80Vogt, Melanie 

回答

0

我做了的var_dump($件)的var_dump($ N)來解決這個問題,它原來有在名字前面的空間,這就是爲什麼我沒有得到它們的ID:

爲$件 -

array (size=8) 
    0 => string 'Müller, Ulrike' (length=14) 
    1 => string ' Schmitz, Irmgard' (length=17) 
    2 => string ' Hampel, Heike' (length=14) 
    ... 

爲$ N -

string 'Müller, Ulrike' (length=14) 
string ' Schmitz, Irmgard' (length=17) 
string ' Hampel, Heike' (length=14) 

修復:

將下面的功能在我的文檔,然後修剪循環:

function trim_value(&$value) 
{ 
    $value = trim($value); 
} 

修剪$pieces只是第二foreach循環之前:

array_walk($pieces, 'trim_value'); 
0
foreach ($items as $v) { 
products_id='".$v."'"); 

變化

foreach ($items as $v) {