www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Comparing XML attributes with PHP

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    39

    resolved [RESOLVED] Comparing XML attributes with PHP

    Hello everyone. I have loaded 2 XML files which have children elements with attributes. Both work correctly, they are tested. The problem is that i am trying to compare an attribute from the first XML with one from the other and it fails even though both files have the same values in a given attribute. More specifically I am loading <option> tags for a <select> tag from this:

    HTML Code:
    <schoolcats>
    		<category id="1">Cat 1</category>
    		<category id="2">Cat 2</category>
    		<category id="3">Cat 3</category>
    		<category id="4">Cat 4</category>
    		<category id="5">Cat 5</category>
    	</schoolcats>
    Categories and their id's load normally in my app. Problem is when I try to make one selected. I try:

    PHP Code:
    <select name="schoolcat">
          <?php

          
    foreach ($schoolcats as $schoolcat)  { 
          
    ?>
          <option value="<?=$schoolcat['id']?>"
          <?php $test $schoolcat['id'];
          if (
    $test == $this_schoolcat) { echo ' selected="selected"'; } ?>><?=$schoolcat?></option>
          <?php ?>
          </select>
    Every variable is tested and correct. Why won't the "if" give me a true when the loop finds the correct option though???

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    Where is $this_schoolcat set? Are you sure it has the expected value when you use it in the comparison?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2010
    Posts
    39
    Hi NogDog, thanks for the prompt reply

    In the beginning of the file I have put:

    PHP Code:
    foreach ($institutions as $institution)  {
        if (
    $institution['id']==$id) {
        
    $current_uni $institution;
        }
        }
        
    $this_schoolcat $current_uni['schoolcat']; 
    Now $current_uni is definately correct, since i have made tons of $current_uni['whatever_i_want'] calls in this file in other places. It is actually a form that gets an id, searches the xml file of universities and fills the form with its data for editing. On the point where I have the problem, I just can't get the university category as selected on the list for the university I've called in the form

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    All I can suggest at this point is to add some debug code within that foreach loop, echoing or var_dump()-ing he variables involved and seeing which ones (if any) are not set as expected. As far as I can see the logic looks OK, so my guess is something does not have the value you think it should. Locating that will probably point you to the source of the problem.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jan 2010
    Posts
    39
    Hmmmm You know what? I think it just might be blank characters (which do not exist in the actual XML but maybe the parsing functions do something fishy). How do I strip spaces from the left and right of a string?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    trim()
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jan 2010
    Posts
    39
    Initially did not work but maybe I made a mistake since I am dead tired :P

    Thanks, I think it was the trimming
    Last edited by erevos; 01-17-2010 at 01:02 PM. Reason: it worked!

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



Recent Articles