繁体   English   中英

如何将数据放入html?

[英]How I can put data in the html?

我将在该网站管理员中创建一个网站,然后在服务器中上传一些音频文件,一切正常,但是我在该文件中上传了一个PHP文件,我将从数据库中获取所有数据(文件名和该文件的URL),但是我不知道如何将这些数据放入HTML代码。 在HTML中,当用户单击名称时,URL应该会加载并播放音乐。

<?php
 define('DB_HOST','localhost');
 define('DB_USERNAME','Root');
 define('DB_PASSWORD','');
 define('DB_NAME','audiofiles');

 //connecting to the db
 $con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or 
 die("Unable to connect");

 //sql query 
 $sql = "SELECT * FROM audio";

//getting result on execution the sql query
$result = mysqli_query($con,$sql);

//response array
$response = array();

$response['audio'] = array();

//traversing through all the rows

 while($row =mysqli_fetch_array($result)){
 $temp = array();
 $temp['id'] = $row['id'];
 $temp['name'] = $row['name'];
 $temp['url'] = $row['url'];
 array_push($response['audio'],$temp);
 }

这是我从数据库中获取音频文件的PHP。 我需要一些帮助,以便在HTML页面中显示该音频文件并播放该文件。 我想要做的是,PHP文件应该获取音频文件的名称和链接,这样我现在想让HTML将名称和链接作为src嵌入即可正常工作。

只需在while()循环内使用<audio> HTML标签while()如下所示:-

while($row =mysqli_fetch_array($result)){?>
    <?php echo $row['name'];?> : <audio controls><source src="<?php echo $row['url'];?>"></audio><br>
<?php }?>

因此完整的代码必须是:

<?php
 define('DB_HOST','localhost');
 define('DB_USERNAME','Root');
 define('DB_PASSWORD','');
 define('DB_NAME','audiofiles');

 //connecting to the db
 $con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or 
 die("Unable to connect");

 //sql query 
 $sql = "SELECT * FROM audio";

//getting result on execution the sql query
$result = mysqli_query($con,$sql);

//traversing through all the rows

while($row =mysqli_fetch_array($result)){?>
    <?php echo $row['name'];?> : <audio controls><source src="<?php echo $row['url'];?>"></audio><br>
<?php }?>

暂无
暂无

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

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