在json_decode()之后,我运行一个foreach()循环以显示所有json结果以及每个结果的提交按钮。我编写了一个mysql INSERT块来发布json结果。我试图弄清楚如何为每个json结果分配一个唯一的ID,以便在单击Submit按钮之后,该JSON对象将正确地发布到我的数据库中。我的代码当前正在发生什么,只是数组中的最后一个值被发布,而不是我尝试提交的值。
我尝试遍历foreach()循环,并将迭代器用作分配的唯一ID。没用我尝试在提交按钮旁边创建复选框,但也没有解决。我不确定这是我的代码放置还是为什么我只能在数组中发布最后一个值。
<?php
$curl = curl_init();
$provider_first_name = 'Henry';
$provider_last_name = 'Lee';
$provider_state = 'CA';
$provider_postal_code = '';
curl_setopt_array($curl, array(
CURLOPT_URL => "https://npiregistry.cms.hhs.gov/api?first_name=". $provider_first_name . "&last_name=" . $provider_last_name . "&state=" . $provider_state . "&postal_code=" . $provider_postal_code . "",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$json = json_decode($response, TRUE);
/ **将从foreach()循环中显示10条亨利·李结果** /
$i = 0;
foreach($json['results'] as $doc){
/** JSON Responses Listed Below **/
$p_name = $doc['basic']['first_name'] . " " . $doc['basic']['last_name'] . ", " . $doc['basic']['credential'];
$p_npi = $doc['number'];
$p_address_one = $doc['addresses'][0]['address_1'];
$p_address_two = $doc['addresses'][0]['city'] . ", " . $doc['addresses'][0]['state'] . " " . $doc['addresses'][0]['postal_code'];
$p_skills = $doc['taxonomies'][0]['desc'];
$p_phone = $doc['addresses'][1]['telephone_number'];
if(empty($doc['addresses'][1]['telephone_number'])){
$p_phone = 'Not Available';
}
//** Echo Each Array Value In Bordered Card **/
echo "
<div class='col-md-4 col-sm-12 wow fadeIn' style='padding-top: 9px;'>
<div class='card border border-secondary mb-3' style='max-width: 18rem;'>
<div class='card-header text-center'>
<i class='fa fa-user-md' aria-hidden='true'></i> $p_name
</div>
<div class='card-body text-primary text-center'>
<p class='card-text text-center'>
NPI Number: $p_npi
</p>
<p class='card-text text-center'>
$i
</p>
<p class='card-text text-center'>
<i class='fa fa-map-marker' aria-hidden='true'></i> $p_address_one
</p>
<p class='card-text text-center'>
$p_address_two
</p>
<p class='card-text text-center'>
<i class='fa fa-phone cyan-text' aria-hidden='true'></i> $p_phone
</p>
<p class='card-text text-center'>
<i class='fa fa-user-md cyan-text' aria-hidden='true'></i> $p_skills
</p>
</div>
<div class='card-footer' style='height: 73px;''>
<form action='' method='post' id='".$json['results'][$i]."'>
<div class='form-group'>
<div class='flex-center'>
<div class='form-row align-items-center'>
<button class='btn btn-pink btn-sm' name='add_doc' type='submit'><i class='fa fa-trash px-1' aria-hidden='true'></i> Add Provider</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
";
$i++;
}
?>
<?php
if(isset($_POST['add_doc'])){
$add_doc = "INSERT INTO user_posted_providers (user_id, provider_name, provider_npi, provider_address_one, provider_address_two, provider_phone, provider_skills, created_at) VALUES ('$user_id', '$p_name', '$p_npi', '$p_address_one', '$p_address_two', '$p_phone', '$p_skills', NOW())";
$run_doc = mysqli_query($connection, $add_doc);
if($run_doc){
echo "<script>alert('Provider Added')</script>";
echo "<script>window.open('user_provider_search-r.php','_self')</script>";
}
}
?>
我正在尝试通过foreach()循环中的“ add_doc”按钮插入正确的数组值,但是我只能在数组中插入最后一个值。
答案 0 :(得分:0)
因此,使用一些简化的代码可能更易于解释。我想向您展示如何向用户展示表单以进行选择,然后在提交表单时使用这些选定的值。
像您这样的声音在很大程度上理解了这一概念。猜猜我不清楚你被困在哪里。让我们使用复选框和一种形式来查看此代码。了解我分享的内容,尝试并实现,让我知道您在哪里/如果拖延。
new Scaffold(
body: new GestureDetector(
onTap: () {
// call this method here to hide keyboard
FocusScope.of(context).requestFocus(new FocusNode());
},
child: new Container(
/*Remaining code goes here*/
)
)
使用提供的实际ID优于在循环中使用一些计数,因为并不能真正保证此服务返回的结果将与处理表单提交时呈现的表单相同。 HTTP是一种无状态协议。