Javascript variable function in php

Hello, it’s posible that function? Because sonting it’s wrong.

<div style= "height: 400px;
  width: 80%;
  background-color: powderblue;
  overflow:scroll;">
<?php
				include 'config.php';

$sql ="SELECT * FROM playlist ORDER BY name DESC";
$result = $conn->query($sql);

	if ( !empty($result->num_rows) && $result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
		$play = "playlist1/".$row["name"];	
			
			
			?>
			
				<button onclick="<?php echo $row["name"] ?>()"> <?php echo $row["name"] ?></button>
				<script type="text/javascript">
				function <?php echo $row["name"] ?>(){
					
				   new Audio("<?php echo $play ?>").play();
				
			};
			</script>
				<?php		
	}}
	$conn->close();
?>
</div>

Sorry to hear something is wrong…

but if you dont explain or tell us… how can we help?

guess?

I’m trying to insert from database path to sound on my pc and after klick button with sound to play this sound.

After click button with some sound, noting hapenning. 100% i do somting wrong :slight_smile:

1.) Why arent you just using a single function and passing in the name as a param/argument? (instead fo making anew function for each button you are creating?)

If you replace:


new Audio("<?php echo $play ?>").play();

with:

alert( <?php echo $row["name"] ?>);

Do you get the alert/notice?

replace code and noting …
dont get allert/notice.

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)

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service