Jump to content

Need a tiny push with this array


arenaninja

Recommended Posts

I use the following code to create a 48x7 array for my application:

                    // Loop through each time division
                    for($i=0; $i<$div; $i++)
                    {
                        // Loop through the week we're displaying
                        for($j=0;$j<$dayPerPage; $j++)
                        {
                            $dateArray = getDate( mktime( 0, 0, 0, date("m"), date("d")+$j+$dateOffset, date("Y") ) );	
                            $date = sprintf('%04d-%02d-%02d',$dateArray["year"],$dateArray["mon"],$dateArray["mday"]);	

                            $datetime[]="$time|$date";
                            
                            list($start_date) = explode('|',$datetime[1]);
                            list($end_date) = explode('|',end($datetime));
                        }
                        $min += $step_size;
                        if($min >= 60)
                        {			
                            $min = 0;
                            $hr++;		
                        }
                        $time = sprintf('%02d:%02d:00',$hr,$min);
                    }

Now, I'm under the assumption that list($start_date) and list($end_date) should give me the beginning and ending dates inside the array (say 2011-10-01 and 2011-10-07), but instead I get the beginning and ending times in it ('00:00:00' AND '23:30:00'). Could someone clarify what I'm doing wrong and how I can fix it? I'm really new to php  :(

 

EDIT: Sorry... used the wrong tags for the code the first time around. Fixed.

Link to comment
Share on other sites

fixed the tags in previous post, sorry about that. Also, since I don't need to overwrite these I rewrote the code slightly:

    // Loop through each time division
    for($i=0; $i<=($div-1); $i++)
    {
        // Loop through the week we're displaying
        for($j=0;$j<$dayPerPage; $j++)
        {
            $dateArray = getDate( mktime( 0, 0, 0, date("m"), date("d")+$j+$dateOffset, date("Y") ) );
            $date = sprintf('%04d-%02d-%02d',$dateArray["year"],$dateArray["mon"],$dateArray["mday"]);

            $datetime[]="$time|$date";
        }
        $min += $step_size;
        if($min >= 60)
        {			
            $min = 0;
            $hr++;		
        }
        $time = sprintf('%02d:%02d:00',$hr,$min);
    }

    list($start_date) = explode('|',$datetime[1]);
    list($end_date) = explode('|',end($datetime));

But $start_date and $end_date still give me the beginning and ending times, not dates.

Link to comment
Share on other sites

I happen to know the person who posted the starting basis of that code, and he wants to know how come you swapped "$date|$time" to be "$time|$date", especially since that is why you are only getting the time part of the result.

The way he originally set it up was that the array was populated by half-hour increments and then move on to the next day, but I need to post the results on a table that displays the results for the week and I can only control the rows through <tr> command (and there isn't an equivalent to control the content in columns, as far as I know), so I needed to group them by time in the array instead of by date.

Incidentally, I got it to work, though I'm still not sure why it wouldn't the way I had it:

    // Loop through each time division
    for($i=0; $i<=($div-1); $i++)
    {
        // Loop through the week we're displaying
        for($j=0;$j<$dayPerPage; $j++)
        {
            $dateArray = getDate( mktime( 0, 0, 0, date("m"), date("d")+$j+$dateOffset, date("Y") ) );
            $date = sprintf('%04d-%02d-%02d',$dateArray["year"],$dateArray["mon"],$dateArray["mday"]);

            $datetime[]="$date|$time";
        }
        $min += $step_size;
        if($min >= 60)
        {			
            $min = 0;
            $hr++;		
        }
        $time = sprintf('%02d:%02d:00',$hr,$min);
    }

    list($start_date) = explode('|',$datetime[0]);
    list($end_date) = explode('|',end($datetime));

And this is how the data is populated (not fully debugged yet, some variables have become redundant and others obsolete, but the idea is there):

// iterate over the date/time slots and display any data
    $k = 1;
    foreach($datetime as $dt)
    {
        // Check for new day
        if($k==1 || $k%7==1)
        {
            // default background color
            echo "<tr><td bgcolor=\"#c0c0c0\">";

            if($startHr < 12 )
                    echo $startHr.":".$startMin."AM";
            else
                    echo $startHr.":".$startMin."PM";   

            $startMin += $step_size;
            if( $startMin >= 60 )
            {
                    $startMin = "00";
                    $startHr++;
            }
            echo " - ".$startHr.":".$startMin;

            if ($startHr<12)
                    echo "AM";
            else
                    echo "PM";
            echo "</td>";
        }
        if(isset($data[$dt]))
        {
            $res_duration = $data[$dt]["timestart"]-$data[$dt]["timeend"];
            // data exists for this date/time slot
            echo "<td rowspan=\"". (($res_duration)/$step_size) . "\"><strong>Name: </strong>".
                    $row["first_name"]." ".$row["last_name"]."<br><strong>Email:</strong>
                    <a href=\"mailto:".$row["email"]."\">".$row["email"]."</a>";
        } 
        else 
        {
            // no data for this date/time slot		
            echo"<td><a href=\"signupnow.php?date=".$date."&time=".$oldSHr.":".$oldSMin."&id=".$equipid."\">Sign up</a></td>";
        }
        // Check if day has ended and move to new column
        if($k%7==0)
        {
            echo "</tr>";
        }

        $k++;
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.