سؤال

I have the following nested loops. The first one gets manufacturers_name and as sometimes there are more than one name in that field I'm using the second foreach loop to split the array and get the ID for each of the manufacturers. The problem is that the loop stops at the first element $pieces.

Loop:

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>";
    }   
}

Result:

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

Expected result:

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
هل كانت مفيدة؟

المحلول 2

I did a var_dump($pieces) and var_dump($n) to troubleshoot the problem and it turned out there is a space in front of the names and that's why I was not getting their IDs:

for $pieces -

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

for $n -

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

Fix:

Put the following function in my document and then trimmed the loop:

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

Trimmed $pieces just before the second foreach loop:

array_walk($pieces, 'trim_value');

نصائح أخرى

foreach ($items as $v) {
products_id='".$v."'");

change to

foreach ($items as $v) {
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top