PHP以字符串

时间:2015-11-03 12:17:37

标签: php mysql sql xml csv

表格如下:

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

此功能正常工作。

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

这是调用函数并从数据库中获取数据的代码,并尝试用逗号分隔我需要的值。

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

问题似乎出现在这段代码中,但我不知道在哪里...... 任何帮助将非常感激! 提前谢谢!

1 个答案:

答案 0 :(得分:1)

尝试这种方式(firts分配给$ size而不连接)

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