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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Help with foreach?

    I am creating a calendar that displays database information from an xml sheet. I need to enter "</tr><tr>" at every 7th time the foreach statement echos the information.
    Here's my XML:
    june.xml
    PHP Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <month>
        <DAY>
            <DATE>[null]</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>[null]</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>1</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>2</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>3</DATE>
            <LOCATION>Roseville</LOCATION>
            <TIME>9:30am - 4:30pm</TIME>
            <TITLE>Sharks Teeth Pillowcase</TITLE>
            <NUMBER>01</NUMBER>
        </DAY>
        <DAY>
            <DATE>4</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>5</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>6</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>7</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>8</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>9</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>10</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>11</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
        <DAY>
            <DATE>12</DATE>
            <LOCATION>[null]</LOCATION>
            <TIME>[null]</TIME>
            <TITLE>[null]</TITLE>
            <NUMBER>[null]</NUMBER>
        </DAY>
    </month>
    And here's my PHP:
    index.php
    PHP Code:
    <?php
    if(!$xml=simplexml_load_file('june.xml')){
        
    trigger_error('Error reading XML file',E_USER_ERROR);
    }

    ?>
    <!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>
    <title>Marilyn's Sewing - June</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <style type="text/css">
    body{font: 85% Calibri,arial,sans-serif;}
    p{margin:0 10px 10px}
    a
    {    text-decoration:none;
        color:#33CC00; 
        margin:0;
        font-family:Calibri, Arial, Helvetica, sans-serif;}
        
    a:hover
    {    text-decoration:none;
        color:#006633;
        margin:0;
        font-family:Calibri, Arial, Helvetica, sans-serif;}
    div#header{margin-bottom:25px;}
    div#container{text-align:left;}
    div#content p{line-height:1.4}
    div#content form{font: 85% calibri,arial,sans-serif}
    div#navigation{margin-top:35px}
    div#navigation ul{margin:15px 0; padding:0; list-style-type:none;}
    div#navigation a:hover{text-decoration:underline; padding-left:2px}
    div#navigation li{margin-bottom:5px; margin-left:10px}
    div#extra{}
    div#footer{}
    div#footer p{margin:0;padding:5px 10px}
    div#container{width:700px;margin:0 auto}
    div#content{float:left;width:500px}
    div#navigation{float:right;width:200px}
    div#extra{float:right;clear:right;width:200px}
    div#footer{clear:both;width:100%}
    img{border:none}
    a img{border:none}
    table{width:800px; border:solid #D889B8;font:Verdana, Arial, Helvetica, sans-serif;}
    th{color:#FFFFFF; background:#D889B8; width:14%;}
    td{text-align:left; width:14%; border-bottom:solid thin #D889B8; border-left:solid thin #D889B8;vertical-align:top;}
    #date{font-weight:bold; margin:0;}
    </style>
    </head>
    <body>
    <div style="text-align:left"><a href="#">January</a> | <a href="#">February</a> | <a href="#">March</a> | <a href="#">April</a> | <a href="#">May</a> | <a href="#">June</a> | <a href="#">July</a> </div>
    <table id="table" cellpadding="5">
    <tr>
        <th>Sun</th>
        <th>Mon</th>
        <th>Tue</th>
        <th>Wed</th>
        <th>thu</th>
        <th>Fri</th>
        <th>Sat</th>
    </tr>
    <tr>
    <?php
    foreach($xml as $day){
        echo 
    str_replace("[null]" " " '<td><p>'.$day->DATE.'</p>'.$day->LOCATION.'<br />'.$day->TIME.'<br /><a href="#">'.$day->TITLE.'</a>');
    }
    ?>
    </tr>

    </table>
    </body>
    </html>
    There are 14 days, and I need a line break in the table every 7 days, like a calendar should. What can I do?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    I see one problem with your idea ...
    What if your XML file starts on a Wednesday?
    I think you need to do some calculations on Sun-Sat.

    My idea below will still have a problem with an extra <tr> at the
    end of the list ... but it might give you an idea of where to start ...

    I still think you will have to deal with starting in the middle of a week.

    Replace this:

    <tr>
    <?php
    foreach($xml as $day){
    echo str_replace("[null]" , " " , '<td><p>'.$day->DATE.'</p>'.$day->LOCATION.'<br />'.$day->TIME.'<br /><a href="#">'.$day->TITLE.'</a>');
    }
    ?>
    </tr>


    With:
    PHP Code:
    <?php 
    $c
    =0;
    foreach(
    $xml as $day){
    if(
    $c==6){
    echo
    "</tr>";
    $c=0;
    }
    if(
    $c==|| $c==6){
    echo
    "<tr>";
    }
        echo 
    str_replace("[null]" " " '<td><p>'.$day->DATE.'</p>'.$day->LOCATION.'<br />'.$day->TIME.'<br /><a href="#">'.$day->TITLE.'</a>'); 
    $c++;

    ?>

  • Users who have thanked mlseim for this post:

    RottenRobbie (06-26-2010)

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    What if your XML file starts on a Wednesday?
    For each empty cell that a month needs, there will be a null DAY child.
    For instance, this month starts on a tuseday.. I would add two null DAYs.
    If it started on a wednesday, there would be three null days..
    ..such as this:
    PHP Code:
    <DAY>
    <
    DATE>[null]</DATE>
    <
    LOCATION>[null]</LOCATION>
    <
    TIME>[null]</TIME>
    <
    TITLE>[null]</TITLE>
    <
    NUMBER>[null]</NUMBER>
    </
    DAY
    Quote Originally Posted by mlseim View Post
    My idea below will still have a problem with an extra <tr> at the
    end of the list ... but it might give you an idea of where to start ...
    I loved it thank you. When I finish I will post my result
    I am creating a simple calendar management system for a clients website.


  •  

    Posting Permissions

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