Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 6 123 ... LastLast
Results 1 to 15 of 76
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    88
    Thanks
    12
    Thanked 0 Times in 0 Posts

    PHP help to save, search and edit a text file

    Can I please get some help for the following. I have a very basic form, but I like to save the data from the form to a text file (or csv file) and then be able to edit the data later on, if necessary. (No, I do not have access to MySQL, hence I have to save the data to a text file.)

    Secondly, on another form I like to have a drop down menu with the keywords as the select and based on the key field seelected, I like to display about that item on that page. Before displaying them, I like to spilt the data from each field and apply some formatting as well, as shown below.

    So basically three things:
    - Save data to a text file
    - Edit data of selected item
    - Display data of selected item


    Sample data that will be entered from the form:
    FR230E|Joe Smith|Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.|Philadelphia Arizona Ohio California
    <repeated with similar data>

    Sample output page:
    ID Number: FR230E (this will be used as the key filed)
    Name: Joe Smith
    Description: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    Cities Covered:

    • Philadelphia
    • Arizona
    • Ohio
    • California

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What is the trouble you're having with this? You need to remember that we are here to help, but not do.
    My suggestion is to create an object representing this. A struct would be better, but since PHP doesn't support those an object would be just fine. Read the data in one line at a time, and populate a collection of you're objects; there are several functions dedicated to reading and can be found here. Either tokenize or split you're data on you're delimiter and store each inside their respective property. When done, save all back to the file.

    If you really want to take the dirty way out, you can look into using serialize and unserialize. These will produce a lot of overhead, but will make reading and writing a piece of cake. Another option is to use XML files which you can logically treat similar to a database (I have an XML File driver I wrote to work with my Storage factory).
    Look into some stuff, try a few things first. Come back when you're having difficulty with actual code.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    88
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you for the feedback. Yes, I understand your point. I thought it might be better for someone to suggest a good solution as opposed to my mediocre solution. However, I do have the code to save the data to a text file.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <form action="./mytest.php" method="POST" name="form1" id="form1">
    Course ID <br />
    <input type="text" name="courseID" /><br /> 
    Course Name<br />
    <input type="text" size="90" name="courseName" /><br />
    Course Vendor <br />
    <input type="text" name="courseVendor" /><br /><br />
    
    Course Type <br />
    <input type="radio" name="courseType" value="Instructor Led"/>Instructor Led
    <input type="radio" name="courseType" value="eLearning"/>eLearning<br /><br />
    
    Objective <br />
    <textarea class="textarea" rows="10" cols="90"  name="objective" /></textarea><br /> 
    Course Description<br/>
    <textarea class="textarea" rows="10" cols="90" name="description"></textarea><br />
    Key Topics Covered<br/><textarea class="textarea" rows="10" cols="90" name="keytopics"></textarea><br />
    
    <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
    </html>
    and the PHP...
    PHP Code:
    <html>
    <head>
    <?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //if ($_POST['submit']=='OK')  {
    //if (count(array_diff(array('name', 'email', 'dept', 'managerName', 'memail', 'location'), array_keys($_POST))) == 0) # POSTed
    //  {
    $courseID $_POST['courseID'] ;
    $courseName $_POST['courseName'] ;
    $courseVendor $_POST['courseVendor'] ;
    $courseType $_POST['courseType'] ;
    $objective $_POST['objective'] ;
    $description $_POST['description'] ;
    $keytopics $_POST['keytopics'] ;
    #echo $name;
    $f=fopen("./courseDetails.txt","a");
    //fwrite($f,"****************************************\r\n\r\n");
    fwrite($f,"$courseID|$courseName|$courseVendor|$courseType|$objective|$description|$keytopics\r\n");
    fclose($f);
    }
    ?>
    <title>Thank You</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    Here is some sample data:
    XML305E|Inroduction to C|Vendor A|Instructor Led|After completing this comprehensive training, you will have the necessary skills to:
    - Write C COde|This comprehensive course, on C programming. You will learn the basics.|Module 1: What is C
    Module 2: C Language Concepts
    Module 3: Arrays and Strings
    Lab 1: Write \"Hello World!\"
    Module 4: Introduction to Functions
    Lab 2: Functions and Procedures
    Module 5: Operators and Expressions
    Module 6:

    CPLUS305E|Inroduction to C|Vendor CPLUS|Instructor Led|After completing this comprehensive training, you will have the necessary skills to:
    - Write C++ COde|This comprehensive course, on C programming. You will learn the basics.|Module 1: What is C++
    Module 2: C Language Concepts
    Module 3: Arrays and Strings
    Lab 1: Write \"Hello World!\"
    Module 4: Introduction to Functions
    Lab 2: Functions and Procedures
    Module 5: Operators and Expressions
    Module 6:

    XML350E|Inroduction to XML|Vendor XML|Instructor Led|After completing this comprehensive training, you will have the necessary skills to:
    - Write XML COde & HTML|This comprehensive course, on XML programming. You will learn the basics of XML Coding.|Module 1: What is XML
    Module 2: XML Language Concepts
    Module 3: Arrays and Strings
    Lab 10: Write \"Hello World!\"
    Module 4: Introduction to Functions
    Lab 20: Functions and Procedures
    Module 5: Operators and Expressions
    - The part I need help now is on how to add an Edit button to the data
    - How to select one item based on the ID. Example, if I just want to edit the information for XML350E, how do I do that.
    - Likewise, how do I pick data based on an ID selected and display the data with some html formatting applied to the last field value. In this case, that would be the topics section, so that I can display them as an unordered list, as shown in my original message. Hope you can help.

    I heard a lot about serialize/deserialize, but without seeing any actual working code, I am having a hard time figuring out how to use that for my application here. I would be happy with an XML solution as well, if you have one that you could share.

    Thank you.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This is where C would be easy, read from a file and dump into a struct of a specified size. PHP doesn't allow this though, which is kinda a pain.
    The biggest problem is that you allow newline entries inside of you're text area, and this will be a pain when trying to determine if you've finished the current record or of you're still inside of it. An option would be to use a record delimiter that differs from the pipebars, but will always be a pain no matter what if an entry includes the delimiter and has not been replaced.
    My recommendation would be to use XML with DOMDocument if you have it available. Combine this with XPath it will make it really easy to find records:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <classes>
      <record>
        <courseCode><![CDATA[XML305E]]></courseCode>
        <courseName><![CDATA[Introduction to C]]></courseName>
        <courseVender><![CDATA[Vendor A]]></courseVender>
        <courseType><![CDATA[Instructor Led]]></courseType>
        <courseObjective><![CDATA[After completing this comprehensive training, you will have the necessary skills to:
    - Write C COde]]></courseObjective>
        <courseDescription><![CDATA[This comprehensive course, on C programming. You will learn the basics.]]></courseDescription>
        <courseKeyTopics><![CDATA[This comprehensive course, on C programming. You will learn the basics.|Module 1: What is C
    Module 2: C Language Concepts
    Module 3: Arrays and Strings
    Lab 1: Write \"Hello World!\"
    Module 4: Introduction to Functions
    Lab 2: Functions and Procedures
    Module 5: Operators and Expressions
    Module 6: ]]></courseKeyTopics>
      </record>
      <!-- ... -->
    </classes>
    Now, here comes the fun. Construct a new dom document with xpath:
    PHP Code:
    $dom = new DOMDocument('file.xml');
    $xpath = new DOMXPath($dom);
    // To find records, we write a query:
    $sQry '//record[courseCode=XML305E]';
    $records $xpath->evaluate($sQry); 
    This will go on and on, and sadly I won't have enough characters to show you how to do this. There are insertions, selections, deletions and modifications to attend to, which means the methods you'll need are DOMDocument::createElement, DOMDocument::createCDATASection, DOMDocument::removeChild, DOMXPath::evaluate, DOMNodeList::item, etc. It gets quite complex, but mimics a database quite well.

    Serialization in PHP is super easy:
    PHP Code:
    $array = array(=> 'My Element @ 1');
    $sArray serialize($array); // Will be something like: a:1{i:1(1),s:14('My Element @ 1')}
    $arrayRestuct unserialize($array); // Put it back into an array. 
    The problem with this is you need to read and write an entire array every time to the truncated file.

    Coming from a C perspective, I'd put a small header at the front of every record indicating how many bytes the entire record is. I would probably force maximum sizes on the ID and names, some of the shorter fields so they can be stored as a sized array instead of as a char *. This will allow you to fread only a certain number of bytes, and then split each of you're properties through explode or tokenizing. This will get around the record separation problem, since now you couldn't care less what its deliminated by.

    As you can see there are several ways of doing this. Personally I'm pro XML files for the simplicity of mimicking the DB. In any case, it will take a little bit of research from you're end to determine the best route. I don't consider any solution to be 'Mediocre', I consider every solution to be a step towards a specified goal. No matter what you create, you can always improve it - there is no such thing as the perfect program.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    cdn2005 (08-13-2008)

  • #5
    New Coder
    Join Date
    May 2005
    Posts
    88
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Wow, thanks for the reply. You are clearly a pro XML! Unfortunately, with my limited expertise in PHP or XML programming, I think it will be a huge mountain for me to tackle to try and do all that you have mentioned.

    I thought PHP was supposed to powerful in handling data for web based applications!

    Just to try and understand your code, i am assuming that I have to wrap the data with all those tag like <courseCode> etc before I save it to the text file.

    I have no idea how to create DOMDocument::createElement etc. I will do soe research, but from my level it would be like learning a brand new language.

    If you get some spare time and if you feel like finishing up what you started, tha would be great. If not, I will keep trying diff things.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PHP is the best route for web based languages IMO. File handling is also superb, but even I haven't done a lot of file handling with PHP in a number of years. Everything is the way of the DB now, so its kinda a setback when you've got to do file stuff (like relearning how to do it lol).
    Lets do a quick DOMDocument tutorial. I'll warn you up front, DOMDocument is a PHP5 exclusive class and can be forcibly disabled when compiled. I find its quite common in PHP5+ servers though.
    PHP Code:
    <?php
    // Create a domDocument:
    $dom = new DomDocument('1.0''UTF-8');
    $dom->formatOutput true;
    $dom->preserveWhiteSpace false;

    // Ok, lets work as if we're on a fresh file:
    $root $dom->createElement('classes');
    $dom->appendChild($root); // Place the classes element into the dom
    $record $dom->createElement('record'); // Create a record element.


    // Create you're entry fields and append them to record:
    $classID $dom->createElement('classID'$dom->createCDATASection('XML305E'));
    $record->appendChild($classID);
    $className $dom->createElement('className'$dom->createCDATASection('Introduction to C'));
    $record->appendChild($className);
    $courseVender $dom->createElement('courseVender'$dom->createCDATASection('Vendor A'));
    $record->appendChild($courseVender);
    $courseType $dom->createElement('courseType'$dom->createCDATASection('Instructor Led'));
    $record->appendChild($courseType);
    $courseObjective $dom->createElement('courseObjective'$dom->createCDATASection('After completing this comprehensive training, you will have the necessary skills to:
    - Write C COde'
    ));
    $record->appendChild($courseObjective);
    $courseDescription $dom->createElement('courseDescription'$dom->createCDATASection('This comprehensive course, on C programming. You will learn the basics.'));
    $record->appendChild($courseDescription);
    $courseKeyTopics $dom->createElement('courseKeyTopics'$dom->createCDATASection('Module 1: What is C
    Module 2: C Language Concepts
    Module 3: Arrays and Strings
    Lab 1: Write \"Hello World!\"
    Module 4: Introduction to Functions
    Lab 2: Functions and Procedures
    Module 5: Operators and Expressions
    Module 6:'
    ));
    $record->appendChild($courseKeyTopics);

    // Now, apply record to the root.  This could have been done first thing, but I like to keep it in order:
    $root->appendChild($record);

    // Thats it, lets save the data to a file:
    $dom->save('classes.xml');
    This is untested, but should provide the same results in the actual file. It looks way worse than it is, but it is really quite simple. Each item is created as an element and given the data in a cdatasection block. These newly created elements are then attached to a record (also an element), which in turn is attached to the root. Finally the file is saved.
    The DOM is huge, you can get to the api here: http://ca3.php.net/manual/en/book.dom.php
    With the correct mixtures of xpath and dom you can literally manipulate the files exactly as if it were a database (much like my driver files). When I've completed it I'll probably post it in the snippets, but its not fully tested yet and I haven't converted all of the conditional syntax (IN, CONTAINS, etc, no joins though, what a nightmare that would be >.<).
    Practice on the DOM. It will come to you in no time flat. A great place to go for the XML information, including its structure and XPath is www.w3schools.org . They are by no means complete but have a great starters. I'll give you a heads up, learn XML ASAP. XML will be leading communications between languages and I'd suspect that it will gain more and more popularity. DTD is also a requirement, and Schema if you get the time (focus on DTD first, its easier). Finally XPath and XQuery, specifically XPath - I haven't found a language yet (though there may be now) that supports XQuery, so XPath seems to be the better to learn. XQuery is far simpilier than XPath but at least you'll learn the hard stuff :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    cdn2005 (08-13-2008)

  • #7
    New Coder
    Join Date
    May 2005
    Posts
    88
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks for the reply. You must be a hard core programmer. This stuff is just flowing out without any problems.

    Your script is very straight forward and easy to follow. But there is more to it than meets the eye. I am tempted to take the XML method for my problem, but I am afraid that I am getting way over my head with this stuff.

    Anyway, I tried your code and got this error message
    Warning: DOMDocument::createElement() expects parameter 2 to be string, object given in C:\wamp\www\testSite\DOM Examples\saveFile.php on line 14
    and the error is repeated for every line containg:
    $classID = $dom->createElement('classID', $dom->createCDATASection('XML305E'));
    I am not sure if PHP 5 is supported in our system. I wonder if the warning has to do with that.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No, you're right I messed up. I tried this after and createElement cannot be given a cdatasection at the same time. Easy fix:
    PHP Code:
    $element $dom->createElement('myelem');
    $data $dom->createCDATASection('My Data for elem.');
    $element->appendChild($data); 
    CDATA needs to be appended to element. My bad, sorry about that. An alternative *probably* exists to this format. CDATASection is likely a typeof Node, meaning it has an attribute called nodeValue or data on it which returns a string result. This would let you combine the two. I'd need to reflect the cdata result to see what it is exactly to confirm this though.

    PHP5 and DOMDocument are both supported on you're system from the system error. It is warning you that it expects different datatypes than what has been provided, and thats ok. If you didn't support it you're error would either be referring to no class definition for DOMDocument or an undefined method on object of unknown type DOMDocument.

    I'm almost done my file driver for this as well, just working out a few bugs and optimizing it. Haven't tried it with 1000 record yet, but with 10 its about 8 times faster than SQL access.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    cdn2005 (08-14-2008)

  • #9
    New Coder
    Join Date
    May 2005
    Posts
    88
    Thanks
    12
    Thanked 0 Times in 0 Posts
    well that helped to move forward.
    Now there is no warning, no erros, but the data didn't get saved in to the classes.xml file.

    All I have in that file is
    <?xml version="1.0" encoding="UTF-8"?>
    <classes>
    <record/>
    </classes>
    I changed everything in my code to...
    PHP Code:
    $element $dom->createElement('classID');
    $data $dom->createCDATASection('XML305E');
    $element->appendChild($data); 
    $element $dom->createElement('className');
    $data $dom->createCDATASection('Introduction to C');
    $element->appendChild($data); 
    and repeated for all elements...

    I thought the script would save the data at the end!

    Your file driver, is it something that you can share, either privately or through the forum?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You're record element is empty.
    Since you are re-using you're $element variable, you need to make sure you're applying the data at the end of every call:
    PHP Code:
    // Not sure what you called it, but we'll call it $record:
    $record $dom->createElement('record');
    $element $dom->createElement('classID');
    $data $dom->createCDATASection('XML305E');
    $element->appendChild($data); 
    // This is what I'm referring to, at this level $element has been reassigned, so we
    // will put this into the $record element right away:
    $record->appendChild($element);

    $element $dom->createElement('className');
    $data $dom->createCDATASection('Introduction to C');
    $element->appendChild($data);  
    $record->appendChild($element); 
    Each of you're elements should be appended to the $record. Don't forget to ensure that $dom->save('yourefile.xml'); is done after all of you're changes are performed. My money is in that you have that at the bottom, but the actual $record doesn't have any data in it. If you've used the variable $element for everything, it will be a quick copy and paste
    Looks like you're coming along pretty well. DOM is so intensive, I've used it for about 2 years now and I still have so many troubles with it >.<. It does get a lot easier with practice though.

    The file driver is almost ready, I went back to work on it the other day. Since its part of a storage factory it requires specific functions to be implemented to work properly (storage factories let you use many different types of storages including files and databases without having to change the code making use of them). I'll likely be posting it by the weekend, I just need to add two more conditions for it to be usable. I'm leaving off schema structures and relationship cascades until version 2. Version 1 will just be the select, insert, update, and deletion functions.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    New Coder
    Join Date
    May 2005
    Posts
    88
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Wow... that part is working now. That feels good, but equally scared because I know now I got the taste of something good and I can' turn back, but I am getting in to deeper waters ...

    I now tried to modify the script so that it will take the data from my original html form. So I added the following lines, but it is not happy.

    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //if ($_POST['submit']=='OK')  {
    //  {
    $courseID $_POST["courseID"] ;
    $courseName $_POST["courseName"] ;
    $courseVendor $_POST["courseVendor"] ;
    $courseType $_POST["courseType"] ;
    $objective $_POST["courseObjective"] ;
    $description $_POST["courseDescription"] ;
    $keytopics $_POST["courseKeyTopics"] ;


    and then the script was changed to :
    PHP Code:
    $element $dom->createElement('courseID');
    $data $dom->createCDATASection('$courseID');
    $element->appendChild($data); 
    $record->appendChild($element);

    $element $dom->createElement('courseName');
    $data $dom->createCDATASection('$courseName');
    $element->appendChild($data);
    $record->appendChild($element); 
    and so on...

    I am sure I should be using a loop for this, but that is something for later... Anyways, this didn't do anything. (I changed the form field names in both form and here)

    Secondly I also came across a tutorial: http://www.phpfreaks.com/tutorial/ha...xml-data/page2

    In that example, the data is stored as
    PHP Code:
    <?xml version="1.0"?>
    <books>
      <book isbn="978-1594489501">
        <title>A Thousand Splendid Suns</title>
        <author>Khaled Hosseini</author>
        <publisher>Riverhead Hardcover</publisher>
        <amazon_price>14.27</amazon_price>
      </book>
      <book isbn="978-1594489587">
        <title>The Brief Wondrous Life of Oscar Wao</title>
        <author>Junot Diaz</author>
        <publisher>Riverhead Hardcover</publisher>
        <amazon_price>14.97</amazon_price>
      </book>
      <book isbn="978-0545010221">
        <title>Harry Potter and the Deathly Hallows</title>
        <author>J. K. Rowling</author>
        <publisher>Arthur A. Levine Books</publisher>
        <amazon_price>19.24</amazon_price>
      </book>
    </books>
    In your example, yo uare storing data as:
    PHP Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <classes>
      <record>
        <classID><![CDATA[XML305E]]></classID>
        <className><![CDATA[Introduction to C]]></className>
        <courseVendor><![CDATA[Vendor A]]></courseVendor>
        <courseType><![CDATA[Instructor Led]]></courseType>
        <courseObjective><![CDATA[After completing this comprehensive training, you 
    will have the necessary skills to: - Write C COde]]></courseObjective>
        <courseDescription><![CDATA[This comprehensive course, on C programming. You
     will learn the basics.]]></courseDescription>
        <courseKeyTopics><![CDATA[Module 1: What is C
    Module 2: C Language Concepts
    Module 3: Arrays and Strings
    Lab 1: Write \"Hello World!\"
    Module 4: Introduction to Functions
    Lab 2: Functions and Procedures
    Module 5: Operators and Expressions
    Module 6:]]></courseKeyTopics>
      </record>
    </classes>
    What is the difference? Which method is better? The <classes> in your example, is that a keyword or could I have used <courses> instead?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Lots of questions in one
    Classes is not reserved. In XML, the only tag you cannot use is <xml> unless its a namespace style tag. Any other tag name that matches criteria (I'm taking a stab at it based on language variables) must start with a string, can contain numbers, chars, and underscores, but no other special characters. XML is so flexible since its a data descriptive language. You can name you're tags whatever you want. You're root can be called <myUberRoot> and it won't matter.
    CDATA blocks are not necessary. Without them is more optimized. However, since I plan on using mine to store HTML and code data, CDATA will allow me to get around storing special character representations for this code. In other words, I'm too lazy to store replaced data and then reparse it on load. CDATA is character data, while no cdata represents parsable character data. So, if I have this:
    Code:
    <?xml version="1.0"?>
    <root>
        <code><html><head><title>Page Title</title></head><body></body></html></code>
    </root>
    The tags within the <code> block will be treated as a part of the XML document. Using <![CDATA[<html><head><title>Page Title</title></head><body></body></html>]]> tells the XML file this is a string, and not to parse the interior code as xml.

    As for you're actual insertion data, the problem is with these:
    $data = $dom->createCDATASection('$courseID');
    Wraping a variable with single quotations in PHP treats it as a non-parsable string. So '$courseID' literally means $courseID. What you want is one of these:
    $data = $dom->createCDATASection($courseID); or $data = $dom->createCDATASection("$courseID");, with emphasis on the first since it is faster to process.

    Don't worry about turning back. Even if you never use the DOM class again, what you are learning is PHP in an Object Oriented or OO style usage. This is important since PHP is shifting from procedural to Object Oriented and its important to learn the concepts early. All objects function in a similar way to how you have it here. When I've got the driver uploaded, it will be a lot easier, something more like this:
    PHP Code:
    $storage Storage::getInstance('XML''./storageDir');
    $aValues = array(
        
    'courseID' => $courseID,
        
    'courseName' => $courseName,
        
    'courseVender' => $courseVender,
        
    'courseType' => $courseType,
        
    'courseObjective' => $objective,
        
    'courseDescription' => $description,
        
    'courseKeyTopics' => $keyTopics,
    );
    $storage->insert($aValues'classes'); 
    Retrieval, deletion and alterations will be just as similar. The purpose is to closely relate to a database for simplicity. This will allow me to create objects which do not care about storage engine and still handle all the necessary functionality.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #13
    New Coder
    Join Date
    May 2005
    Posts
    88
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you for the reply. Sorry for the too many qustions.
    I hope you won't give up on me now.

    Okay now changed the code as follows:
    PHP Code:
    $storage Storage::getInstance('XML''./storageDir');
    $aValues = array(
        
    'courseID' => $courseID,
        
    'courseName' => $courseName,
        
    'courseVender' => $courseVender,
        
    'courseType' => $courseType,
        
    'courseObjective' => $objective,
        
    'courseDescription' => $description,
        
    'courseKeyTopics' => $keyTopics,
    );
    $storage->insert($aValues'classes'); 

    $element $dom->createElement('courseID');
    $data $dom->createCDATASection($courseID);
    $element->appendChild($data); 
    $record->appendChild($element);

    $element $dom->createElement('courseName');
    $data $dom->createCDATASection($courseName);
    $element->appendChild($data);
    $record->appendChild($element); 
    And I get the error
    Fatal error: Class 'Storage' not found in /www/hsweb/training/DOM_Examples/saveFile.php on line 11
    I feel like walking blind folded now... stepping in to the unknown... one step at a time...

    How does it know that it has to capture the $_POST data?

    And is ./StorageDir an directory that I have to create?

    I added this line, but didn't help:
    PHP Code:
    $storage $dom->createElement('storage'); 
    Please remember that I do not have a lot of exposure to XML programming, so if you could add more comments to your code, it would be extremely helpful at this stage. I really appreciate your help and patience. This would be a real joy to see this code working. Wow... an XML code!

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That code is how my driver works - this code:
    PHP Code:
    $storage Storage::getInstance('XML''./storageDir');
    $aValues = array(
        
    'courseID' => $courseID,
        
    'courseName' => $courseName,
        
    'courseVender' => $courseVender,
        
    'courseType' => $courseType,
        
    'courseObjective' => $objective,
        
    'courseDescription' => $description,
        
    'courseKeyTopics' => $keyTopics,
    );
    $storage->insert($aValues'classes'); 
    Will produce the exact same results as what you are doing right now. Storage does not exist as a built in PHP class, so this will have to wait for a driver. It was a demonstration to show the simplification to what you're producing right now. I'm struggling with a LIKE syntax on it, so I'll need to check in with the XML forum after to make a posting about that. It was simply to show a comparison, what you will be able to do with a driver versus actually writing the code out. They function identically, but the driver is generic so it handles whatever data you throw at it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #15
    New Coder
    Join Date
    May 2005
    Posts
    88
    Thanks
    12
    Thanked 0 Times in 0 Posts
    So I suppose what you are saying is that I could replace all of the entries like

    PHP Code:
    $element $dom->createElement('courseName');
    $data $dom->createCDATASection($courseName);
    $element->appendChild($data);
    $record->appendChild($element);

    $element $dom->createElement('courseVendor');
    $data $dom->createCDATASection($courseVendor);
    $element->appendChild($data); 
    $record->appendChild($element); 
    with something simple like this, if we had a storage class available.

    PHP Code:
    $storage Storage::getInstance('XML''./storageDir');
    $aValues = array(
        
    'courseID' => $courseID,
        
    'courseName' => $courseName,
        
    'courseVender' => $courseVender,
        
    'courseType' => $courseType,
        
    'courseObjective' => $objective,
        
    'courseDescription' => $description,
        
    'courseKeyTopics' => $keyTopics,
    );
    $storage->insert($aValues'classes'); 
    So i suppose there was no point in me struggling to get ehat code working, since I was already missing the storage class. Sorry, I didn't realize that it was class... my mistake!

    I tried adding this line, but didn't help:
    PHP Code:
    $root->appendChild($storage);
    $dom->save('classes.xml'
    So I will wait for your driver to be ready. can I assume that you would share the driver with me as well.


  •  
    Page 1 of 6 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •