i am trying input check box list current folder .mp3 get using gob function glob_grace list the audio files using check box then
get array check box values make xspf file using php
i write code all for array to xml
but small error please anyone tell me what error and the error where occurred !
<?php
foreach (glob("*.{mp3,mp4}", GLOB_BRACE) as $filename) {
$values = $filename ;
echo "<form action='p.php' method='POST'>";
echo "<input type='checkbox' name='foo[]' value='$values'>$values <hr>";
}
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";
?>
it will go to next page below
<?php
$g = $_POST['foo'];
$cnt = count($g);
//function definition to convert array to xml
function array_to_xml($array, &$xml_user_info) {
for ($i=0 ; $i < $cnt ;$i++)
{
$track = $xml_user_info->addChild('track');
$track->addChild("location",$array[$i]);
}
}
//creating object of SimpleXMLElement
$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><trackList></trackList>");
//function call to convert array to xml
array_to_xml($g,$xml_user_info);
//saving generated xml file
$xml_file = $xml_user_info->asXML('users.xspf');
//success and error message based on xml creation
if($xml_file){
echo 'XML file have been generated successfully.';
}else{
echo 'XML file generation error.';
}
?>
please give me answer solution working code
thanks in advance
If glob("*.{mp3,mp4}", GLOB_BRACE)
returns an array with more than 1 item, you are generating the <form action='p.php' method='POST'>
tag multiple times.
Maybe you could move the form declaration to outside of the foreach
.
If I submit the form and I load the p.php, I get this Notice:
Notice: Undefined variable: cnt
You are using $cnt = count($g);
to count the items, but you could also pass this $g
and count its items inside the function array_to_xml
.
I think that if you want to get the value from $_POST['foo']
, you should first check if it is a POST and then check if the $_POST['foo']
is set.
Maybe this setup can help you:
<?php
//function definition to convert array to xml
function array_to_xml($array, &$xml_user_info)
{
for ($i = 0; $i < count($array); $i++) {
$track = $xml_user_info->addChild('track');
$track->addChild("location", $array[$i]);
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['foo'])) {
$g = $_POST['foo'];
//creating object of SimpleXMLElement
$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><trackList></trackList>");
//function call to convert array to xml
array_to_xml($g, $xml_user_info);
//saving generated xml file
$xml_file = $xml_user_info->asXML('users.xspf');
//success and error message based on xml creation
if ($xml_file) {
echo 'XML file have been generated successfully.';
} else {
echo 'XML file generation error.';
}
}
}
?>