简体   繁体   English

从HTML中的PHP DB查询渲染数据

[英]Render data from PHP DB query in HTML

I am a newbie to PHP but trying to learn it to enhance my programming skillset 我是PHP的新手,但尝试学习它以提高我的编程技能

So far i have the following PHP code in my page to return some data from my Database: 到目前为止,我的页面中包含以下PHP代码以从数据库中返回一些数据:

<?php
            //code missing to retrieve my data
            while($row = mysql_fetch_array($result))
            {
                echo '$row['Name']';
            }

            mysql_close($connection);
?>

This is working in that I can see the names from my database displayed on screen. 这是因为我可以在屏幕上看到数据库中的名称。 I have seen as well that I can include html in the echo to format the data. 我也看到我可以在回显中包含html以格式化数据。 However if I have the html code like below in a jQuery accordion outside my PHP code in the page - how can I dynamically place the Names in the specific h3 tags - so the first name in my table is Joe so that comes back in [0] element of array - is there a way I can reference this from my html code outside the php? 但是,如果我在页面的PHP代码之外的jQuery手风琴中具有如下所示的html代码-如何动态将Names放置在特定的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>

Try something like this: 尝试这样的事情:

<?php while($row = mysql_fetch_array($result)) { ?>
    <h3><?php echo $row['name']; ?></h3>
    <div>
        <p>Some blurb about Joe</p>
    </div>
<?php } ?>

I'm assuming 'Some blurb about Joe' would also have to be replaced by a field in the DB, which you can accomplish in the same manner as the name. 我假设“关于Joe的某些内容”也必须替换为数据库中的一个字段,您可以使用与名称相同的方式来完成此操作。

@Gert is correct - the original mysql API is deprecated and should not be used anymore. @Gert是正确的-不建议使用原始的mysql API,并且不再应使用。 Look into mysqli or PDO , instead. 查看mysqliPDO

add your html in while loop like this 像这样在while循环中添加html

<?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);
?>

Like this : 像这样 :

    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM