请有人帮我找到如何从 mysql 数据库检索数据并在 jquery mobile 的 ListView 中填充的解决方案。
我的php代码如下
<? php
include('libraries/config.php');
$result = mysql_query("SELECT * from category") or die(mysql_error());
while ($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
echo json_encode($data);
echo '{"Deals":'.json_encode($arr).'}';
?>
这里从mysql获取json格式的数据,但我不知道如何在listview中填充它,我的html页面如下
<div id="content-area" style="height:auto;">
<br/>
<ul data-role="listview">
<li>
<a href="comfort_list.html">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Comfort</b>
</div>
</div>
</a>
</li>
<li>
<a href="#">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Handling Your Lenses</b>
</div>
</div>
</a>
</li>
</ul>
</div>
在我的代码中,我给出了静态数据来列出舒适度和处理镜头的数据,我在数据库中拥有这些数据,我需要获取该数据并将其放置在这里。
在我以这样的形式发布的页面中
<form method="post" action="help.html">
<ul data-role="listview" >
<?php while ($row = $stmt->fetch()){
?>
<li>
<a href="help1.php?id=<?php echo $row['categoryID']; ?>">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b><?php echo $row['title'];?>
</b>
</div></div></a>
</li>
<?php }?>
</ul>
</form>
在另一个页面中我尝试获取 id,如下
<?php
include('libraries/config.php');
$getID = $_GET['id'];
echo $getID;
$stmt = $db->prepare("SELECT * from category where categoryID ='$_GET['id']'");
$stmt->execute();
?>
但是当转到该页面时,它什么也没显示,刷新时它就会显示,所以我只询问如何让它获取数据并在页面加载时打印它。谢谢。
最佳答案
正如 C.S. 所说,您不需要使用 JSON,只需循环数组并添加列表项即可。
编辑以展示 PDO 的基本用法,并演示如何访问查询结果。
从 PHP 5.5.0 开始,mysql_query
扩展已被弃用,因此我建议您使用 PDO_MySQL
首先设置数据库连接:
$dbName = "your_database_name_here";
$dbUserName = "your_username_here";
$dbPassword = "your_password_here";
$db = new PDO( 'mysql:host=localhost;dbname='.$dbName, $dbUserName, $dbPassword );
然后运行您的查询,并回显您的列表(或者通过访问 while
循环中的 $row
数组来从数据库中获取您需要的任何内容):
$stmt = $db->prepare("SELECT * from category");
$stmt->execute();
while ($row = $stmt->fetch()){
echo "<li>". $row['title'] ."</li>";
}
附:你应该摆脱内联CSS,这是一个坏习惯。
关于php - 如何从 mysql 数据库检索数据并填充到 jquery mobile 的 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20697576/