如何将数据放入html?

时间:2019-01-04 08:19:49

标签: php 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嵌入。

1 个答案:

答案 0 :(得分:3)

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

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