Jump to content

HTML and PHP while bucle


JonyBanano

Recommended Posts

Hello guys, it's complicate for me to write in English, but i'm gonna try it, sorry for that. I have a problem with a code, I don't know how to solve it, so if anyone could help, I'd be grateful.

 

I've just started using php, my knowledge is very limited.

 

 

I'd like to make a code, to load as many buttons as audio files contains a folder (called 'DIRECTORY') (in this case there are 5 files in that folder: 0.wav, 1.wav, 2.wav, 3.wav and 4.wav) The following code, shows 5 buttons, but pushing any of them, the 5 audio files reproduces at the same time.

 

I would like to reproduce each audio file pushing only its own button.

 

 


<?php
$nombre = 'DIRECTORY';
$indice = 0;
$total = 5;

while ($indice < $total){ 

echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
<input type="submit" name="escuchar2" value="Escuchar '. $indice .'">
</form>'; 

if(isset($_POST["escuchar2"]))
{
$file='./grab/'.$nombre.'/'.$indice.'.wav';

echo "<embed src =\"$file\" hidden=\"true\" autostart=\"true\"></embed>";

}
$indice++;
}

?>

 

Any help or suggestion is welcomed. Thank you very much

Regards

Link to comment
Share on other sites

A for loop is better in this situation, because it uses less code and this is exactly what it is used for.

for ($i = 0; $i < $total; $i++)
{
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
<input type="submit" name="escuchar2" value="Escuchar '. $i .'">
</form>';
}

 

