Leo Loki
Leo Loki

Reputation: 109

Why not output the first element array?

Why not output the first element array?

i use next code

$product_idn='123112$2313213';
$count_products=substr_count($product_idn,'$')+1;

$idn_products=explode('$',$product_idn);
$name_products='';
$s=0;
while($s<=$count_products){
$prod=$idn_products[$s];
$res10=mysql_query("..... WHERE `product_idn`='$prod'");
$i10=mysql_fetch_assoc($res10);
$name_products.=$i10['name'].', ';
$s++;
}
echo $name_products;
//give 2313213,,

Why not output the first element array ?

Upvotes: 0

Views: 105

Answers (2)

gview
gview

Reputation: 15371

There are a lot of unusual techniques being used in the original code. My best guess at what I'd do, without really knowing the purpose of this code is:

$product_idn = '123112$2313213';

$idn_products = explode('$', $product_idn);

$name_products = '';
foreach($product_idn as $value) {
    $res10 = mysql_query("SELECT name FROM ... WHERE `product_idn`='$value'");
    if ($res10) {
        $i10 = mysql_fetch_assoc($res10);
        $name_products .= $i10['name'].', ';
    }
}
$name_products = rtrim(', ', $name_products);
echo $name_products;

Upvotes: 0

Dion
Dion

Reputation: 3345

What about

$product_idn='123112$2313213';

$idn_products=explode('$',$product_idn);
$name_products='';

foreach($idn_products as $val){
$res10=mysql_query("..... WHERE `product_idn`='$val'");
$i10=mysql_fetch_assoc($res10);
$name_products.=$i10['name'].', ';
}
echo $name_products;

Upvotes: 4

Related Questions