我是PHP的新手但是试图学习它来提高我的编程技能
到目前为止,我的页面中有以下PHP代码可以从我的数据库中返回一些数据:
<?php
//code missing to retrieve my data
while($row = mysql_fetch_array($result))
{
echo '$row['Name']';
}
mysql_close($connection);
?>
这是有效的,我可以在屏幕上看到我的数据库中的名字。我也看到过,我可以在echo中包含html来格式化数据。但是,如果我在页面中的PHP代码之外的jQuery手风琴中有如下所示的html代码 - 我如何动态地将名称放在特定的h3标签中 - 所以我的表中的第一个名字是Joe,所以它返回[0 ]数组的元素 - 有没有办法可以从我在php之外的html代码中引用它?
<div id="accordion">
<h3>Joe</h3>
<div>
<p>
Some blurb about Joe
</p>
</div>
<h3>Jane</h3>
<div>
<p>
Some blurb about Jane
</p>
</div>
<h3>John</h3>
<div>
<p>
Some Blurb about John.
</p>
</div>
</div>
答案 0 :(得分:1)
尝试这样的事情:
<?php while($row = mysql_fetch_array($result)) { ?>
<h3><?php echo $row['name']; ?></h3>
<div>
<p>Some blurb about Joe</p>
</div>
<?php } ?>
我假设'关于Joe的一些模糊'也必须被DB中的一个字段替换,你可以用与名称相同的方式来完成。
@Gert是正确的 - original mysql API已被弃用,不应再使用了。相反,请查看mysqli或PDO。
答案 1 :(得分:0)
像这样在
<?php
//code missing to retrieve my data
while($row = mysql_fetch_array($result))
{
?>
<h3><?php echo $row['Name']?></h3>
<div>
<p>
Some blurb about <?php echo $row['Name']?>
</p>
</div>
<?php
}
mysql_close($connection);
?>
答案 2 :(得分:0)
像这样:
<div id="accordion">
<?php
while($row = mysql_fetch_array($result))
{
<h3><?php echo $row['Name'] ?></h3>
<div>
<p>
Some blurb about Joe
</p>
</div>
} ?>
</div>