myitalan Posted July 26, 2012 Share Posted July 26, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/ Share on other sites More sharing options...
KevinM1 Posted July 26, 2012 Share Posted July 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1364705 Share on other sites More sharing options...
myitalan Posted July 28, 2012 Author Share Posted July 28, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1365165 Share on other sites More sharing options...
KevinM1 Posted July 29, 2012 Share Posted July 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1365193 Share on other sites More sharing options...
myitalan Posted August 1, 2012 Author Share Posted August 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1365873 Share on other sites More sharing options...
Christian F. Posted August 1, 2012 Share Posted August 1, 2012 $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. Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1365887 Share on other sites More sharing options...
myitalan Posted August 7, 2012 Author Share Posted August 7, 2012 Thanks for the reply, but not only am i still stuck with the original question, you added another option that I have no clue... Where in my code can I change it from adding to updating? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1367528 Share on other sites More sharing options...
Christian F. Posted August 7, 2012 Share Posted August 7, 2012 You need to find the element that you want to update, and then either delete + replace it, or update whatever value you want to update. Read here for more information on how to do this: http://no.php.net/DOMDocument Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1367533 Share on other sites More sharing options...
myitalan Posted August 7, 2012 Author Share Posted August 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1367558 Share on other sites More sharing options...
Christian F. Posted August 8, 2012 Share Posted August 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266528-further-adventures-in-xml/#findComment-1367923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.