Results 1 to 7 of 7

Thread: Newbie Seeks some PHP/XML understanding

  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newbie Seeks some PHP/XML understanding

    All,

    I am new to PHP and not good by any standard, and even worse when it comes to XML. From all the readings regarding XML parsers, I have not seen an explanation of how to target a single entry in a XML list.

    For example,
    I have built a PHP calendar and would now like to read an EVENT.xml table to populate the dates that actually have an event defined in the table.

    EVENT.xml

    <calendar>
    -<year>
    --<month>
    ---<day>
    ----<event>
    -----<short_desc></short_desc>
    -----<long_desc></long_desc>
    -----<link></link>
    ----</event
    ---</day>
    --</month>
    -</year>
    </calendar>



    Now, in the calendar.php script, I have a while loop that builds the days for the calendar. What I have not seen on ANY of the many XLM forums I've been to while trying to figure is how do I grab the short_desc value of an event when the variable matches the day that the calendar script is currently building?

    All the examples I've seen have been to output the entire file. What I need to do is when the calendar script builds the line for the 15th day, of the 3rd month, of year 2009, is to ensure I get just that days values for the <EVENT> elements. How do I specify a specific year/month/day? Is it quicker to load the entire year's worth of data, or load only the month being displayed?

    Your help is appreciated.

    MK.

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I don't know what the rest of your XML file looks like, but based on what is below, you can pull the long-desc, etc out like this. If this isn't what you needed, let me know.

    I guess I would make a loop of all the month elements, and loop through those, outputing information for each day.

    Code:
    <?xml version='1.0' ?>
    <calendar>
    	<year>
    		<month>
    			<day>
    				<event>
    				<short_desc>short desc</short_desc>
    				<long_desc>long desc</long_desc>
    				<link>link</link>
    				</event>
    			</day>
    		</month>
    	</year>
    </calendar>
    PHP Code:
    <?php

    $xml 
    simplexml_load_file('event.xml');

    $event $xml->year->month->day->event;

    echo 
    $event->long_desc '<br />';
    echo 
    $event->short_desc '<br />';
    echo 
    $event->link '<br />';

    ?>

  3. #3
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Jason, and thanks for your reply.

    As my Calendar.php script only displays 1 month at a time, my question is really, "How do I ensure that given an XML file that has events for a full year, divided into 12 months, with a possible 365 days full of events, when the calendar.php script runs for the month of March, how do I specifically specify the short_desc, long_desc, and any other object of the item event to make sure that the event details for March 15th, 2009 appear in the day 15 box of the calendar?"

    Forgive my XML ignorance, but the code you suggested looks like it would output the details for every single event in the XML file, instead of specifically pulling only the relevent event data for the day the loop is currently building.

    What I envisioned XML having is some way of saying to set the <year> element = 2009, the <month> element = 3, the <day> element = 15. As I stated...I do not know XML coding, so I don't know what I'm typing here, but something like:

    xml_parse_into_struct($parser,$xml,$values,$index);
    xml_parser_free($parser);
    $short_desc = $values[$index[short_desc][$day]][value];

    The problem with the $short_desc line of the code is that there could be only a single <event> listed in the XML file for March 15th, or there could be 365 events. I'd like the ability to be able to specify by year, month, day, event# to list the appropriate details. Otherwise, as I see it, I'd have to break the large YEARs worth XML file into 12 smaller XML files and read the data as each month is built.


    TIA,

    MK

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Are you building the XML file yourself or is it output by some software you have?

    Post an example of an XML document you would use for this.

    Perhaps the structure of your XML is causing the problem.

    My first instinct is that something like this would be better for you:

    Code:
    <calendar>
    
      <event>
        <year>2009</year>
        <month>March</month>
        <day>15</day>
        <time>14:00</time>
        <long>This is a long description</long>
        <short>Short</short>
        <link>LINK</link>
      </event>
    
      <event>
        ...
      </event>
    
    </calendar>

  5. #5
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Here are the first draft events from Jan and Feb.

    ------------------------------------------


    <?xml version="1.0" encoding="ISO-8859-1" ?>
    - <calendar ver="2.0">
    - <yar yeartag="2009">
    - <mn m="1">
    - <dy d="1">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="2">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="3">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="4">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="5">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="6">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="7">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="8">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="9">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="10">
    <shorttext>Game Night</shorttext>
    <link />
    <longtext>Game Night</longtext>
    </dy>
    - <dy d="11">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="12">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="13">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="14">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="15">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="16">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="17">
    <shorttext>Movie Night</shorttext>
    <link />
    <longtext>Movie Night</longtext>
    </dy>
    - <dy d="18">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="19">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="20">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="21">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="22">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="23">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="24">
    <shorttext>Dance: Winter Ball</shorttext>
    <link />
    <longtext>Dance: Winter Ball</longtext>
    </dy>
    - <dy d="25">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="26">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="27">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="28">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="29">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="30">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="31">
    <shorttext />
    <link />
    <longtext />
    </dy>
    </mn>
    - <mn m="2">
    - <dy d="1">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="2">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="3">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="4">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="5">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="6">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="7">
    <shorttext>Games Fund Raiser</shorttext>
    <link />
    <longtext>Games Fund Raiser</longtext>
    </dy>
    - <dy d="8">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="9">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="10">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="11">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="12">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="13">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="14">
    <shorttext>Valentines Day Dance</shorttext>
    <link />
    <longtext>Valentines Day Dance: Can - Can theme</longtext>
    </dy>
    - <dy d="15">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="16">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="17">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="18">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="19">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="20">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="21">
    <shorttext>Movie Night</shorttext>
    <link />
    <longtext>Movie Night</longtext>
    </dy>
    - <dy d="22">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="23">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="24">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="25">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="26">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="27">
    <shorttext />
    <link />
    <longtext />
    </dy>
    - <dy d="28">
    <shorttext>WII tournament and Open Dancing</shorttext>
    <link />
    <longtext>WII tournament and Open Dancing</longtext>
    </dy>
    </mn>

  6. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I think this is a step in the right direction. I'm learning a lot about this by trying to work this out.

    It would be nice if we could just pick out the month by using it's attribute instead of looping through every month. I haven't figured out how to do that yet. Hopefully someone who is good with simpleXML will help us out.

    event.xml

    Code:
    <?xml version="1.0" encoding="ISO88591" ?>
    <calendar ver="2.0">
    	<yar yeartag="2009">
    		<mn m="1">
    			<dy d="1">
    				<shorttext>Day 1 Short</shorttext>
    				<link>Day 1 LINK</link>
    				<longtext>Day 1 Long</longtext>
    			</dy>
    			<dy d="2">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="3">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="4">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="5">
    				<shorttext>Dancing and Wii Tourney</shorttext>
    				<link>Click here for Dancing</link>
    				<longtext>Long Wii text and Dancing</longtext>
    			</dy>
    			<dy d="6">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="7">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    		</mn>
    		<mn m="2">
    			<dy d="1">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="2">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="3">
    				<shorttext>Month 2 day 3 short</shorttext>
    				<link>LINK LINK</link>
    				<longtext>Long text for day 2/3</longtext>
    			</dy>
    			<dy d="4">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="5">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="6">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="7">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    		</mn>
    		<mn m="3">
    			<dy d="1">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="2">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="3">
    				<shorttext>Month 3 day 3 short</shorttext>
    				<link>LINK LINK</link>
    				<longtext>Long text for day 3/3</longtext>
    			</dy>
    			<dy d="4">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="5">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="6">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="7">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    		</mn>
    		<mn m="4">
    			<dy d="1">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="2">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="3">
    				<shorttext>Month 4 day 3 short</shorttext>
    				<link>LINK LINK</link>
    				<longtext>Long text for day 4/3</longtext>
    			</dy>
    			<dy d="4">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="5">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="6">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    			<dy d="7">
    				<shorttext />
    				<link />
    				<longtext />
    			</dy>
    		</mn>
    	</yar>
    </calendar>
    Your php file :

    PHP Code:
    <?php

    $xml 
    simplexml_load_file('event.xml');

    $currentYear date('Y');
    $currentMonth date('n');

    $years $xml->yar;

    foreach ( 
    $years as $year ) {
        
        if ( 
    $year->attributes()->yeartag == $currentYear ) {
            
            
    $months $year->mn;
            
            foreach ( 
    $months as $month ) {
                                    
                if ( 
    $month->attributes()->== $currentMonth ) {

                    
    $days $month->children(); // All the days

                    
    foreach ( $days as $day ) {
                        
                        
    // I guess the calendar would get output here:
                        
    echo $month->attributes()->'/' $day->attributes()->'<br />';
                        echo !empty( 
    $day->shorttext ) ? $day->shorttext '<br />' '';
                        echo !empty( 
    $day->link ) ? $day->link '<br />' '';
                        echo !empty( 
    $day->longtext ) ? $day->longtext '<br />' '';

                    }

                }

            }
            
        }
        
    }
    ?>
    I really am not happy with all the nested foreach loops. Being able to select elements based on their attributes would be great. I'll keep trying to figure that out.

  7. #7
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Better:

    PHP Code:
    <?php

    $xml 
    simplexml_load_file('event.xml');

    $currentYear date('Y');
    $currentMonth date('n');

    $years $xml->yar;

    foreach ( 
    $years as $year ) {
        
        if ( 
    $year->attributes()->yeartag == $currentYear ) {
            
            
    $months $year->mn;
            
        }
        
    }

    foreach ( 
    $months as $month ) {
        
        if ( 
    $month->attributes()->== $currentMonth ) {
            
            
    $days $month->dy;
            
        }
        
    }

    foreach ( 
    $days as $day ) {

        
    // I guess the calendar would get output here:
        
    echo $currentMonth '/' $day->attributes()->'<br />';
        echo !empty( 
    $day->shorttext ) ? $day->shorttext '<br />' '';
        echo !empty( 
    $day->link ) ? $day->link '<br />' '';
        echo !empty( 
    $day->longtext ) ? $day->longtext '<br />' '';
        
    }

Bookmarks

Posting Permissions

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