简体   繁体   中英

PHP exporting values from database, comma separated, in a string

The tables look like this:

virtuemart_customfield_id___|___virtuemart_product_id___|____custom_value___
                        1   |                   4346    |           45
                        2   |                   4346    |           46
                        3   |                   4347    |           45

This function is working properly as is.

function getSizes($product_id){
    global $mainframe;
    $db = &JFactory::getDBO();
    $sql = 'SELECT * FROM `#__virtuemart_product_customfields` WHERE `virtuemart_product_id` ='.$product_id.'';
    $db->setQuery($sql);
    $result=$db->loadObjectList();
    return $result;
}

And this is the code that will call the fuction and get the data from the database, and try to separate the values I need with commas.

$records = $this->getSizes($product_id);
$product_size = '';
foreach ($records as $value){
    $size = $value->custom_value;
    if ($product_size == '') {
        $product_size .= $size;
    } else {
        $product_size .= ", " .$size;
}

The problem seems to be inside this piece of code somewhere, but I have no idea where... Any help would be much appreciated! Thanks in advance!

try this way (firts assign to $size without concatenation)

$records = $this->getSizes($product_id);
$product_size = '';
foreach ($records as $value){
   $size = $value->custom_value;
   if ($product_size == '') {
       $product_size = $size;
   } else {
       $product_size .= ", " .$size;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM