Results 1 to 3 of 3

Thread: [RESOLVED] PHP, XML. Can't get to modify tags the way I need to

  1. #1
    Join Date
    Dec 2011

    resolved [RESOLVED] PHP, XML. Can't get to modify tags the way I need to

    I got an xml file being echoed by php and all the code works fine but my problem is that inside a tag (exam and the attribute is chapter which is what's being echoed) there's more than one tag (ex, that's what is being echoed as exam #) and I am echoing the data to a table and I want it to display it like this:

    | Chapter 1 | Chapter 2 |

    |--Exam 1--|--Exam 1--|

    |--Exam 2--|--Exam 2--|

    | Chapter 3 | Chapter 4 |

    |--Exam 1--|--Exam 1--|
    But What I am getting is something like this: (I know why though)

    | Chapter 1 |

    |--Exam 1--|

    | Chapter 1 |

    |--Exam 2--|

    | Chapter 2 |

    |--Exam 1--|
    It keeps on repeating the tag's attribute and I know why, because I am echoing the tag's attribute that I have as a variable in a table data. So the question is not why it is happening but how can I change it to display it the way I want it.

    I guess maybe there's a way to delete the attribute if there's more than one (I know you can delete it but I don't know how to show it once and delete the others)

    The code that I have is something like this:

    In XML

      <exam chapter="Chapter 1">
        <ex id="1">Exam 1</ex>
        <ex id="2">Exam 2</ex>
        <ex id="3">Exam 3</ex>
      <exam chapter="Chapter 2">
        <ex id="4">Exam 1</ex>
        <ex id="5">Exam 2</ex>
      <exam chapter="Chapter 3">
        <ex id="6">Exam 1</ex>
      <exam chapter="Chapter 4">
        <ex id="7">Exam 1</ex>
        <ex id="8">Exam 2</ex>
        <ex id="9">Exam 3</ex>
        <ex id="10">Exam 4</ex>
    The PHP

    PHP Code:
    $xpath = new DOMXPath($xml);
    $exams $xpath->query('//exam/ex');

    $istyle 1//This is to add gray after every other row

    echo "      <table cellpadding=\"0\" cellspacing=\"3\" border=\"0\" width=\"60%\" align=\"center\">
                <tr id=\"center\" bgcolor=\"#999\">
                    <td>Student's Exam Status</td>

    $exams as $exams2) {
    $chapter $exams2->parentNode->getAttribute('chapter');
    $examid $exams2->getAttribute('id');
    $examname $exams2->nodeValue;

        if (
    $examid 5) { //where it says 5 there goes a php variable where it has the queried user's exam number from the database, again this is all finished no need to change this.
    $exstatus "You already took this exam.";
        }elseif (
    $examid == 5) {
    $exstatus "This is your exam (exam link)";
        }elseif (
    $examid 5) {
    $exstatus "You are not yet on this exam";

    "<tr id=\"center\"";

            if (
    $istyle == 0)
    " bgcolor=\"#ccc\"";



    Notice that the table structure is different than the way I said I want it and I was getting it above, I just changed it because I couldn't leave it the way it was.

    Note that what I want to change is where it says chapter, I want it to display it once and below it to display exam 1 and below that exam2 ect. and next to the chapter put the next chapter (in this case chapter 2) and below that exam 1 and below that exam 2 ect. and after that create another table row below exam 2 and put two other chapters and below that the other exams.

    Notice that the exams do not follow a pattern and this is an edited version of the file since there's hundreds of those and the values are different of what you see above.

    The code above works, I just want to modify it so it could meet my requirements. Also the variable names are for demo only, I made this file to test how I want my code to run, this wasn't pulled from the original file so all the var and other names and things are for demo only, so you guys could have an idea. No need to tell me that the vars don't make sense or confuses.

  2. #2
    Join Date
    Dec 2011


    I've figured it out, it took me hours but I finally did it.

    This is the code I used for the PHP, the XML stayed the same. I'll try to be as detailed as possible so it could help anyone with a similar problem or so they could extract info from it and modify it to their needs.

    PHP Code:
    //self explanatory
    $xml DOMDocument::load('needed/examdata.xml');
    //again, self explanatory
    $xpath = new DOMXPath($xml);
    //looks for ex under exam in the xml I loaded above xml
    $exams $xpath->query('//exam/ex');
    //I just put a number for testing purposes but it actually gets the user's exam id from the database
    $studentexnum "5";
    //column counter, will be used to tell that after every 2 chapters, break the table data (td) and table row (tr) and create a new one
    $_2chapters 1;
    //Opens the table and displays it to the client
    echo "<table cellpadding=\"5\" cellspacing=\"5\" border=\"0\" width=\"25%\" align=\"center\">
    //Opens the first table row and end the echoing

    //starts the loop, for every exam, makes exam2 same as exam
    foreach($exams as $exams2) {
    //looks at my xml file for the tag ex (what I defined above) and gets the attribute called chapter from his parent (the tag above it)
    $chapter $exams2->parentNode->getAttribute('chapter');
    //gets the attribute called id from the tag ex
    $examid $exams2->getAttribute('id');
    //makes the tag ex a string so we could display it
    $examname $exams2->nodeValue;

    ////////////////////////////////////////Now for the Fun Part/////////////////////////////////////////////////////////////////////////////////

    //conditional statement saying that if the variable chapter2 is set, display the conditions below, if it's not set (which is not when its starts the loop) do something else
    if (isset($chapter2)){
    //says if variable chapter defined above is equal to variable chapter 2 which is defined below do something. This is not true the first time but it is the rest of the loop, even is is a million.
    if ($chapter == $chapter2) {
    //variable chaptertd (which is called below) will equal to nothing if chapter equals to chapter2, this will happen at every exam (every ex tag in my xml file which is in the question above)
    //this will avoid having repeated chapter, ie: chapter1 - exam one, chapter1 - exam 2, chapter1 exam 3, ect. will make it chapter 1, exam 1, exam 2, exam 3, ect
    $chaptertd "";
    //if chapter does not equal to chapter2
    }else {
    //here we increment variable _2chapters that was 1 above, now is two, this is necessary so it could display two chapters side by side and break right before the third chapter, this will happen later
    //Now we give a value to variable chaptertd, it was nothing before because I didn't want to repeat the title every time the loop found a new tag ex from my xml. This will only happen once in every chapter
    $chaptertd "
            <td align=\"center\" valign=\"top\">
    $chapter2";//here we create the html that variable chaptertd will be displaying after a new name from the attribute chapter is found. This will display the name of the chapter to our table
    //this else will only happen the first time the loop runs since only the first time is when the variable chapter2 is not defined, after this runs the variable chapter2 will have been defined
    }else {
    //chapter2 will be the same as chapter, if chapter equals to the string chapter1 so will chapter2.
    $chapter2 $chapter;
    //here we create the same td as above since we want to display the name of the chapter the fist time it runs, if we don't do this the first chapter won't be display to the client
    $chaptertd "
            <td align=\"center\" valign=\"top\">
    //This part you don't have to concern yourself with it, I made this because I needed it to display different info depending whether the user was allow to see that exam.
    //the variable examval is defined outside this code, that's on my html code which would do nothing here since it uses javascript and css. this gets the attribute id from our ex tag.
    if ($examid $studentexnum) {
    $exval "lessthan";
        }elseif (
    $examid == 5) {
    $exval "equalto";
        }elseif (
    $examid 5) {
    $exval "greaterthan";

    //here we say what happens when the variable _2chapters reaches the third attribute called chapter. we say if the remainder of variable _2chapters divided by 3 equals 0 do something
    //else do nothing since we didn't define the else because we didn't needed it. this part will only happen at every third chapter, it will break from the previous chapter thus making
    //it a new row right under the values of the tag ex which is under chapter 1 the third time the loops runs, but this will happen infinite amounts of time, after that it will be under
    //chapter 3 and after that chapter 5 and so on.
    if ($_2chapters == 0) {
    //here we reset the count back to one because if we don't and there's more than one tag ex under chapter 3, it will be braking after every ex
    $_2chapters 1;
    //here we echo the break from the previous chapter
    echo "
        <tr id=\"center\">"
    //here we echo the variable chaptertd which we gave different values to above depending whether the chapter's name has been declared already or not. If it has been declared, chaptertd
    //won't show anything, if the chapter has never been declared it will create a new td and display the chapter's name and after that it will become nothing again
    echo "$chaptertd<br />
                <a href=\"#\" class=\"modalInput\" rel=\"#
    $exval\">$examname</a>";//here we show the client what's the name of the exams under the given chapter, there could be one or one hundred of this and it will only display the chapter once and will display the rest
    //of the exam names one after the other

    //here we say that chapter2 equals to chapter, this way chapter2 will equal to the name of that loop, if the next time it repeats there's a new chapter the value of chapter will change
    //thus making this statement false above and that will force it to create a new td with the new name until another name appears.
    $chapter2 $chapter;

    //close the table to have a well formatted html file to display to the client.
    echo "

  3. #3
    Join Date
    Dec 2011
    I forgot something I had before but I deleted it thinking it was unnecessary but what it was doing was that it was displaying the first attribute "chapter" twice and the rest one behind. to fix that I put this: $chapter2 = $chapter; here:

    PHP Code:
    so now it reads like this:

    PHP Code:
    $chapter2 $chapter;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09361 seconds
  • Memory Usage 2,928KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (4)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates