>>413
while(1){
  if (!empty($val))
    $data .= $val;
}

これでおk