Secondly, you need to move the if(isset($_POST... block out of the loop, so it is only executed once.

 

Also, you shouldn't use $_SERVER['PHP_SELF'] in this way because it opens up an XSS vulnerability. If you want the form to submit to itself, simply leave the action attribute blank or remove it completely.

 

Here is some updated code which should work:

<?php

if (!empty($_POST)) {
if (isset($_POST['escuchar2'])) {
	$nombre = 'DIRECTORY';		
	$sound  = $_POST['escuchar2'];

	$file = "./grab/$nombre/$sound.wav";

	echo '<embed src="' . $file . '" hidden="true" autostart="true"></embed>';		
}
} else {
$total = 5;

for ($i = 0; $i < $total, $i++)
{
	echo '<form method="post">
	<input type="submit" name="escuchar2" value="Escuchar '. $i .'">
	</form>';
}
}

Link to comment
Share on other sites

A for loop is better in this situation, because it uses less code and this is exactly what it is used for.

for ($i = 0; $i < $total; $i++)
{
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
<input type="submit" name="escuchar2" value="Escuchar '. $i .'">
</form>';
}

 

Secondly, you need to move the if(isset($_POST... block out of the loop, so it is only executed once.

 

Also, you shouldn't use $_SERVER['PHP_SELF'] in this way because it opens up an XSS vulnerability. If you want the form to submit to itself, simply leave the action attribute blank or remove it completely.

 

Here is some updated code which should work:

<?php

if (!empty($_POST)) {
if (isset($_POST['escuchar2'])) {
	$nombre = 'DIRECTORY';		
	$sound  = $_POST['escuchar2'];

	$file = "./grab/$nombre/$sound.wav";

	echo '<embed src="' . $file . '" hidden="true" autostart="true"></embed>';		
}
} else {
$total = 5;

for ($i = 0; $i < $total, $i++)
{
	echo '<form method="post">
	<input type="submit" name="escuchar2" value="Escuchar '. $i .'">
	</form>';
}
}

Thank you scootstah. I tried it, but it didn't run.

 

5 buttons appears but pushing any of them, nothing happened.

 

I modify a bit the code in this way:

<?php

if (!empty($_POST)) {
if (isset($_POST['escuchar2'])) {

	$nombre = ''DIRECTORY'';	

	$sound  = $_POST['escuchar2'];

	$i = 0;

	$filee='./grab/'.$nombre.'/'.$i.'.wav';


	echo '<embed src="' . $filee . '" hidden="true" autostart="true"></embed>';		
}
} else {
$total = 5;

for ($i = 0; $i < $total; $i++)
{

$file='./grab/'.$nombre.'/'.$i.'.wav';


	echo '<form method="post">
	<input type="submit" name="escuchar2" value="Escuchar '. $i .'">
	</form>';
}
}

?>

With this one, after pushing a button (any of them), the same file [0.wav] is reproduced. But the page blanked and all the buttons dissapeared

Link to comment
Share on other sites

Sorry, I made a typo.

<?php

if (!empty($_POST)) {
if (isset($_POST['escuchar2'])) {
	$nombre = 'DIRECTORY';		
	$sound  = $_POST['escuchar2'];

	$file = "./grab/$nombre/$sound.wav";

	echo '<embed src="' . $file . '" hidden="true" autostart="true"></embed>';		
}
} else {
$total = 5;

for ($i = 0; $i < $total; $i++)
{
	echo '<form method="post">
	<input type="submit" name="escuchar2" value="Escuchar '. $i .'">
	</form>';
}
}

 

I tested it this time, and it does what is expected.

 

In the future, it would be good to turn on error reporting. The fact that you didn't see this error, and saw a blank screen, is indication to me that you don't have error reporting on. A quick way to enable it (on a per-script or per-application basis) would be to put this at the top of your script:

error_reporting(-1);
ini_set('display_errors', 1);

 

You can also turn those on in your php.ini to make it site-wide (more about that here).

Link to comment
Share on other sites

Thank you scootstah, but same happened. Audio files sound  :shrug:

 

Could you reproduce the audio file? In my case, the 5 buttons appear, but as in the previous case, after pushing one of them, the page blanks and no audio is reproduced.

 

 

For example, in the following code, 2 buttons appears, with the 1st one 1.wav is reproduced and the 2nd one 2.wav

 

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="submit" name="escuchar" value="Escuchar 01">
</form>

<?php
if(isset($_POST["escuchar"]))
{
$file='./musica/1.wav';

echo "<embed src =\"$file\" hidden=\"true\" autostart=\"true\"></embed>";
}
?>


<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="submit" name="escuchar2" value="Escuchar 02">
</form>

<?php
if(isset($_POST["escuchar2"]))
{
$file='./musica/2.wav';

echo "<embed src =\"$file\" hidden=\"true\" autostart=\"true\"></embed>";
}
?>

 

?Is possible to have it with a for bucle?

Link to comment
Share on other sites

Well, I don't have the audio files, so I obviously can't test that.

 

But, I echo'd out the value of $file and it was correct.

 

However, the page should be blank once submitted, based on that code. Nothing visual is output after the submit button is pressed. View the page source, and confirm that the HTML is correct, and that the path to the audio file is correct.

Link to comment
Share on other sites

In the future, it would be good to turn on error reporting. The fact that you didn't see this error, and saw a blank screen, is indication to me that you don't have error reporting on. A quick way to enable it (on a per-script or per-application basis) would be to put this at the top of your script:
error_reporting(-1);
ini_set('display_errors', 1);

 

You can also turn those on in your php.ini to make it site-wide (more about that here).

You are right, I don't have any error reporting. Thank you, I'll take a look about that.

 

In fact, I'm a begginer in php code (or any others codes). You know, many problems to do whatever I try. Step by step.

 

Regards

Link to comment
Share on other sites

Well, I don't have the audio files, so I obviously can't test that.

 

But, I echo'd out the value of $file and it was correct.

 

However, the page should be blank once submitted, based on that code. Nothing visual is output after the submit button is pressed. View the page source, and confirm that the HTML is correct, and that the path to the audio file is correct.

But in your code, the value $file would try to reproduce the file in the path $file = "./grab/$nombre/$sound.wav"; ? or would try to reproduce $file = "./grab/$nombre/0.wav"; in the first button for example?

 

Otherwise, I don't want to blank the page once submitted.

 

Maybe my code is not well planned, for example, it would help me to know if the following code is possible to have with a bucle (for/while...) function:

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="submit" name="escuchar" value="Escuchar 01">
</form>

<?php
if(isset($_POST["escuchar"]))
{
$file='./musica/1.wav';

echo "<embed src =\"$file\" hidden=\"true\" autostart=\"true\"></embed>";
}
?>


<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="submit" name="escuchar2" value="Escuchar 02">
</form>

<?php
if(isset($_POST["escuchar2"]))
{
$file='./musica/2.wav';

echo "<embed src =\"$file\" hidden=\"true\" autostart=\"true\"></embed>";
}
?>

 

Thank you!!!

Link to comment
Share on other sites

If your directory doesn't contain any wav files that you do NOT want to use for this, then there is an easier solution. The following will build the buttons (and play the selected file) automatically by reading the directory where the files reside. No need to define the start and end of the file numbers and the files don't even need to be named numerically.

 

$nombre = 'DIRECTORY';
$ext = 'wav';

//Get list of wav files from directory
//Requires file ext to be same case, i.e. 'wav'
$files = glob("{$nombre}/*.{$ext}");

foreach ($files as $file)
{ 
    $fileName = basename(substr($file, 0, strrpos($file, '.')));
    echo "<form method='post' action=''>
          <input type='submit' name='escuchar2' value='{$fileName}'>
          </form>\n"; 
}

if(isset($_POST['escuchar2']))
{
    $file = $_POST["escuchar2"];
    echo "<embed src ='{$nombre}/{$file}.{$ext}' hidden='true' autostart='true'></embed>";
}

Link to comment
Share on other sites

Well, I had a long reply listed, then accidentally closed the window.  But, I now see it is almost in-line with Psycho's post.  But here it is, using a little AJAX to flesh it out a bit.

<?php
$directory = 'path/to/directory/'; //path to the directory (must have trailing slash).
$file_name = 'test3.php'; //the name of the current file (assigns to javascript function.
$list_of_files = glob($directory . '*.[wW][aA][vV]'); //get a list of files that reside in the directory. This will pick any file that ends in .WAV or .wav, it will also automatically pick them up, as they are added.
$list_of_files = array_filter($list_of_files,'is_file'); //just in case a directory skipped through.
if(!empty($_GET['name'])) { //if the get parameter exists, and isn't empty.
if(in_array($_GET['name'],$list_of_files)) { //and the get parameter exists in the file array.
	echo '<object id="myMovie" classid="CLSID:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" height="250" width="540">
			<param name="controls" value="ImageWindow">
			<param name="console" value="_master">
			<param name="center" value="true">
			<embed name="myMovie" src="' . $_GET['name'] . '" height="250" width="540" nojava="true" controls="ImageWindow" console="_master" center="true" pluginspage="http://www.real.com/"></embed>
			</object>'; //echo the object to the page.
	exit(); //and end the script.
} 
else { //else someone sent the wrong file to the get parameter.
	echo '<p>Invalid File!</p>'; //so tell them it is invalid!
	exit();//then end the script.
}
}

foreach($list_of_files as $file) { //for each file, create a link.
$links[] = $file . ' <a href="javascript:void(0);" onclick="play_file(\'' . $file . '\');">Play Now!</a>'; //links held in an array.
}

$file_links = (is_array($links)) ? implode('<br /><br />',$links) : 'No Files Present!'; //you can implode the links any way you like, I just double spaced them.

//this is heredoc syntax, and is used to output the css styling (to make it look similar to a button, you may need to play with it), the javascript to load the file, and populates the links.
//Upon clicking the Play Now link, the browser will request the object from the server, which will load through AJAX, so the page will not refresh.
echo <<<EOF
<style type="text/css">
a:link {
display: block; 
width: 8em; 
height: 1.5em; 
background-color: #999999; 
border-top: 1px solid #CCCCCC; 
border-right: 1px solid #333333;
border-bottom: 1px solid #333333; 
border-left: 1px solid #CCCCCC;
text-decoration: none; 
color: #000000; 
cursor: default;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function play_file(file) {
$.get("{$file_name}",{name: file}, function(data) { $('#play_file').html(data); });
}
</script>
<div id="file_links">{$file_links}</div>
<div id="play_file"> </div>
EOF;
?>

 

BTW, I put a $file_name variable in there, which should hold the value of what you name the file (including ext), this is for the JQuery function, and only because I dislike $_SERVER['PHP_SELF'] even if it would be a problem in this instance.

Link to comment
Share on other sites

But in your code, the value $file would try to reproduce the file in the path $file = "./grab/$nombre/$sound.wav"; ? or would try to reproduce $file = "./grab/$nombre/0.wav"; in the first button for example?

 

Well, $sound variable is the value of the submit button. So for the first button it would be "Escuchar0", the second would be "Escuchar1", and so on.

 

Otherwise, I don't want to blank the page once submitted.

 

Okay, you can remove the else block then. This way, you are only running the embed HTML if a button was submitted, and you still display all the buttons after it was submitted.

Link to comment
Share on other sites

If your directory doesn't contain any wav files that you do NOT want to use for this, then there is an easier solution. The following will build the buttons (and play the selected file) automatically by reading the directory where the files reside. No need to define the start and end of the file numbers and the files don't even need to be named numerically.

 

$nombre = 'DIRECTORY';
$ext = 'wav';

//Get list of wav files from directory
//Requires file ext to be same case, i.e. 'wav'
$files = glob("{$nombre}/*.{$ext}");

foreach ($files as $file)
{ 
    $fileName = basename(substr($file, 0, strrpos($file, '.')));
    echo "<form method='post' action=''>
          <input type='submit' name='escuchar2' value='{$fileName}'>
          </form>\n"; 
}

if(isset($_POST['escuchar2']))
{
    $file = $_POST["escuchar2"];
    echo "<embed src ='{$nombre}/{$file}.{$ext}' hidden='true' autostart='true'></embed>";
}

:o This is exactly what I was looking for. #wow thank you so much!!!!

 

I tried it and I think it fits perfectly.

 

Thank you too jcbones, I don't know about AJAX, but I'll watch your code too. I need some time to understand it (never work with javascript before).

 

 

Moderators, please, give me a couple of days to try these codes well before close or mark this post as solved.

 

Many thanks scootstah, Psycho and jcbones for your post. This help me a lot, I really appreciate it.

Link to comment
Share on other sites

But in your code, the value $file would try to reproduce the file in the path $file = "./grab/$nombre/$sound.wav"; ? or would try to reproduce $file = "./grab/$nombre/0.wav"; in the first button for example?

 

Well, $sound variable is the value of the submit button. So for the first button it would be "Escuchar0", the second would be "Escuchar1", and so on.

OK, I thought I need to define it somewhere else.

 

Otherwise, I don't want to blank the page once submitted.

 

Okay, you can remove the else block then. This way, you are only running the embed HTML if a button was submitted, and you still display all the buttons after it was submitted.

To know this will be interesting for me.

 

Thanks for your help scootstah

Link to comment
Share on other sites

Moderators, please, give me a couple of days to try these codes well before close or mark this post as solved.

 

You are responsible for marking your post solved (although the staff can do that too). You should go ahead and do that now. You can always come back later and make it unsolved if you have issues or open a separate thread if the issue is somewhat different.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.