-2

I am using this code which calls the period_id :

$cart = $this->GetUserCart();
$dataset = array();
while ($data = $cart->fetch_array()) {  
    $dataset[] = $data['period_id'];
}

so I need to implode the result so they can be separated by comma

$p = implode(",",$dataset);

So in the usercart function will call for example, 3 distinct period_id

so I need to finish like this :

$p='2,5,3';
Abhishek
  • 2,925
  • 4
  • 34
  • 59
Fin del Mundo
  • 156
  • 11

2 Answers2

0

Your code is correct. Maybe you just have to echo it.

$dataset = array();
while ($data = $cart->fetch_array()) {  
    $dataset[] = $data['period_id'];
}

$p = implode(',', $dataset);
echo $p; //you just need to echo it
Abhishek
  • 2,925
  • 4
  • 34
  • 59
aldrin27
  • 3,407
  • 3
  • 29
  • 43
0

I think if

        $p = implode(",",$dataset);

if $dataset = array("1", "5", "3");

when you echo $p ; //output will be 1,5,3

put if you add this :

       $p = "'".implode(",",$dataset)."'";
       echo $p ; //output will be '1,5,3' 

and that is what you need if i understand you so

       $p = '1,5,3';
Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18
  • lol. Why would he concatenate the imploded data? If the actual data is correct? – aldrin27 Sep 07 '15 at 06:32
  • ya , but sometimes you need output looks like groups and may use more than one implode results for example ,'1,2,3','1,4,7' say we have two array then we need out put to be like that , echo "'".implode(",",$dataset)."'" ; echo ","; echo "'".implode(",",$dataset2)."'" ; and so on – Mohammed Elhag Sep 07 '15 at 06:51