2016-06-01 94 views
4

我正在使用API​​ curl方法來獲取JSON數據,然後將其插入到我的web應用程序的HTML格式表中。如何使用單個查詢創建多個搜索字段? [PHP/HTML]

我的需求稍有變化,我需要現在在單個搜索中搜索多個門票號碼。這是我目前..

<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> 

哦,還有,這裏是我的代碼,目前工作在搜索時獲得一票ID PHP部分。

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

我試圖用我上面的HTML代碼中添加更多的字段和下面這段代碼:

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

     } 

等等等等,如果有人可以幫助我,我會非常感激。我知道這是一個非常基本的問題,但我無法找到關於使用$ _GET方法查詢多個搜索的任何細節。我發現的所有內容都與MYSQL查詢有關。

在此先感謝您的時間和幫助!

+0

如果你改變你的字段ID1 ,id2,id3,並且只檢查是否設置了'id',如果你不再有id字段,就會遇到問題。 – Shane

回答

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>'; 
} 
} 
+0

非常感謝您的快速響應!我按照您的指定嘗試了這段代碼: 不幸的是,它返回一個空表。 我沒有使用SQL數據庫,正如我在上面的問題中指定的那樣。我使用API​​的CURL。 – DragonKyn

+0

好吧,我做了一些編輯,這應該爲你工作 –

+0

'$ url ='http://-withheld-/api/v1/tickets/'.$id。'?API_KEY = 0f8797897-ba73-40bb-9b74-366ef03c2cbf'; \t \t //啓動捲曲 \t \t $ ch = curl_init(); \t \t //禁用SSL驗證 \t \t curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE); \t \t //將返回響應,如果爲false,則打印響應 \t \t curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); \t \t //設置URL \t \t curl_setopt($ CH,CURLOPT_URL,$網址); \t \t //執行 \t \t $ result = curl_exec($ ch); \t \t //結束 \t \t curl_close($ ch); \t \t //將轉儲一個美麗的json:3 \t \t $ jsonObj = json_decode($ result); \t \t?> ' – DragonKyn

相關問題