Php 如何在jquery mobile中从mysql数据库检索数据并在列表视图中填充

Php 如何在jquery mobile中从mysql数据库检索数据并在列表视图中填充,php,jquery,html,mysql,jquery-mobile,Php,Jquery,Html,Mysql,Jquery Mobile,请有人帮我找到如何从mysql数据库检索数据并在jquery mobile的列表视图中填充的解决方案 我的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 js

请有人帮我找到如何从mysql数据库检索数据并在jquery mobile的列表视图中填充的解决方案

我的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).'}';

?>
<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>
<?php
include('libraries/config.php');
$getID = $_GET['id'];
echo $getID;
$stmt = $db->prepare("SELECT * from category where categoryID ='$_GET['id']'");

$stmt->execute();

?>
这里我从mysql获取json格式的数据,但我不知道如何在listview中填充这些数据,我的html页面如下

<? 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).'}';

?>
<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>
<?php
include('libraries/config.php');
$getID = $_GET['id'];
echo $getID;
$stmt = $db->prepare("SELECT * from category where categoryID ='$_GET['id']'");

$stmt->execute();

?>
在我的代码中,我给列表提供了静态数据,这是舒适性和处理镜头的方式,我有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');

$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).'}';

?>
<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>
<?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的基本用法,并演示如何访问查询结果

从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 );
然后运行查询,通过访问while循环中的$row数组,从数据库中回显列表或任何需要的内容:


p、 你应该摆脱内联css,这是一个坏习惯。

请有人帮我解决问题为什么没有人回答我的问题?你的JSON数据看起来像什么?分类分类分类YD:舒适,标题:舒适,标题:舒适,提示:提示:提示:提示:[\1\,\2\,\3\3\,\4\,\5\,\5\,\6\,\6\,\6\,\7你们们可以粘贴你们们请你们们将你们们将你们们粘贴起来。你们们请你们们请将你们们请将其粘贴起来。分类分类分类分类分类YD:D你们们请你们们请你们们请你们们请你们们请你们们请你们们请你们们请你们们请你们们请将他们他们他们他们他们请将他们他们他们他们他们他们他们他们他们他们他们他们他们他们请请粘贴起来……………………………………。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。·类别ID:视力,标题:视力,提示:[\68\,\69\,\70\,\71\,\72\,\73\,\74\,\75\,\76\,\77\,\78\,\79\,\80\,\81\,\82\,\83\,\84\,\85\]}]}这就是我的json数据看起来的样子基本上你有一个数组,数组中有categoryID、title和tips的对象。你需要遍历数组中的每个对象,并将结果打印在tagshai daniel中。谢谢你的回复,但我怀疑如何在jquery mobile html页面中给出它。我需要吗使用php从数据库中检索数据的代码与我打印数据的代码相同,请告诉我Thankswanning:为ForEach提供的参数无效这是因为$resultArray未定义。这只是一个示例…我已编辑以显示更详细的演示。感谢daniel,它工作得很好,我还有一个疑问,单击这些特定的列表元素它必须转到另一个页面,提示必须以列表视图的形式显示在该页面中。怎么做?嗨,丹尼尔,你能帮我吗?