![](/img/trans.png)
[英]How can I return data from MYSQL as JSON using PHP so that I can use it in on my HTML page
[英]how can i display all the json data in the html page using the given php code
当我在搜索字段中输入电影名称时,我想显示所有是json文件的电影,但是使用此代码,我只能获得其中一部电影,请您帮助我。
<?php
if (isset($_POST['submit-search'])) {
$txtresult = $_POST['search'];
function getImdbRecord($title, $ApiKey)
{
$path = "http://www.omdbapi.com/?s=$title&apikey=$ApiKey";
$json = file_get_contents($path);
return json_decode($json, TRUE);
}
$data = getImdbRecord($txtresult, "f3d054e8");
echo "<div class = 'info-box'><img src =".$data['Poster']."</img><h3> Name :".$data['Title']."</h3><h3> Year : ".$data['Year']."</h3><h3> Duration : ".$data['Runtime'],"</h3></div>";
}
?>
您需要使用foreach循环来获取所有搜索结果。
喜欢。
$data = getImdbRecord($txtresult, "f3d054e8");
foreach($data['Search'] as $value){
echo "<div class = 'info-box'><img src =".$value['Poster']."</img><h3> Name :".$value['Title']."</h3><h3> Year : ".$value['Year']."</h3><h3> Duration : ".$value['Runtime'],"</h3></div>";
}
完整的代码将。
<?php
//add function out side the if condition
function getImdbRecord($title, $ApiKey){
$path = "http://www.omdbapi.com/?s=$title&apikey=$ApiKey";
$json = file_get_contents($path);
return json_decode($json, TRUE);
}
if (isset($_POST['submit-search'])) {
$txtresult = $_POST['search'];
$data = getImdbRecord($txtresult, "f3d054e8");
//use loop to get all the seacrh result.
foreach($data['Search'] as $value){
echo "<div class = 'info-box'><img src =".$value['Poster']."</img><h3> Name :".$value['Title']."</h3><h3> Year : ".$value['Year']."</h3><h3> Duration : ".$value['Runtime'],"</h3></div>";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.