Not quite sure your handling of the DB/connection (results) is correct…
This works:
$sql ="SELECT * FROM playlist ORDER BY name DESC";
$results = $conn->query($sql);
foreach ($results as $result) {
print $result['name'] . "\t";
//print $result['length'] . "<br>";
}
Perhaps try this:
<div style= "height:400px; width:80%; background-color:powderblue; overflow:scroll;">
<?
//include 'config.php';
$playListDetails_sql = "SELECT * FROM playlist ORDER BY name DESC";
$playListDetails_stmt = $conn->prepare($playListDetails_sql);
$playListDetails_stmt->execute();
$playListDetails_stmt->setFetchMode(PDO::FETCH_ASSOC);
$myPlayList = $playListDetails_stmt->fetchAll(); //returns multi-dimensional array
$colcount = $playListDetails_stmt->columnCount();
$rowcount = $playListDetails_stmt->rowCount();
if($rowcount == 0){
echo 'NO PLAYLIST RETURNED';
}else{
echo 'PLAY LIST FOUND<br><br>';
for($i=0; $i<count($myPlayList); $i++){
//output button
?>
<button onclick="playSong('<?= $myPlayList[$i]["name"] ?>');"> <?= $myPlayList[$i]["name"] ?></button><br>
<?
}
//posted outside of loop and s single static function with argument used
?>
<script type="text/javascript">
function playSong($targetSong){
//new Audio("playlist1/".$targetSong).play();
alert($targetSong);
};
</script>
<?
}
?>
</div>
I dont know what your config.php file does or is for… but I dont have it… so its commented out.
- single JS function (not one for each button)
- output is fine
- clicking on each button prompts alert with dynamic button name populated
at this point… its a working model, but I dont have any audio file to trigger to test the audio portion (which I’m not 100% sure if being used correctly anyways)