Mixing jquery with php

Discussion in 'PHP' started by makamo66, Jan 3, 2013.

  1. #1
    I have a jquery calendar in my cakephp application with


    <?php //foreach ($mealplans as $mealplan){ ?>
    				{ 
    					title: '<?php echo $mealplan['Mealplan']['event_name'] . ", ". $mealplan['Mealplan']['total_calories']. " cals"; ?>',
    					start: new Date(y, m, <?php
    					$event = $mealplan['Mealplan']['event_date']; echo substr($event, 0, -8); ?>, 0),
    					end: new Date(y, m, 16),
    					allDay: true,
    					url: 'http://localhost/mealplans/view/1'
    				},
    				<?php //} ?>
    
    Code (markup):
    When I uncomment foreach and its curly bracket at the end then it prints out the first of my meal plans on January 15 until January 16 but I want it to print out all of them. Most of the php is working in the jquery code it's just this one part that doesn't work. When I uncomment the foreach and its end bracket then the calendar disappears. You can see it at http://myownmealplanner.com/calendars/navigate The calendar code is from http://arshaw.com/fullcalendar/docs/usage/
     
    makamo66, Jan 3, 2013 IP
  2. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    I found the problem. One of the meal plans had no date associated with it and the jquery was corrupted as a result. It works now at localhost.
     
    makamo66, Jan 3, 2013 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    You have one other glaring train wreck (that most people are guilty of) - open PHP, write your code and close PHP. ONCE. Stop opening and closing it all over the place.
    
    <?php
    foreach ($mealplans as $mealplan){
      	echo 'title: '.$mealplan['Mealplan']['event_name'] . ", ". $mealplan['Mealplan']['total_calories']. " cals,";
     	echo 'start: new Date(y, m, '; 	$event = $mealplan['Mealplan']['event_date']; 
    	echo substr($event, 0, -8).', 0),';
     	echo 'end: new Date(y, m, 16),';
     	echo 'allDay: true,';
     	echo 'url: "http://localhost/mealplans/view/1"'; 
    }
    ?> //assuming this is the end of the file
    
    PHP:
    I may have caused a typo or two, but you get the idea.
     
    Rukbat, Jan 3, 2013 IP