Jump to content

xml to php table


myitalan

Recommended Posts

Greetings,

 

Thank you for your posts... it's been very helpful.

 

I have an issue i hope you can help me with regarding parsing an xml file to table in php.

 

 

 

1. I have an input form that writes an xml file. (it works)

 

2. the xml file it writes is in this format:

 

 

<?xml version="1.0" encoding="iso-8859-1"?>

<images>
  <title>2</title>
  <description>2</description>
  <tmg></tmg>
  <img>2</img>
</images>

 

3. Now, this is what I need for my flash gallery to play and I can't change that.

 

4. The Problem is when I list all the images in the admin section to add and delete, I am getting a parsing error.

 

    - Clue the parser will work if I format the xml file like this:

 

      <images>
          <image title="2" description="2" tmb="2" img="2" />
      </images>

 

5. Finally, yes I would write to that format using:

 

$write_string = "<images>";
foreach($images_final as $image){
    $write_string .= "<image title=\"$image[title]\" description=\"$image[description]\" tmb=\"$image[tmb]\" img=\"$image[img]\" />";
}
$write_string .= "</images>";
$fp = fopen("gallery.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);

 

....but my flash gallery won't work.

 

 

 

Here is what I'm using that is generating the error.

 

<?php
function start_tag($parser, $name, $attrs){
    global $table_string;
    $table_string .= "<tr><td>[title]</td><td>[description]</td><td>[tmb]</td><td>[img]</td></tr>";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
$table_string = "<table>
        <tr><th>Title</th><th>Description</th><th>Thumbnail</th><th>Image</th></tr>";
xml_parse($parser, file_get_contents("gallery.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;
?>

 

Thank you in advance for your help!!

 

 

 

Alan

Link to comment
Share on other sites

What kind of error? It seems to parse fine for me

 

<?php

$data = '<?xml version="1.0" encoding="iso-8859-1"?>

<images>
  <title>2</title>
  <description>2</description>
  <tmg></tmg>
  <img>2</img>
</images>';

$xml = new SimpleXMLElement($data);

print_r($xml);

?>

Link to comment
Share on other sites

Thank you for your reply.  I thought no one was going to reply so I went in another direction.  However I would appreciate if you take a look at my new road block.

 

I am still editing an xml file.. uploading images and updating the xml file.  Here is my error followed by the code.

 

Notice: Undefined variable: _FILES2 in /playlistaction.php on line 16

 

Notice: Undefined variable: _FILES2 in /playlistaction.php on line 17

 

Notice: Undefined variable: _FILES2 in /playlistaction.php on line 18

 

Notice: Undefined variable: _FILES2 in /playlistaction.php on line 19

 

Warning: Wrong parameter count for move_uploaded_file() in /playlistaction.php on line 22

Error uploading file

 

-------------------------------

<?php

ini_set('display_errors',1);

error_reporting(-1);

 

$uploadDir = 'photoGallery/';

$uploadDir2 = 'photoGallery/';

 

if(isset($_POST['upload']))

{

$fileName = $_FILES['userfile']['name'];

$tmpName = $_FILES['userfile']['tmp_name'];

$fileSize = $_FILES['userfile']['size'];

$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

 

$fileName2 = $_FILES2['userfile2']['name2'];

$tmpName2 = $_FILES2['userfile2']['tmp_name2'];

$fileSize2 = $_FILES2['userfile2']['size2'];

$fileType2 = $_FILES2['userfile2']['type2'];

$filePath2 = $uploadDir2 . $fileName2;

 

$result = move_uploaded_file($tmpName, $tmpName2, $filePath, $filePath2);

if (!$result) {

                echo "Error uploading file";

                exit;

              }

 

}

 

$song = array(

    'title' => $_POST['title'],

    'description' => $_POST['description'],

    'tmb' => $filePath,

    'img' => $filePath2,

);

   

$doc = new DOMDocument();

$doc->load( 'photoGallery.xml' );

 

$doc->formatOutput = true;

$r = $doc->getElementsByTagName("musiclist")->item(0);

 

$b = $doc->createElement("song");

 

$title = $doc->createElement("title");

$title->appendChild(

    $doc->createTextNode( $song["title"] )

);

$b->appendChild( $title );

 

$description = $doc->createElement("description");

$description->appendChild(

    $doc->createTextNode( $song["description"] )

);

$b->appendChild( $description );

 

$tmb = $doc->createElement("tmb");

$tmb->appendChild(

    $doc->createTextNode( $song["tmb"] )

);

 

$img = $doc->createElement("img");

$img->appendChild(

    $doc->createTextNode( $song["img"] )

);

 

$b->appendChild( $tmb );

$b->appendChild( $img );

$r->appendChild( $b );

   

$doc->save("photoGallery.xml");

 

    header("Location: {$_SERVER['HTTP_REFERER']}");   

?>

 

Link to comment
Share on other sites

updated based on your hint, and suggestion.

 

No errors!!!! but no action????

 

no entries added

 

<?php

ini_set('display_errors',1);

error_reporting(-1);

 

$uploadDir = 'photoGallery/';

$uploadDir2 = 'photoGallery/';

 

if(isset($_POST['upload']))

{

$fileName = $_FILES['userfile']['name'];

$tmpName = $_FILES['userfile']['tmp_name'];

$fileSize = $_FILES['userfile']['size'];

$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

 

$fileName2 = $_FILES['userfile2']['name'];

$tmpName2 = $_FILES['userfile2']['tmp_name'];

$fileSize2 = $_FILES['userfile2']['size'];

$fileType2 = $_FILES['userfile2']['type'];

$filePath2 = $uploadDir2 . $fileName2;

 

$result = move_uploaded_file($tmpName, $filePath,);

if (!$result) {

                echo "Error uploading file";

                exit;

              }

 

}

 

$song = array(

    'title' => $_POST['title'],

    'description' => $_POST['description'],

    'tmb' => $filePath,

    'img' => $filePath2,

);

   

$doc = new DOMDocument();

$doc->load( 'photoGallery.xml' );

 

$doc->formatOutput = true;

$r = $doc->getElementsByTagName("musiclist")->item(0);

 

$b = $doc->createElement("song");

 

$title = $doc->createElement("title");

$title->appendChild(

    $doc->createTextNode( $song["title"] )

);

$b->appendChild( $title );

 

$description = $doc->createElement("description");

$description->appendChild(

    $doc->createTextNode( $song["description"] )

);

$b->appendChild( $description );

 

$tmb = $doc->createElement("tmb");

$tmb->appendChild(

    $doc->createTextNode( $song["tmb"] )

);

 

$img = $doc->createElement("img");

$img->appendChild(

    $doc->createTextNode( $song["img"] )

);

 

$b->appendChild( $tmb );

$b->appendChild( $img );

$r->appendChild( $b );

   

$doc->save("photoGallery.xml");

 

    header("Location: {$_SERVER['HTTP_REFERER']}");   

?>

Link to comment
Share on other sites

Things that used to work isn't, so i had to rebuild to this point.

 

I am stuck... don't know if the second image should be included with the first group of code or if it should be separate being that it's no errors just a blank screen i don't know what to do.

 

Would you mind a little push?

 

Thx

Link to comment
Share on other sites

Ohhh  by far am I  not afraid to admit my ignorance with coding.  I can make minor edits to php but in no way skilled.  I do respect your craft as why I don't just ask for you to give it to me as it is better for me in the long run and by the time of my first post and now should show that I'm not afraid to put in the time just to get one function working.  I've been at it for 9+ hours and just asking for a push as I am truly stuck.  At this point I'd just be messing up what I got... I made changes from your hint, but now I'm wondering if i actually made the right changes.... What is right about my code and should leave alone and what wrong.

 

Thanks

Link to comment
Share on other sites

I'm a volunteer. I can't be here 24/7 waiting for replies from you ;) Other stuff going on.

 

If you'd like, I'm more than happy to help you do this right... by that I mean start from scratch and make sure you understand what each line is doing. If you just need it done ASAP, I suggest going over to the freelance forum, and perhaps requesting the user explains the code as well, if you have the extra money.

 

What you're asking isn't help, it's pretty much standing behind you while you blindly make changes to the code. I'd much rather take the time to help you learn PHP.

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.