【发布时间】:2017-02-21 06:06:06
【问题描述】:
我有 cURL 函数,它根据我提供的 ID 调用 api 并返回数组中的数据。到目前为止,一切正常。我想要但想不通的是:
我有 foreach,它在表格中显示所有订单所在的位置
@foreach($orders as $order)
$order->address
$order->time_created
$order->price
... etc
@endforeach
是否可以将此 cURL 放在 foreach 中并根据 $order->order_id 添加到每一行以显示来自数组的更多数据?
像这样的
@foreach($orders as $order)
$order->address
$order->time_created
$order->price
... etc
$url = get_curl_content_tx("http://example.com/".$order->order_id);
$data = json_decode($url,true);
@foreach ( $data as $moreData )
@if ( $moreData['data'] == $order->time_created )
// show something
@else
// show other thing
@endif
@endforeach
@endforeach
我的问题是我不知道如何循环 url,所以我可以得到所有 orders_id 即
$url = get_curl_content_tx("http://example.com/1");
$url = get_curl_content_tx("http://example.com/2");
$url = get_curl_content_tx("http://example.com/3");
$url = get_curl_content_tx("http://example.com/4");
$url = get_curl_content_tx("http://example.com/5");
$url = get_curl_content_tx("http://example.com/6");
然后我可以执行我的if/else 块。我使用的框架是 Laravel 4.2。
希望我想要完成的工作足够清楚。如果不是我会尝试清除的东西..
编辑:示例应该是什么:
<table>
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center">Date Created</th>
<th class="text-center">Name</th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<th>$url = get_curl_content_tx("http://example.com/1")</th>
<th>Product 1</th>
</tr>
<tr>
<th>2</th>
<th>$url = get_curl_content_tx("http://example.com/2")</th>
<th>Product 2</th>
</tr>
</tbody>
</table>
【问题讨论】:
-
是的,这是可能的。不过我还是不确定你的问题是什么?
-
我的问题是我循环槽订单,页面上有 10 个订单。然后我得到了 10 次具有相同 order_id 的 curl url .. 即第一个。
-
据我了解,您希望在 foreach 中执行 CURL 请求,而 CURL 取决于 foreach 的值。但是你说 CURL 是用相同的 order_id 执行的?如果您显示的代码是正确的(即:
@foreach($orders as $order)和每个$order->order_id不同,那么应该可以正常工作。您能否提供更多详细信息? -
是的,它正在工作,但例如,如果 foreach 从数据库中获取 5 个订单并将它们显示在页面上的表格中,我应该有 5 倍这个 CURL,每个产品 ID 在末尾
$url = get_curl_content_tx("http://example.com/1...5");。相反,我有 5 次具有相同 ID 的 curl url,即 ID=1 的循环顺序 5 次。似乎我的循环中的某些内容不正确。 -
我已经用预期的方式更新了我的问题。但是,我没有获得正确的 ID,而是在每个产品上获得了相同的 ID。
标签: php arrays json curl laravel-4