views:

65

answers:

1

Can someone please show me how to do this basic thing using Zend Framework MVC?

I'm looping over the timestamp data and populating my table that way. i don't understand how I would pull my presentation HTML from this loop and stick it in the view? Any help would be greatly appreciated!

<table>
<?php
  $day = date("j");
  $month = date("m");
  $year = date("Y");        
  $currentTimeStamp = strtotime("$year-$month-$day"); 
  $numDays = date("t", $currentTimeStamp); 
  $counter = 0; 

            for($i = 1; $i < $numDays+1; $i++, $counter++) 
            { 
                $timeStamp = strtotime("$year-$month-$i"); 
                if($i == 1) 
                { 
                // Workout when the first day of the month is 
                $firstDay = date("w", $timeStamp); 

                for($j = 0; $j < $firstDay; $j++, $counter++) 
                echo "<td>&nbsp;</td>"; 
                } 

                if($counter % 7 == 0) {
                  echo "</tr><tr>"; 
                }

                    echo "<td>" .$i . "</td>";

            }
?> 
</table>

I'm wanting to turn the above code into functions, but the HTML is throwing me off.

+2  A: 
Yanick Rochon
Hey thanks. This is working very well for me. I just returned $calTable and sent it to the view and it works great! Thanks again.
Joel
if this code is generated outside a view script, or outside a view helper, then you are breaking the MVC pattern (because you're generating HTML--view--data outside the view fragment of your dispatch request :) ) However, glad it helped!
Yanick Rochon
I am using Zend Framework, so I want to stick with (and learn) MVC. After the $calTable = implode line above, I returned calTable. I created a new instance of the code as an object from the controller and then displayed it in the view. Sorry if I'm missing correct terminology. Is this the correct way? It is working, but I do want to follow proper MVC. That was the basis for this question-I'm not sure how to handle this situation and keep the HTML only in the view.
Joel
technically, you could create a view helper that would accept an array as parameter, for example $data; that array would be populated with whatever event you need to be displayed in your calendar table. The view helper would then have this code in it, and populate $cal with the data in the given $data array. So, your controller calls a model that returns the $data array and sets it in the view ($this->view->calData = $data;). In your view, echo the view helper's returned value (the given code) with $this->createCalendarTable($this->calData); ...Is this clear?
Yanick Rochon
I generally understand, but I have never had to do something like determine the split of the table based on the array. So the above code won't work in a pure MVC fashon is that correct? Would you be willing to show me how you split that array so the view would know wether it is just feeding to a '<td>' or a new row? That's what I don't understand. I figured there must be an easy way to do this with partialLoop, but I'm still a newbie so I don't really understand that well enough to get my code to work with it.
Joel
solution edited. Cheers!
Yanick Rochon
Thanks so much! It's almost 5am here so I have to rest, but I will look at it in the morning.
Joel
yeah... it's right about that here too.... going to bed now :) Glad I could help!
Yanick Rochon