如何在symfony循环的每次迭代中将数据从控制器发送到视图(树枝)
我想将一个从控制器的值发送到foreach循环的每次迭代的视图。
上下文:
我有一个以100个元素为例的数组。在控制器中,我想循环阵列,对于每个迭代,将一个值发送到视图。我该如何在Symfony中这样做
是代码示例
$myArray = ["val1", "val2", "val100"];
foreach ($myArray as $key => $val) {
// here I want to send the data (val1 to val100) to the view on each iteration
}
注:
返回this-> json(...)或新的jsonresponse(...) ,在循环中,循环在第一次迭代后停止。如果将其放在循环外,则仅渲染最后一个Val(此示例中的Val100)。
我想这样做?
为什么 在控制器中读取文件后,我将数据逐个插入相关表中通过foreach循环,我想喂食一个进度栏,这是为了让用户知道数据正在插入(如( 1/100,2/100 ... 100/100)。如何从循环中传递每次迭代的数据以达到视图。
数据已成功地插入数据库,,但我仍然找不到一种方法来喂食进度栏,以预先对循环的每次迭代
提前,非常感谢。
I want to send a value from a controller to a view on each iteration of a foreach loop.
Context:
I have an array that has 100 elements as an example. In the controller, I want to loop the array and for each iteration send a value to the view. How can I do that in Symfony
Here is the code example
$myArray = ["val1", "val2", "val100"];
foreach ($myArray as $key => $val) {
// here I want to send the data (val1 to val100) to the view on each iteration
}
Note:
With return this->json(...) or new JsonResponse(...), in the loop, the loop stops after the first iteration. If you put it outside of the loop, only the last val (val100 in this example) is rendered.
Why do I want to do that?
Let's say I am working on a symfony project to insert data into a database from excel file or something similar. After reading the file in the controller, I'm inserting the data one by one into the related table via a foreach loop and I aslo want to feed a progress bar which is in the view to let the user know that data is inserting like (1/100, 2/100 ... 100/100). How can I pass the data on each iteration from the loop to reach the view.
Data is inserted into the db successfully, but I still can't find a way to feed the progress bar in the view on each iteration of the loop
In advance, many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论