Jump to content

Further adventures in XML


myitalan

Recommended Posts

O.K. now that I have the button that display the data from the xml file

 

echo "<b>$title - $description - $tmb <input name=\"delete\" type=\"button\" value=\" \">\n</b><br>";}?>

 

I would like to place the title in text boxes in order to make changes.

 

This is what I got. Of course it broke

 

 

echo "<b>?> <input name="tupdate" type="text" id="tupdate" value="<?php echo "$title"; ?>" /> - $description - $tmb <input name=\"delete\" type=\"button\" value=\" \">\n</b><br>";}?>

 

Where am going wrong?

Link to comment
Share on other sites

You can't echo within an echo.

 

---

 

I think you need to step back and reorganize your code.  You're falling into one of the classic blunders (no, not starting a land war in Asia), but rather using PHP to echo out a ton of markup.

 

At it's roots, PHP is a template engine.  This means that PHP scripts are best served with all of the processing done first, with the results stored in variables, followed by 90%-95% pure markup (HTML, XML, whatever) with just enough PHP (simple echo statements, conditionals, loops) to output the data in those variables at the right spot.

 

In other words, instead of something like:

 

echo "<b>$title - $description - $tmb <input name=\"delete\" type=\"button\" value=\" \">\n</b><br>";}

 

You have something a hell of a lot cleaner:

 

<strong><?php echo "$title - $description - $tmb"; ?> <input name="delete" type="button" value="" /></strong>

 

As you progress in your coding journey, you'll hear the term Separation of Concerns.  What that means is that each component of your script should focus on one thing (it's concern) and be as separate as possible from the rest, interacting only when necessary.  Echoing markup blurs the separation between PHP and (in your case) XML, which in turn makes your code harder to debug, edit, and maintain.

 

PHP is best used for processing data.  Markup is best used to display things.  The only time they should interact is when dynamic data needs to be displayed, and in those cases, you're far better off doing the bulk of the work in the markup, using base PHP logic only where absolutely necessary.

Link to comment
Share on other sites

Thanks for the reply, however I am in need of placing the variable $title in a text box in order to edit the text.  I will be adding an update button next to the delete button.

 

This is my post:

 

O.K. now that I have a button after the data from the xml file

 

echo "<b>$title - $description - $tmb <input name=\"delete\" type=\"button\" value=\" \">\n</b><br>";}?>

 

I would like to place the variable $title in a text boxes in order to make changes. I will be adding an update button also.

 

This is what I did and of course it broke

 

echo "<b>?> <input name="tupdate" type="text" id="tupdate" value="<?php echo "$title"; ?>" /> - $description - $tmb <input name=\"delete\" type=\"button\" value=\" \">\n</b><br>";}?>

 

Where am going wrong?

Link to comment
Share on other sites

You're going wrong with trying to echo within an echo in that second line, like I originally said.  Get rid of the interior echo.

 

If that still doesn't make sense, then stop and brush up on the basics of PHP.  I'm getting the distinct impression that you're trying to run before you can walk.

Link to comment
Share on other sites

Thanks KevinM1

 

O.K. I have my xml data loaded into a form where i can make txt changes and display my image. I know this is the cleanest but i'm close to getting it done at least to get my friend going, then I'll come back and do as recommend.

 
?> <input type="text" id="title" name="title"  value="<?php echo $title ?>" > <input type="text" id="description" name="description"  value="<?php echo $description ?>" ><img src="<?php echo $tmb ?>" width="100" height="80" /><?php echo "<b> <input name=\"delete\" type=\"button\" value=\"Delete Image \"> <input name=\"Update\" type=\"submit\" value=\"Update Title/Descrition \">\n</b><br>";}?>

 

My issue now is when I hit update it just adds another record.  Here is my update code: 

<?php

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


$images = array(
    'title' => $_POST['title'],
    'description' => $_POST['description'],
    'tmb' => $filePath,
    'tmbs' => $_POST['tmbs'],
);
    
$doc = new DOMDocument();
$doc->load( 'photoGallery.xml' );

$doc->formatOutput = true;
$r = $doc->getElementsByTagName("gallery")->item(0);

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

$title = $doc->createElement("title");
$title->appendChild(
    $doc->createTextNode( $images["title"] )
);
$b->appendChild( $title );

$description = $doc->createElement("description");
$description->appendChild(
    $doc->createTextNode( $images["description"] )
);
$b->appendChild( $description );

$tmb = $doc->createElement("tmb");
$tmb->appendChild(
    $doc->createTextNode( $images["tmb"] )
);
$b->appendChild( $tmb );

$tmb = $doc->createElement("img");
$tmb->appendChild(
    $doc->createTextNode( $images["tmb"] )
);
$b->appendChild( $tmb );

$r->appendChild( $b );
    
$doc->save("photoGallery.xml");
//    header("Location: {$_SERVER['HTTP_REFERER']}");   
header("Location:adedit.php");
?> 

 

When i try to use w+ it breaks again.

Link to comment
Share on other sites

$b->appendChild( $tmb );
$r->appendChild( $b );

It's no wonder that it just adds a new record, as that's exactly what you're asking it to do. You need to either replace, or delete, the previous element. Not just add the new one.

"w+" is a red herring in this case, unless you want to replace the entire file. In which case it would be easier to just use a template for generating the XML, and "file_put_contents ()" to save it to disk.

Link to comment
Share on other sites

Would you make another attempt to help me understand where in this code the problem is.... well o.k. I know the problem is me, but I truly am lost.

 

i see that when I make a change to the data, the script instead of replacing the data, it adds the updates to the bottom of the file.  How do i stop it from adding without removing the old data.  In other words update the changes i make from the txt box

 

Thanks

Link to comment
Share on other sites

How would you find the element you want to replace? That's the first thing you need to figure out, then you need to figure out how to replace the data you want replaced.

The answer to both of these should be fairly obvious, after you've read the PHP manual and understood DOM traversing. That part, however, I cannot help you with.

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.