简体   繁体   English

在while循环之外使用数据

[英]using data outside while-loop

I am using this code which calls the period_id :我正在使用这个调用period_id :代码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所以在 usercart 函数中会调用例如 3 个不同的period_id

so I need to finish like this :所以我需要这样完成:

$p='2,5,3';

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

I think if我想如果

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

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

when you echo $p ; //output will be 1,5,3当你echo $p ; //output will be 1,5,3 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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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