请有人帮助我找到如何从mysql数据库中检索数据并在jquery中填充列表视图的解决方案。
具有下面的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>在我的代码中,我给出了静态数据列表,这是舒适的和处理你的镜头,有这些数据在db和我需要得到和放置在这里。
在这一页中,我以这样的形式张贴
<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();
?>但是,当进入该页面时,它没有显示任何内容,当刷新时,它就显示了,因此只需要询问如何获取数据,并在加载页面时打印数据。谢谢。
发布于 2013-12-21 05:49:29
正如C.S.所说,您不需要使用JSON,只需遍历数组并添加列表项即可。
编辑展示了PDO的基本用法,并演示了如何访问查询结果.
mysql_query扩展在PHP5.5.0中被废弃,所以我建议您使用MySQL
首先设置数据库连接:
$dbName = "your_database_name_here";
$dbUserName = "your_username_here";
$dbPassword = "your_password_here";
$db = new PDO( 'mysql:host=localhost;dbname='.$dbName, $dbUserName, $dbPassword );然后运行您的查询,并回显您的列表(或通过访问$row循环中的while数组从数据库中获取所需的任何内容):
$stmt = $db->prepare("SELECT * from category");
$stmt->execute();
while ($row = $stmt->fetch()){
echo "<li>". $row['title'] ."</li>";
}附注:你应该摆脱那种内联css,这是一个坏习惯,进入。
https://stackoverflow.com/questions/20697576
复制相似问题