如何使用单个查询创建多个搜索字段?[PHP/HTML]

3

我正在使用API curl方法获取JSON数据,然后将其插入到格式化的HTML表格中供我的Web应用程序使用。

我的需求略有变化,我现在需要在单个搜索中搜索多个票号。这是我目前的情况..

 <form action="http://-withheld-/shop/ticket.php" method="GET">
    <input type="text" name="id" placeholder="Search by TicketID" required="required"><br>

    <button type="submit">Search</button><br>

我尝试增加更多字段,但没有效果。

例如:

<input type="text" name="id1" placeholder="Search by TicketID" required="required"><br>
<input type="text" name="id2" placeholder="Search by TicketID" required="required"><br>
<input type="text" name="id3" placeholder="Search by TicketID" required="required"><br>

还有,这是我的代码的PHP部分,目前可以在搜索时获取单个票据ID。

if(isset($_GET['id'])) {
        $id = $_GET['id'];
    }

我尝试使用上述HTML代码和以下代码添加更多字段:

if(isset($_GET['id'])) {
$id = $_GET['id1'];
$id = $_GET['id2'];
$id = $_GET['id3'];

        }

等等。如果有人能帮我解决这个问题,我将非常感激。我知道这是一个非常基本的问题,但是我无法找到任何关于使用$_GET方法查询多个搜索的具体信息。我找到的所有内容都与MYSQL查询有关。

提前感谢您的时间和帮助!


如果你将字段更改为id1、id2、id3,并且仅检查是否设置了“id”,那么如果你不再有一个id字段,你将遇到问题。 - Shane
1个回答

1

表单:

<form action="http://-withheld-/shop/ticket.php" method="GET">
 <input type="text" name="id1" placeholder="Search by TicketID" required="required"><br>
 <input type="text" name="id2" placeholder="Search by TicketID" required="required"><br>
 <input type="text" name="id3" placeholder="Search by TicketID" required="required"><br>

<button type="submit">Search</button><br>

收集表单数据并查询JSON数据:
if(isset($_GET['id1'])) {
$id1 = $_GET['id1'];
$url1 = 'http://-withheld-/api/v1/tickets/'.$id1.'?api_key=0f8797897-ba73-40bb-9b74-366ef‌​03c2cbf'; 
// Initiate curl 
$ch1 = curl_init(); 
// Disable SSL verification 
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); 
// Set the url 
curl_setopt($ch1, CURLOPT_URL,$url1); 
// Execute 
$result1=curl_exec($ch1); 
// Closing 
curl_close($ch1); 
// Will dump a beauty json :3 
$jsonObj1 = json_decode($result1);
}
if(isset($_GET['id2'])) {
$id2 = $_GET['id2'];
$url2 = 'http://-withheld-/api/v1/tickets/'.$id2.'?api_key=0f8797897-ba73-40bb-9b74-366ef‌​03c2cbf'; 
// Initiate curl 
$ch2 = curl_init(); 
// Disable SSL verification 
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); 
// Set the url 
curl_setopt($ch2, CURLOPT_URL,$url2); 
// Execute 
$result2=curl_exec($ch2); 
// Closing 
curl_close($ch2); 
// Will dump a beauty json :3 
$jsonObj2 = json_decode($result2);
}
}
if(isset($_GET['id3'])) {
$id3 = $_GET['id3'];
$url3 = 'http://-withheld-/api/v1/tickets/'.$id3.'?api_key=0f8797897-ba73-40bb-9b74-366ef‌​03c2cbf';
// Initiate curl 
$ch3 = curl_init(); 
// Disable SSL verification 
curl_setopt($ch3, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); 
// Set the url 
curl_setopt($ch3, CURLOPT_URL,$url3); 
// Execute 
$result3=curl_exec($ch3); 
// Closing 
curl_close($ch3); 
// Will dump a beauty json :3 
$jsonObj3 = json_decode($result3);
}
}

然后显示结果:
if(isset($jsonObj1)){
foreach ($jsonObj1->{'tickets'} as $ticket1) { 
echo '<tr>'; echo '<td>'.$ticket1->{'id'}.'</td>'; echo '<td>'.$ticket1->{'number'}.'</td>'; echo '<td>'.$ticket1->{'customer_business_then_name'}.'</td>'; echo '<td>'.$ticket1->{'subject'}.'</td>'; echo '<td>'.$ticket1->{'created_at'}.'</td>'; echo '<td>'.$ticket1->{'status'}.'</td>'; echo '<td>'.$ticket1->{'problem_type'}.'</td>'; echo '<td>'.$ticket1->{'updated_at'}.'</td>'; echo '<td><a href="'.$link_addr.'">View</a></td>'; echo '<html></tr></html>';
}
}

if(isset($jsonObj2)){
foreach ($jsonObj2->{'tickets'} as $ticket2) { 
echo '<tr>'; echo '<td>'.$ticket2->{'id'}.'</td>'; echo '<td>'.$ticket2->{'number'}.'</td>'; echo '<td>'.$ticket2->{'customer_business_then_name'}.'</td>'; echo '<td>'.$ticket2->{'subject'}.'</td>'; echo '<td>'.$ticket2->{'created_at'}.'</td>'; echo '<td>'.$ticket2->{'status'}.'</td>'; echo '<td>'.$ticket2->{'problem_type'}.'</td>'; echo '<td>'.$ticket2->{'updated_at'}.'</td>'; echo '<td><a href="'.$link_addr.'">View</a></td>'; echo '<html></tr></html>';
}
}

if(isset($jsonObj3)){
foreach ($jsonObj3->{'tickets'} as $ticket3) { 
echo '<tr>'; echo '<td>'.$ticket3->{'id'}.'</td>'; echo '<td>'.$ticket3->{'number'}.'</td>'; echo '<td>'.$ticket3->{'customer_business_then_name'}.'</td>'; echo '<td>'.$ticket3->{'subject'}.'</td>'; echo '<td>'.$ticket3->{'created_at'}.'</td>'; echo '<td>'.$ticket3->{'status'}.'</td>'; echo '<td>'.$ticket3->{'problem_type'}.'</td>'; echo '<td>'.$ticket3->{'updated_at'}.'</td>'; echo '<td><a href="'.$link_addr.'">View</a></td>'; echo '<html></tr></html>';
}
}

非常感谢您的快速回复!我按照您指定的方式尝试了这段代码:不幸的是,它返回了一个空表。正如我在问题中指定的那样,我没有使用SQL数据库。我正在使用来自API的CURL。 - DragonKyn
`$url = 'http://-withheld-/api/v1/tickets/'.$id.'?api_key=0f8797897-ba73-40bb-9b74-366ef03c2cbf'; // Initiate curl $ch = curl_init(); // Disable SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Will return the response, if false it print the response curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set the url curl_setopt($ch, CURLOPT_URL,$url); // Execute $result=curl_exec($ch); // Closing curl_close($ch); // Will dump a beauty json :3 $jsonObj = json_decode($result); ?> ` - DragonKyn
刚才注意到在第二个JSON解码时犯了一个小错误 - 设置URL时,$url1应该是$url2。 - Shane Buckley
仍然出现以下错误:解析错误:语法错误,意外的“as”(T_AS) - DragonKyn
这是用于网站吗? - Shane Buckley
显示剩余30条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接