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