Jump to content


Photo

looping through array


  • Please log in to reply
1 reply to this topic

#1 mattastic

mattastic
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 06 May 2006 - 01:58 PM

Hi Folks.

I have my array that I will populate with a title and startdate and enddate. This will be done using a query but I've done it manually here so you can see the test data.

I then want to loop thorugh the days in a month and highlight the days that will have events. Problem is I get 3 of everything, here is the result of this page:

[a href=\"http://www.w00t.biz/result.html\" target=\"_blank\"]http://www.w00t.biz/result.html[/a]

Can anyone help me with this please?

Thanks

<?php


$n = 1;
for ($n = 1; $n <= 3; $n++){

//set event dates to array
    $events['event1']['title'] = 'test event 1';
    $events['event1']['startdate'] = mktime(0, 0, 0, 3, 5, 2006);
    $events['event1']['enddate'] = mktime(0, 0, 0, 3, 8, 2006);
    
    $events['event2']['title'] = 'test event 2';
    $events['event2']['startdate'] = mktime(0, 0, 0, 3, 6, 2006);
    $events['event2']['enddate'] = mktime(0, 0, 0, 3, 7, 2006);
    
    $events['event3']['title'] = 'test event 3';
    $events['event3']['startdate'] = mktime(0, 0, 0, 3, 22, 2006);
    $events['event3']['enddate'] = mktime(0, 0, 0, 3, 26, 2006);
    
    
    echo $events['event'.$n]['title'] . "<br>";
    echo date('d',$events['event'.$n]['startdate']) . "<br>";
    echo date('d',$events['event'.$n]['enddate']) . "<br>";
    echo "<br><br>";
}


echo "<p><p><p>";
for ($p = 1; $p <= 31; $p++) {

  $i = 1;
while (isset($events['event'.$i]))
        {
          
        if (
        (date('d',$events['event'.$i]['startdate'])  < $p AND date('d',$events['event'.$i]['enddate'])  > $p )
        OR
        (date('d',$events['event'.$i]['startdate']) == $p
        OR
        date('d',$events['event'.$i]['enddate'])  == $p
        ))
            {
                echo "<b>" . $p . "</b>" . "<br>";
            }
            else{
              echo $p . "<br>";
            }

        $i++;
        
        }

}

?> 


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 May 2006 - 06:09 PM

You get everything 3 times as it's all inside the first for loop;

Try this

// array of days in month
$days = array();
for ($i=1; $i <= 31; $i++) {
    $days[$i] = array();
}

//set event dates to array
    $events['event1']['title'] = 'test event 1';
    $events['event1']['startdate'] = mktime(0, 0, 0, 3, 5, 2006);
    $events['event1']['enddate'] = mktime(0, 0, 0, 3, 8, 2006);
    
    $events['event2']['title'] = 'test event 2';
    $events['event2']['startdate'] = mktime(0, 0, 0, 3, 6, 2006);
    $events['event2']['enddate'] = mktime(0, 0, 0, 3, 7, 2006);
    
    $events['event3']['title'] = 'test event 3';
    $events['event3']['startdate'] = mktime(0, 0, 0, 3, 22, 2006);
    $events['event3']['enddate'] = mktime(0, 0, 0, 3, 26, 2006);
    
    
echo '<pre>', print_r($events, true), '</pre>';

// add events to relevant days
foreach ($events as $ev_data) {
    $s = date('j', $ev_data['startdate']);
    $e = date('j', $ev_data['enddate']);
    for ($i = $s; $i <= $e; $i++) {
        $days[$i][] = $ev_data['title'];   
    }
}

// print results
foreach ($days as $d => $ev)  {
    echo "$d : " , join (', ', $ev), '<br />'; 
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users