我目前正在构建一个使用MobiOne studio的应用程序,以便使用它们的发布功能。然而,我遇到了一个独特的问题。该应用程序最基本的功能是向用户查询要输入到托管MYSQL数据库中的数据。然后通过搜索框,你可以从同一个MYSQL数据库中召回某些记录。插入部分工作得很好。但是,我无法将结果显示出来。我可以在任何旧的浏览器中使用一个简单的PHP代码片段来显示循环中的记录,以获得尽可能多的结果。
但是,PHP在客户端运行的情况下并不能很好地运行。MobiOne更喜欢使用Javascript/JSON/JQuery来显示数据。有没有办法通过Javascript将PHP脚本服务器端创建的循环内容显示到HTML块或DOM?
下面是我用来从一个id为“index.php”的GET表单中查找内容的PHP代码。就像我说的,这很好用。但是我需要能够获得这个输出的数据,并用一些JS显示它。
如果您不确定,请查看此链接作为代码实际功能的示例。只需输入"cracker“作为测试搜索查询即可。
http://tjrcomputersolutions.com/apps/edibles/v1.0/index.php
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['query']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "localhost"; //server
$db = "*****"; //database name
$user = "*****"; //database user name
$pwd = "*****"; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM items WHERE item LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whether there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "Item: " . $row['item'] . "<br />";
$output .= "Store: " . $row['store'] . "<br />";
$output .= "Size: " . $row['size'] . "<br />";
$output .= "Price: " . $row['price'] . "<br /><br />";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>发布于 2013-11-02 07:27:11
您可以使用json_encode将PHP数组转换为JSON格式。例如:
$items = array(
array(
'Item' => 'a',
'Store' => 'b',
'Size' => 'c',
'Price' => 'd',
),
array(
'Item' => 'e',
'Store' => 'f',
'Size' => 'g',
'Price' => 'h',
),
);
echo json_encode($items); // [{"Item":"a","Store":"b","Size":"c","Price":"d"},{"Item":"e","Store":"f","Size":"g","Price":"h"}]在本例中,您必须使用DB中的数据挂载数组结构,然后调用此函数将其转换为JSON。
https://stackoverflow.com/questions/19736967
复制相似问题